Mock 1
Mock 1
Problem-1
Accept a string of length three as input and print all possible three-letter strings that can be
formed using the characters in the original string. Repetition is allowed. Print the strings in
alphabetical order, one string on each line. You can assume that all characters in the input string
will be unique.
Solution
1 word = ''.join(sorted(input()))
2 for a in word:
3 for b in word:
4 for c in word:
5 print(a, b, c, sep = '')
aaa
aab
aac
aba
abb
abc
aca
acb
acc
baa
bab
bac
bba
abc bbb
bbc
bca
bcb
bcc
caa
cab
cac
cba
cbb
cbc
cca
ccb
ccc
Input Output
111
112
113
121
122
123
131
132
133
211
212
213
221
321 222
223
231
232
233
311
312
313
321
322
323
331
332
333
Input Output
ppp
ppq
ppr
pqp
pqq
pqr
prp
prq
prr
qpp
qpq
qpr
qqp
qpr qqq
qqr
qrp
qrq
qrr
rpp
rpq
rpr
rqp
rqq
rqr
rrp
rrq
rrr
Input Output
000
001
009
010
011
019
090
091
099
100
101
109
110
109 111
119
190
191
199
900
901
909
910
911
919
990
991
999
Input Output
aaa
aac
aat
aca
acc
act
ata
atc
att
caa
cac
cat
cca
atc ccc
cct
cta
ctc
ctt
taa
tac
tat
tca
tcc
tct
tta
ttc
ttt
Problem-2
numbers.txt is a file that has a sequence of comma separated integers on each line. The first
three lines of the file are given for your reference:
1 1,2,3,4,5
2 3,1,10,9,8,4,6
3 5
Write a function named process_line that accepts a non-negative integer — i — as input. Find
the following information about line-i in the file:
1 def process_line(i):
2 '''
3 Argument: non-negative integer
4 Return: (N, S, P) --- tuple of integers
5 '''
If i is greater than or equal to the number of lines in the file, return the tuple (-1, -1, -1) . Zero-
based indexing is used. So, i = 0 corresponds to the first line in the file, i = 1 the second line
and i = n - 1 corresponds to the last line in a file of n lines.
You do not have to accept input from the user or print the output to the console. You just have to
write the function definition. However, within the function, you have to read the file
numbers.txt .
Solution
1 def process_line(i):
2 with open('numbers.txt', 'r') as f:
3 lines = f.readlines()
4 rows = [ ]
5 for line in lines:
6 row = [int(num) for num in line.strip().split(',')]
7 rows.append(row)
8 if i >= len(rows):
9 return -1, -1, -1
10 row = rows[i]
11 S, P, C = 0, 1, len(row)
12 for num in row:
13 S += num
14 P *= num
15 return C, S, P
Private Test Cases
Input Output
3 7,70,10000000
4 2,1002,1001
5 3,24,504
7 10,45,0
50 -1,-1,-1
Problem-3
students is a list of lists. Each element in students is a list of courses done by a student in a
semester. An example list is as follows:
[['math', 'phy', 'chem', 'cs'], ['math', 'phy'], ['math', 'chem'], ['history',
'eco']]
The first student has done the courses ['math', 'phy', 'chem', 'cs'] , the last student has
done ['history', 'eco'] . You get the idea.
Your task is to write two functions:
(1) Write a function named consolidate that accepts students as an argument and returns a
dictionary named consol that has the following structure: courses are the keys; value for a given
key is the number of students who have done that course.
1 def consolidate(students):
2 '''
3 Argument: students --- a list of lists,
4 each element of an inner list is a string (course name)
5 Return: consol --- a dictionary:
6 key: string (corresponds to course name)
7 value: number of students who have done the above course
8 '''
(2) Write a function named popular that accepts the dictionary returned by consolidate as
argument. It should return the course that has been done by the maximum number of students.
You can assume that there will always be exactly one such course.
1 def popular(consol):
2 '''
3 Argument: consol --- a dictionary:
4 key: string (corresponds to course name)
5 value: number of students who have done the above course
6 Return: popular_course --- a string
7 the name of the course which has been done by the most number
of students
8 '''
You do not have to accept input from the user or print the output to the console. You just have to
write the function definition for two functions: consolidate and popular. Calling the functions will
be the responsibility of the autograder.
Solution
1 def consolidate(students):
2 P = dict()
3 for student in students:
4 for course in student:
5 if course not in P:
6 P[course] = 0
7 P[course] += 1
8 return P
9
10 def popular(P):
11 max_course, max_enroll = None, -1
12 for course, enroll in P.items():
13 if enroll > max_enroll:
14 max_course, max_enroll = course, enroll
15 return max_course
Input Output
CS11:2
CS12:2
[['CS11', 'MA12'], ['HS23', 'MA12'], ['HS23', 'CS12'], ['HS23', 'MA11'], ['HS23', HS23:4
'MA12', 'CS11', 'CS12']] MA11:1
MA12:3
Popular:HS23
ch:6
cs:2
[['ma', 'ch', 'cs'], ['ma'], ['ch'], ['ch', 'ph'], ['ch'], ['ch', 'ma', 'cs', 'ph'], ['ma', 'ch'], hs:1
['hs']] ma:4
ph:2
Popular:ch
CS1:2
CS2:1
CS3:1
CS4:1
[['CS1', 'CS2'], ['CS3', 'CS4'], ['CS5', 'CS6', 'CS7'], ['CS1']]
CS5:1
CS6:1
CS7:1
Popular:CS1
CS1:2
CS2:4
CS3:2
[['CS1', 'CS2'], ['CS3', 'CS4'], ['CS5', 'CS6', 'CS7'], ['CS1'], ['CS2', 'CS3'], ['CS2'], CS4:2
['CS2', 'CS4']] CS5:1
CS6:1
CS7:1
Popular:CS2
math:1
[['math']]
Popular:math
Problem-4
This problem is about reversing a square matrix along row or column:
The first line of the input will be an integer n , which denotes the dimension of the square matrix.
Each of the next n lines in the input will have a sequence of n comma-separated integers. The
last line in the input will be one of these two words: row or column . If it is row, then reverse the
matrix along the row, else, reverse it along the column.
Print the reversed matrix as output: each line should contain one row of the matrix as a sequence
of comma-separated integers.
Solution
1 n = int(input())
2 mat = [ ]
3 for i in range(n):
4 mat.append([int(word) for word in input().split(',')])
5 axis = input()
6
7 out = [[0 for _ in range(n)] for _ in range(n)]
8 if axis == 'row':
9 for i in range(n):
10 for j in range(n):
11 out[i][j] = mat[n - i - 1][j]
12 else:
13 for i in range(n):
14 for j in range(n):
15 out[i][j] = mat[i][n - j -1]
16
17
18 for i in range(n):
19 for j in range(n):
20 if j != n - 1:
21 print(out[i][j], end = ',')
22 else:
23 print(out[i][j])
3
1,2,3 7,8,9
4,5,6 4,5,6
7,8,9 1,2,3
row
3
1,2,3 3,2,1
4,5,6 6,5,4
7,8,9 9,8,7
column
3
1,1,1 1,1,1
1,0,1 1,0,1
0,1,1 1,1,0
column
4
1,2,3,4 13,14,15,16
5,6,7,8 9,10,11,12
9,10,11,12 5,6,7,8
13,14,15,16 1,2,3,4
row
4
1,2,3,4 4,3,2,1
5,6,7,8 8,7,6,5
9,10,11,12 12,11,10,9
13,14,15,16 16,15,14,13
column
Problem-5
In spreadsheets, columns are labeled as follows:
1 Label Number
2 A 1
3 ... ...
4 Z 26
5 AA 27
6 ... ...
7 AZ 52
8 BA 53
9 ... ...
10 ZZ 702
11 AAA 703
12 ... ...
13 AAZ 728
14 ABA 729
15
A is the first column, B is the second column, Z is the 26th column and so on. The three dots
represent the missing labels and their column numbers. Using the table given above, deduce the
mapping between column labels and their corresponding numbers. Accept the column label as
input and print the corresponding column number as output.
Solution
1 mapping = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
2 def colnum(col):
3 base = mapping.index(col[0]) + 1
4 if len(col) == 1:
5 return base
6 return base * (26 ** (len(col) - 1)) + colnum(col[1: ])
7
8 print(colnum(input()))
Input Output
ABCD 19010
AZEQT 917794
ZAERG 11902807
ABCDEFGHIJK 152686330658691
ZZZZZZZ 8353082582