def build_table(l):
# Use a dictionary to build a frequency table
frequency = {}
# For each number, create a new entry in the table or increment the frequency
for n in l:
if n in frequency.keys():
frequency[n] = frequency[n] + 1
else:
frequency[n] = 1
return(frequency)
def sort_table(fdict):
# First build a list of the form (r,n)
flist = [ (fdict[n],n) for n in fdict.keys() ]
# Sort this list using built in sort, which will sort first by frequency, then by value
flist.sort()
# Flip each pair and return
return( [ (n,r) for (r,n) in flist ])
def histogram(l):
frequency_table = build_table(l)
return(sort_table(frequency_table))
def transcript(coursedetails, studentdetails, grades):
# Create dictionaries to store course details and student names
course_dict = dict(coursedetails)
student_dict = dict(studentdetails)
students= {}
for tup in grades:
temp = []
temp.append(tup[0])
temp.append(student_dict[tup[0]])
if tup[0] in students:
students[tup[0]][2].append((tup[1],course_dict[tup[1]],tup[2]))
else:
students[tup[0]] = temp
temp.append([(tup[1],course_dict[tup[1]],tup[2])])
lis = []
for i,k in students.items():
k[2].sort()
lis.append(tuple(k))
lis.sort()
return(lis)
def setup_coursedict(details):
dict = {}
for (ccode,cname) in details:
dict[ccode] = cname
return(dict)
def setup_studentdict(details):
dict = {}
for (rollno,name) in details:
dict[rollno] = name
return(dict)
def setup_gradedict(details):
dict = {}
for (rollno,ccode,grade) in details:
if rollno in dict.keys():
dict[rollno].append((ccode,grade))
else:
dict[rollno] = [(ccode,grade)]
return(dict)
# Hidden code below
import ast
fncall = input()
lparen = fncall.find("(")
rparen = fncall.rfind(")")
fname = fncall[:lparen]
farg = fncall[lparen+1:rparen]
if fname == "histogram":
arg = ast.literal_eval(farg)
print(histogram(arg),end="\n")
elif fname == "transcript":
arg = ast.literal_eval(farg)
print(transcript(arg[0],arg[1],arg[2]),end="\n")
else:
print("Function", fname, "unknown")
def transcript(coursedetails, studentdetails, grades):
# Create dictionaries to store course details and student names
course_dict = dict(coursedetails)
student_dict = dict(studentdetails)
students= {}
for tup in grades:
temp = []
temp.append(tup[0])
temp.append(student_dict[tup[0]])
if tup[0] in students:
students[tup[0]][2].append((tup[1],course_dict[tup[1]],tup[2]))
else:
students[tup[0]] = temp
temp.append([(tup[1],course_dict[tup[1]],tup[2])])
lis = []
for i,k in students.items():
k[2].sort()
lis.append(tuple(k))
lis.sort()
return(lis)