In: Computer Science
In python
def lambda_2(filename):
# Complete this function to read grades from `filename` and map the
test average to letter
# grades using map and lambda. File has student_name, test1_score,
test2_score,
# test3_score, test4_score, test5_score. This function must use a
lambda
# function and map() function.
# The input to the map function should be
# a list of lines. Ex. ['student1,73,74,75,76,75', ...]. Output is
a list of strings in the format
# studentname: Letter Grade -- 'student1: C'
# input filename
# output: (lambda_func, list_of_studentname_and_lettergrade) --
example (lambda_func, ['student1: C', ...])
# Use this average to grade mapping. Round the average
mapping.
# D = 65<=average<70
# C = 70<=average<80
# B = 80<=average<90
# A = 90<=average
grade_mapping = {} # fill this!
# YOUR CODE HERE
Example from file
student11,72,69,69,65,91
student12,78,84,76,76,83
student13,86,66,70,84,97
student14,85,84,97,87,88
student15,67,82,82,76,81
student16,98,73,72,85,77
student17,74,79,87,87,94
def lambda_2(filename):
#lambda function to get grade
gradeCalc = lambda x:'A'*(x>=90)+'B'*(x>=80 and
x<90)+'C'*(x>=70 and x<80)+'D'*(x>=65 and
x<70)+'F'*(x<65)
#lamda function to get required string. It takes a 2 element tuple
where 1st element is name, snd 2nd is list of score
mapper = lambda x:'{}:
{}'.format(x[0],gradeCalc(sum(x[1])/len(x[1])))
data=[]
with open(filename) as f:
for line in f:
tokens=line.strip().split(',')
name=tokens[0]
scores = [float(i) for i in tokens[1:]]
data.append((name,scores))
finalData = list(map(mapper,data))
return mapper,finalData