Grpa Week 6 Solutions
Grpa Week 6 Solutions
PROGRAM 1:
The scores dataset is a list of dictionaries one of whose entries is given below
for your reference:
SOLUTION 1:
PROGRAM 2:
SOLUTION 2
def freq_to_words(words):
word_set=set(words)
d={}
for i in word_set:
count=0
for j in range(len(words)):
if words[j]==i:
count+=1
if (count in d):
d[count].append(i)
else:
d[count]=[i]
return d
PROGRAM 3:
Write a function named rotate that accepts a matrix mat as argument. It should
return a matrix that is rotated by 90^{\circ}90
∘
in the clockwise direction. For example:
SOLUTION 3:
def rotate(mat):
final=[]
row=len(mat)
column=len(mat[0])
for i in range(column):
x=[]
for j in range(row):
x.append(mat[row-1-j][i])
final.append(x)
x=[]
return final
PROGRAM 4:
Write a function named two_level_sort that accepts a list of tuples named scores as
argument. Each element in this list is of the form (Name, Marks) and represents the
marks scored by a student in a test: the first element is the student's name and
the second element is his or her marks.
SOLUTION 4:
def two_level_sort(scores):
d={}
score_list=[]
for i in scores:
if(i[1] in d):
d[i[1]].append(i[0])
else:
d[i[1]]=[i[0]]
score_list.append(i[1])
score_list.sort()
d_level1_sort={}
for i in score_list:
d_level1_sort[i]=d[i]
d_level1_sort[i].sort()
final=[]
for i in score_list:
for j in d_level1_sort[i]:
x=(j,i)
final.append(x)
return final