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.
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...
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...
Create a new Python program (you choose the filename) that contains a main function and another...
Create a new Python program (you choose the filename) that contains a main function and another function named change_list. Refer to the SAMPLE OUTPUT after reading the following requirements. In the main function: create an empty list. use a for loop to add 12 random integers, all ranging from 50 to 100, to the list. use second for loop to iterate over the list and display all elements on one line separated by a single space. display the 4th element,...
Use python. redact_file: This function takes a string filename. It writes a new file that has...
Use python. redact_file: This function takes a string filename. It writes a new file that has the same contents as the argument, except that all of the phone numbers are redacted. Assume that the filename has only one period in it. The new filename is the same as the original with '_redacted' added before the period. For instance, if the input filename were 'myfile.txt', the output filename would be 'myfile_redacted.txt'. Make sure you close your output file.
In Python Write a function to read a Sudoku board from an input string. The input...
In Python Write a function to read a Sudoku board from an input string. The input string must be exactly 81 characters long (plus the terminating null that marks the end of the string) and contains digits and dots (the `.` character represents an unmarked position). The input contains all 9 rows packed together. For example, a Sudoku board that looks like this: ``` ..7 ... ... 6.4 ... ..3 ... .54 ..2 ... .4. ... 9.. ... ..5 385...
Coding in python question def generate_project_data_files(expected_grade_file_path, std_dev, num_projects, folder_path): """ For given student expected grades, generate...
Coding in python question def generate_project_data_files(expected_grade_file_path, std_dev, num_projects, folder_path): """ For given student expected grades, generate the grades as described in generate_assignment_data given std_dev. For this method, you will generate multiple files for each project. For example, if num_projects = 4, then you should generate four files according to the following naming convention: "P_0.csv" ... "P_3.csv" . The files should be written in the folder defined by folder_path. For example, given num_projects = 1 and folder_path="data", you should create one...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT