0% found this document useful (0 votes)
937 views2 pages

Grpa Week 6 Solutions

The document discusses 4 programming problems and their solutions. The problems include finding student toppers by subject and gender from a scores dataset, creating a frequency dictionary from a list of words, rotating a matrix 90 degrees clockwise, and sorting a list of student names and marks by score then alphabetically.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
937 views2 pages

Grpa Week 6 Solutions

The document discusses 4 programming problems and their solutions. The problems include finding student toppers by subject and gender from a scores dataset, creating a frequency dictionary from a list of words, rotating a matrix 90 degrees clockwise, and sorting a list of student names and marks by score then alphabetically.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

WEEK 6

PROGRAM 1:

The scores dataset is a list of dictionaries one of whose entries is given below
for your reference:

{'SeqNo': 1, 'Name': 'Devika', 'Gender': 'F', 'City': 'Bengaluru',


'Mathematics': 64, 'Physics': 48, 'Chemistry': 79, 'Biology': 75,
'Computer Science': 88, 'History': 43, 'Civics': 78, 'Philosophy': 55}

SOLUTION 1:

def get_toppers(scores_dataset, subject, gender):


score=[]
name=[]
for i in range(len(scores_dataset)):
if(scores_dataset[i]['Gender']==gender):
score.append(scores_dataset[i][subject])
max_score=max(score)
for i in range(len(scores_dataset)):
if scores_dataset[i]['Gender']==gender:
if(scores_dataset[i][subject]==max_score):
name.append(scores_dataset[i]['Name'])
return name

PROGRAM 2:

Write a function named freq_to_words that accepts a list of words as argument. It


should return a dictionary which has the following structure:

key: frequency of words in the list

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy