Test Questions
Question 1
You have an URL string:
"https://example.com/login?uid=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9&pwd=qH7Zj_m3kY69kxhaQXTa"
For this string you have to find the user name and password by string search method.
import re
url = "https://example.com/login?uid=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9&pwd=qH7Zj_m3kY69kxhaQXTa"
pattern = re.compile(r'uid=([a-zA-Z0-9]+)&pwd=([a-zA-Z0-9_]+)')
strs = pattern.search(url)
username = strs.group(1)
password = strs.group(2)
print(f"user_name: {username}, password: {password}")
Question 2
You have two arrays with equal number of elements. First array elements are any random integer number, and the second array have duplicate elements.
Condition 1: First array should not have any duplicate elements.
Condition 2: Second array must be sorted and in ascending order.
Now, you have to find the sum of the elements of first array according to the index of duplicate elements in the sorted second array.
First array is [10,12,5,19,15,6,7,18,9,11]
Second array is [1,2,5,1,5,2,3,4,3,2]
Sorted array is [1,1,2,2,2,3,3,4,5,5]
Now, for duplicate 1 (index: 0, 1) the sum of the elements of first array is 10+12, for duplicate 2 (index: 2, 3 , 4) the sum of the elements of first array is 5+19+15, and so on…
Print an array with the result [22, 39, 13, 18, 20]
arr1 = [10,12,5,19,15,6,7,18,9,11]
arr2 = [1,2,5,1,5,2,3,4,3,2]
sorted_arr2 = sorted(arr2)
index_dict = {}
for i, elem in enumerate(sorted_arr2):
if elem not in index_dict:
index_dict[elem] = [i]
else:
index_dict[elem].append(i)
result = []
for elem in set(arr2):
indices = index_dict[elem]
total = 0
for index in indices:
total += arr1[index]
result.append(total)
print(result)
Question 3
In a rail ticket counter there are five people waiting for getting ticket of Kolkata, Delhi, Mumbai, Chennai, Bangalore accordingly. Assign tickets to them in the way shown below:
People: A, B, C, D, E
Assign Tickets: [[‘A’, ‘Kolkata’], [‘B’, ‘Delhi’], [‘C’, ‘Mumbai’], [‘D’, ‘Chennai’], [E, ‘Bangalore’ ]]
people = ['A', 'B', 'C', 'D', 'E']
ticket = ['Kolkata', 'Delhi', 'Mumbai', 'Chennai', 'Bangalore']
give_ticket = []
assign_ticket = []
for i in range(len(people)):
give_ticket.extend([people[i], ticket[i]])
assign_ticket.append(give_ticket)
give_ticket = []
print(assign_ticket)
Question 4
A pipe has odd number of balls. One ball is taken from one end and that ball is thrown out and the next ball is taken out and inserted into the other end; this cycle is repeated. After performing this act which ball will be there in the pipe at last (Mention the ball number).
Condition: Minimum number of balls is 10.
Example:
Balls = [1,2,3,4,5,6,7,8,9,10,11]
Output = 6
balls = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
for i in range(len(balls)):
if len(balls) != 1:
balls.pop(0)
ball = balls.pop(0)
balls.append(ball)
print(*balls)
Question 5
Create a data frame “df1” with help of pandas. Data frame must have 3 columns and 6 rows. Then create another data frame “df2” have 2 columns and 5 rows. Join “df1” and “df2” and make “df”. Now remove all rows with Null values from “df”. Export this “df” as a CSV file.
import pandas as pd
ids1 = [1, 2, 3, 4, 5, 6]
ids2 = [7, 8, 9, 10, 11]
names1 = ['Subho', 'Vijay', 'Ricky', 'Ashley', 'Samya', 'Souvik']
names2 = ['Ratan', 'Ajay', 'Sanjay', 'Amitabh', 'Vicky']
rolls1 = [11, 12, 13, 14, 15, 16]
df1 = pd.DataFrame({'id': ids1, 'name': names1, 'roll_no': rolls1})
df2 = pd.DataFrame({'id': ids2, 'name': names2})
df = pd.concat([df1, df2], axis=0).set_index('id')
df.dropna(inplace=True)
df.to_csv("sample_df.csv", index=None)
df
Question 6
Create a 4X4 matrix of some random numbers with help of numpy. Reshape this matrix into an array. Sort this array with numpy also.
import numpy as np
# create a 4x4 matrix of random numbers
matrix = np.random.rand(4, 4)
# reshape the matrix into an array
array = matrix.reshape(-1)
# sort the array with numpy
sorted_array = np.sort(array)
print(sorted_array)