Question

In: Computer Science

In python def lambda_2(filename): # Complete this function to read grades from `filename` and map the...

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

Solutions

Expert Solution

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


Related Solutions

In python Complete the function get_Astring(filename) to read the file contents from filename (note that the...
In python Complete the function get_Astring(filename) to read the file contents from filename (note that the test will use the file data.txt and data2.txt provided in the second and third tabs), strip off the newline character at the end of each line and return the contents as a single string.
In Python write a function with prototype “def wordfreq(filename = "somefile.txt"):” that will read a given...
In Python write a function with prototype “def wordfreq(filename = "somefile.txt"):” that will read a given file that contains words separated by spaces (perhaps multiple words on a line) and will create a dictionary whose keys are the words and the value is the number of times the word appears. Convert each word to lower case before processing.
Python code def plot_dataset(file_path): """ Read in a text file where the path and filename is...
Python code def plot_dataset(file_path): """ Read in a text file where the path and filename is given by the input parameter file_path There are 4 columns in the text dataset that are separated by colons ":". c1:c2:c3:c4 Plot 3 datasets. (x axis vs y axis) c1 vs c2 (Legend label "n=1") c1 vs c3 (Legend label "n=1") c1 vs c4 (Legend label "n=1") Make sure you have proper x and y labels and a title. The x label should be...
Write a python program that has: function createCustomerRecord that has 1 parameter, the filename to read...
Write a python program that has: function createCustomerRecord that has 1 parameter, the filename to read from, the method then reads all the records from the file and returns a dictionary. Each record in the file has the following format CivilIdNumber Name Telephone#     Address CivilIdNumber Name Telephone#     Address CivilIdNumber Name Telephone#     Address Etc A record always consists of 4 lines (civilid, name, telephone and address). You can find a sample input file on last page of this assignment, copy it...
Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
Write a python program to read from a file the names and grades of a class...
Write a python program to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a gradesInput() function that reads data from a file and stores it and returns it as a dictionary....
def read_words(filename, ignore='#'): """ Read a list of words ignoring any lines that start with the...
def read_words(filename, ignore='#'): """ Read a list of words ignoring any lines that start with the ignore character as well as any blank lines. """ return ['a', 'z'] How would I code this in Python?
def compareRisk(compareCountry, countryList, filename):     filename = open(filename, "r")     content = filename.readlines()     returnList =...
def compareRisk(compareCountry, countryList, filename):     filename = open(filename, "r")     content = filename.readlines()     returnList = []     for row in content [1:]:         line = row.split(",")         name = line[0]         population = float(line[1])         infectedPopulation = float(line[2])     for compareCountry in content:         comparePopulation = float (line[1])         compareInfected = float (line[2])     for name in countryList:         if population < comparePopulation:             if infectedPopulation > compareInfected:                 returnList.append(name)             else:                 return "No countries"     return returnList...
Please use python3 Create the function team_average which has the following header def team_average(filename): This function...
Please use python3 Create the function team_average which has the following header def team_average(filename): This function will return the average number of games won by the Red Sox from a text file with entries like this 2011-07-02 Red Sox @ Astros Win 7-5 2011-07-03 Red Sox @ Astros Win 2-1 2011-07-04 Red Sox vs Blue Jays Loss 7-9 This function should create a file object for the file whose name is given by the parameter filename. If the file cannot...
PYTHON. Create a function that accepts a filename where in each line there is a name...
PYTHON. Create a function that accepts a filename where in each line there is a name of a type of bird. use a python dictionary in order to count the # of time each bird appears. (1 line = 1 appearance) Loop over your dict. to print each species and the # of times it was seen in the file once all lines have been counted. return dictionary containing count the filename opens this: blackbird canary hummingbird canary hummingbird canary...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT