MID Term Assessment Testqs and Ans

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

MID Term Assessment Test

CodeTest 1a

Product sequence is generated by multiplying the indices of the position.

 i-th term  in the Product  sequence, denoted by Ai  is defined as the Product of the numbers from 1 to i.

The Product sequence is recursively defined as : Ai=A(i-1)*i, i >1, A1=1. For example, A5=1 * 2 * 3 * 4
* 5 = 120

Product sequence with five terms would be:

1, 2, 6, 24, 120

Given a value ‘n’, build a dictionary with first ‘n’ terms of  the Product sequence as keys and the list of
their odd divisors as their values. For example, if ‘n’ is 5, then the expected output is:

{1: [1], 2: [1], 6: [1, 3], 24: [1, 3], 120: [1, 3, 5, 15]}

Print the dictionary in ascending order of key. Use pprint function for printing dictionary in sorted order.

Syntax for pprint is

pprint(dictionary name)

Include the line “from pprint import pprint” in the top of your program

Input Format

First line contains the number of terms in the Product sequence, n

Output Format

A dictionary with terms of the product sequence  as key and their odd divisors as values. Print the
dictionary in ascending order of key

Codetest-1b

Index sum sequence is generated by summing the indices of the position. i-th term in the Index sum
sequence, denoted by Ai  is defined as Ai= sum of the numbers from 1 to i.  Index sum sequence is 
recursively defined as : Ai=A(i-1)+i, i >1, A1=1. For example, A5=

1 + 2 + 3 + 4 + 5 = 15.
Index sum sequence upto  eight terms would be:

1, 3, 6, 10, 15, 21, 28, 36, …

Given a value ‘n’, build a dictionary with first ‘n’ terms in the sequence as keys and the list of their
divisors as their values. For example, if ‘n’ is 8, then the expected output is:

 {1 : [1], 3: [1,3], 6: [1,2,3,6], 10:[1,2,5,10], 15:[1,3,5,15], 21:[1,3,7,21], 28:[1,2,4,7,14,28], 36:


[1,2,3,4,6,8,9,12,18,36]}

Print the dictionary in ascending order of key. Use pprint function for printing dictionary in sorted order.

Syntax for pprint is

pprint(dictionary name)

Include the line “from pprint import pprint” in the top of your program

Input Format

First line contains the number of terms in the sequence, n

Output Format

A dictionary with  the terms of ‘index sum sequence’ as key and their divisors as values. Print the
dictionary in ascending order of key

Codetest – 2a

Given three positive integers, write an algorithm and the subsequent Python code to check whether the
given three integers can represent the length of the three sides of a triangle. In any triangle, sum of the
lengths of any two sides must be greater than the length of the third side. For example, since 3 < 4+5, 4 <
3+5 and 5<3+4, the triplet (3, 4, 5) can represent the lengths of the of sides of a triangle. Since the sum
5+4 is not greater 11 the triplet (5,4,11) cannot represent the length of sides of a triangle.

Input Format:

First integer

Second integer

Third integer
Output Format:

Print Yes if they can represent the length of sides of a triangle and No otherwise

Codetest 2b

BCCI maintains a table of team member’s run score in three one day matches. Players are identified by
their  Player_ID. Team managers check the member’s performance based on their run scores in 3matches
in a year. Given the details of the players in the team, their score in three matches and a player ID ‘p’,
write an algorithm and the subsequent Python code to determine the total score and the position of player
‘p’. Player ‘p’ is in position 1 if he has got the maximum score. Assume that all players have different
total score. For example, if the given Player_ID is A1 then the expected output is

A1

132

Player_I Score  
D s
12  

A1 25  

95  
22
 
A2 55

60
20

A3 25

0
78

A4 65

0
54

A5 76

0  Input Format
Total No.of. Players

Player_id1,Score_1,Score_2,Score_3

Player_id2,Score_1,Score_2,Score_3

Player_idn,Score_1,Score_2,Score_3

Player_id to check performance

Output Format

Player_ID

Total Score in 3 Matches

Player Position

Debugging

#. There are ‘n’ numbers of students in a class. They have secured some percentage mark in their CAT- I
Examination for the subject CSE1001. Find the maximum and minimum mark among the students.

'''

Input format:

Read n #no of elements to be stored in tuple

Read s # marks of each student.

Output format:

Print Minimum number

Print number students got Minimum marks.

Print Maximum number

Print number students got Maximum marks.


'''

marks=()

n=int(input())

for i in range(0,n):

s = int(input())

marks = marks+(s,)

min=max=marks[0]

#Start Deb

for i in range(1,n):

if(min>marks[i]):

min=marks[i]

if(max<marks[i]):

max=marks[i]

#End Deb

print(min)

print(max)

#2. Design a search engine for train dataset like “Googletrain” which is used to search whether the
particular train is running for the day or not. The train dataset contains “Train Number” as a field . Write
a python program to get train number as the input and to locate whether that particular train number is
available in the dataset or not. If the train is running on the particular day then display ”1”, else display
“0”.

'''

Input format:

Read n #no of elements to be stored in tuple

Read s # Train_No
Output format:

Print 1 if the train is available

Print 0 if the train is not available

'''

Train_No=()

n=int(input())

for i in range(0,n):

s = int(input())

Train_No = Train_No+(s,)

X=int(input())

i=0

count=0

#Start Deb

while (i<len(Train_No)):

if(X==Train_No[i]):

print(1)

count=count+1

break

else:

i=i+1

else:

print(0)

#End Deb
#3. Validate the Password using regular expression.

'''

Should have at least one number.

Should start with uppercase.

Should have at least one special symbol.

Should be between 6 to 10 characters long.

Sample Input:

Anusooya123#

Sample Output:

Invalid

Sample Input:

Anu123#

Sample Output

Valid

'''

import re

passwd = input()

#Start Deb

reg = "^(?=.*[A-Z][a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!#%*?&]{6,10}$"

#End Deb

pat = re.compile(reg)
mat = re.search(pat, passwd)

if mat:

print("Valid")

else:

print("Invalid")

#4

key_value ={}

key_value[2] = 56

key_value[1] = 2

key_value[5] = 12

key_value[4] = 24

key_value[6] = 18

key_value[3] = 323

#SD

for i in sorted (key_value.keys()) :

print(i, end = " ")

#ED

#5

x=5

x >>= 3

#
print(x)

#output0

#6

#SD

adj = ["red", "big", "tasty"]

fruits = ["apple", "banana", "cherry"]

#ED

for x in adj:

for y in fruits:

print(x, y)

'''

red apple

red banana

red cherry

big apple

big banana

big cherry

tasty apple

tasty banana

tasty cherry '''

#7

#SD

for x in range(6):
print(x)

#ED

else:

print("Finally finished!")

'''

for x in range(6):

print(x)

else:

print("Finally finished!")

Finally finished!

'''
#8

i=1

while i < 6:

print(i)

if (i == 3):

break

#SD

i += 1

#ED

'''

'''

#9

#Check if the string contains either "falls" or "stays":

import re

txt = "The rain in Spain falls mainly in the plain!"

#SD

x = re.findall("falls|stays", txt)

print(x)

#ED

if x:
print("Match")

else:

print("No match")

#10

#Return a match at every no-digit character:

import re

txt = "The rain in Spain"

#SD

x = re.findall("\D", txt)

#ED

if x:

print("Match")

else:

print("No match")

MCQ

1. import re

txt = "ani is a good girl"

x = re.findall("[^a-s]^\S", txt)

if x:

print("Match")

else:

print("No match")

Output
1. Match
2. No Match
3. Error
4. None

2.

import re

txt = "The rain in Spain"

x = re.findall("Spain\Z", txt)

if x:

print("Match")

else:

print("No match")

Output

1. Match
2. No Match
3. Error
4. None

3.

mydoubler = lambda a,n : a * n

mytripler = lambda a,n : a * n

print(mydoubler(11,2),mytripler(11,3))

Output

22 33

1111 111111

23
Error

4.

key_value ={}

key_value[2] = 56

key_value[1] = 2

key_value[5] = 12

key_value[4] = 24

key_value[6] = 18

key_value[3] = 323

print(sorted(key_value.items(), key =lambda kv:(kv[1], kv[0])))

output

[(1, 2), (5, 12), (6, 18), (4, 24), (2, 56), (3, 323)]

{(1, 2), (5, 12), (6, 18), (4, 24), (2, 56), (3, 323)}

[(1, 2), (2, 56), (3, 323) ,(4, 24), (5, 12), (6, 18)]

{(1, 2), (2, 56), (3, 323) ,(4, 24), (5, 12), (6, 18)}

5.

x=2

y=5

print(x ** y)

output

1. 32
2. 10
3. 25
4. 20
1

6.

x = ["apple", "banana"]

print("pine apple" not in x)

output

1. True
2. False

True

7.

x = ["apple", "banana"]

y = ["apple", "banana"]

z=x

print(x is not z)

output

1. True
2. False

False

8.

list1 = ["abc", 34, True, 40, "male"]

print(type(list1))

output

["abc", 34, True, 40, "male"]

abc,34,True,40,male

<class 'list'>
['abc', 34, True, 40, 'male']
3
9.

list1 = ["a", "b", "c"]


list2 = [1, 2, 3]

Identify which will print the output as ['a', 'b', 'c', 1, 2, 3]

Output

list3 = list1 + list2

for x in list2:

list1.append(x)

list1.extend(list2)

All the above

10.

thistuple = ("apple")

print(type(thistuple))

output:

<class 'tuple'>

<class 'str'>

<class ‘thistuple’>

<class,class>

11.

fruits = ("apple", "banana", "cherry", "strawberry", "raspberry")

(green, yellow, *red) = fruits

print(green)

print(yellow)
print(red)

output:

1. apple
banana
['cherry', 'strawberry', 'raspberry']
2. apple
banana
cherry
strawberry
raspberry
3. apple
banana
cherry

4. apple
banana
cherry strawberry raspberry
1

12.

numb = (1,2,3)

mytuple = numb * 2

print(mytuple)

output

(1, 2, 3, 1, 2, 3)
2, 4, 6

11, 22, 33

(1, 1, 2, 2, 3, 3)

13.

thisset = set(("apple", "banana", "cherry"))

print(thisset)

output:

Similar output of thisset enclosed with { }


Random output of thisset enclosed with { }

Similar output of thisset enclosed with ( )

Random output of thisset enclosed with ( )

14.

set1 = {"apple", "banana", "cherry"}

set2 = {1, 5, 7, 9, 3}

set3 = {True, False, False}

print(set1+set2+set3)

print(set2)

print(set3)

Output

1. Error
2. {'apple', 'cherry', 'banana'}
{1, 3, 5, 7, 9}
{False, True}
3. { {'apple', 'cherry', 'banana'},{1, 3, 5, 7, 9},{False, True}}
{1, 3, 5, 7, 9}
{False, True}
4. {'apple', 'cherry', 'banana', 1, 3, 5, 7, 9,False, True}
{1, 3, 5, 7, 9}
{False, True}
1

15.

thisset = {"apple", "banana", "cherry"}

thisset.remove("bananan")

print(thisset)

output

Error

{'banana', 'cherry', 'apple'}


{'cherry', 'apple'}
{“cherry”, “apple”}

16.

thisset = {"apple", "banana", "cherry"}

del thisset

print(thisset)

output:

Error

{"apple", "banana", "cherry"}

{'banana', 'cherry', 'apple'}


banana,cherry,apple

17.

x = {"apple", "banana", "cherry"}

y = {"google", "microsoft", "apple"}

z = x.intersection(y)

x.intersection_update(y)

print(z)

print(x)

Output

1. {'apple'}

{'apple'}

2. {'apple'}
{'google', 'microsoft', 'apple'}
3. {'google', 'microsoft', 'apple'}
{'apple'}
4. {'google', 'microsoft'}
{'banana', 'cherry'}

1
18.

x1 = {"f", "e", "d", "c", "b", "a"}

y1 = {"a", "b", "c"}

x2 = {"f", "e", "d", "c", "b"}

y2 = {"a", "b", "c"}

z1 = x1.issuperset(y1)

z2 = x2.issuperset(y2)

print(z1)

print(z2)

Output

1. True

False

2. False

True

3. True

True

4. False

False

19.

thisdict = {

"brand": "Ford",

"model": "Mustang",

"year": 1964

}
thisdict.pop()

print(thisdict)

output

1. Error
2. {'brand': 'Ford', 'year': 1964}
3. {'brand': 'Ford', 'model': ‘Mustang’}
4. {''model': ‘Mustang’, ‘year': 1964}

20.

What is the use of pass statement in python?

Output

To avoid getting error

To pass the loop

To print empty

To print nothing

You might also like