Subhradeep Rang/



Test Questions

Question 1

You have an URL string:


For this string you have to find the user name and password by string search method.

import re

url = ""
pattern = re.compile(r'uid=([a-zA-Z0-9]+)&pwd=([a-zA-Z0-9_]+)')
strs =
username =
password =
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]
result = []
for elem in set(arr2):
    indices = index_dict[elem]
    total = 0
    for index in indices:
        total += arr1[index]

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]])
    give_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.


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:
        ball = balls.pop(0)

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.to_csv("sample_df.csv", index=None)

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)

!pip install -q solara
Hidden output
import solara

clicks = solara.reactive(0)

def page():
    def increment_clicks():
        clicks += 1
    solara.Button(label=f"Clicked {clicks} times.", on_click=increment_clicks)
import pandas as pd

# Creating two sample DataFrames with index values
left = pd.DataFrame({'A': ['A0', 'A1', 'A2']},
                    index=['K0', 'K1', 'K2'])

right = pd.DataFrame({'B': ['B0', 'B1', 'B2']},
                     index=['K0', 'K2', 'K3'])

# Joining DataFrames based on their index values
result = left.join(right)
import pandas as pd

# Creating two sample DataFrames with a common column 'key'
left = pd.DataFrame({'key': ['K0', 'K1', 'K2'],
                     'A': ['A0', 'A1', 'A2'],
                     'B': ['B0', 'B1', 'B2']})

right = pd.DataFrame({'key': ['K0', 'K1', 'K2'],
                      'C': ['C0', 'C1', 'C2'],
                      'D': ['D0', 'D1', 'D2']})

# Merging DataFrames based on the 'key' column
result = pd.merge(left, right, on='key')