Question

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

In python

def lambda_1(filename):
# Complete this function to read grades from `filename` and find the minimum
# student test averages. File has student_name, test1_score, test2_score,
# test3_score, test4_score, test5_score. This function must use a lambda
# function and use the min() function to find the student with the minimum
# test average. The input to the min function should be
# a list of lines. Ex. ['student1,33,34,35,36,45', 'student2,33,34,35,36,75']
# input filename
# output: (lambda_func, line_with_min_student) -- example (lambda_func, 'student1,33,34,35,36,45')

# YOUR CODE HERE
raise NotImplementedError()

This is an example of the file

student1,94,88,86,78,94

student2,88,78,80,84,80

student3,84,97,85,73,76

student4,72,99,72,86,82

student5,71,92,71,77,91

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The following program does the required tasks. Read the comments to understand.

#Lambda function to be implemented
def lambda_1(filename):
    f1 = open(filename,"r")
    #list of lines
    students = f1.readlines()
    #A lambda function to calculate average for each line
    lambda_func = lambda x : sum(list(map(int,x.split(",")[1:])))/5
    #min function using the lambda function to find the minimum value
    st = min(students, key=lambda_func)
    #The output to be returned
    return (lambda_func, st)

print(lambda_1("inp.txt"))
    

inp.txt:

student1,94,88,86,78,94
student2,88,78,80,84,80
student3,84,97,85,73,76
student4,72,99,72,86,82
student5,71,92,71,77,91

Output:

(<function lambda_1.<locals>.<lambda> at 0x00000254EDD47790>, 'student5,71,92,71,77,91')

The min function returns the minimum element for which the value of lambda(average) is minimum.

Add a comment
Know the answer?
Add Answer to:
In python def lambda_1(filename): # Complete this function to read grades from `filename` and find the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Python: LoadFile is a function that takes in a string (a filename) and then returns...

    In Python: LoadFile is a function that takes in a string (a filename) and then returns a list. The list is the contents of the file, where each element is a list of data from the file. Here's an example of using this function. The input file had four lines of text. >>> lines = LoadFile("test.txt") >>> print("OUTPUT", lines) OUTPUT ["Hello there", "I am a test file", "please load me in and print me out", "Thanks"]

  • (IN PYTHON) You are to develop a Python program that will read the file Grades-1.txt that...

    (IN PYTHON) You are to develop a Python program that will read the file Grades-1.txt that you have been provided on Canvas. That file has a name and 3 grades on each line. You are to ask the user for the name of the file and how many grades there are per line. In this case, it is 3 but your program should work if the files was changed to have more or fewer grades per line. The name and...

  • ### Question in python code, write the code on the following program: def minmaxgrades(grades, names): """...

    ### Question in python code, write the code on the following program: def minmaxgrades(grades, names): """ Computes the minimum and maximujm grades from grades and creates a list of two student names: first name is the student with minimum grade and second name is the student with the maximum grade. grades: list of non-negative integers names: list of strings Returns: list of two strings """ def main(): """ Test cases for minmaxgrades() function """ input1 = [80, 75, 90, 85,...

  • python Write the function getSpamLines(filename) that read the filename text file and look for lines of the form &#...

    python Write the function getSpamLines(filename) that read the filename text file and look for lines of the form 'SPAM-Confidence: float number'. When you encounter a line that starts with "SPAM-Confidence:" extract the floating-point number on the line. The function returns the count of the lines where the confidence value is greater than 0.8. For example, if 'spam.txt' contains the following lines along with normal text getSpamLines('spam.txt') returns 2. SPAM-Confidence: 0.8945 Mary had a little lamb. SPAM-Confidence: 0.8275 SPAM-Confidence: 0.7507 The...

  • Use python Start: def main(): gradeList = []    fileName = getFile() gradeList = getData(fileName) mean...

    Use python Start: def main(): gradeList = []    fileName = getFile() gradeList = getData(fileName) mean = calculateMean(gradeList) sd = calculateSD(mean, gradeList) displayHeadings(gradeList, mean, sd) curveGrades(mean, sd, gradeList)       if __name__ == "__main__": main() For this program, you will create a grade curving program by reading grades from a file, calculating the mean and standard deviation. The mean is the average value of the grades and the standard deviation measures the spread or dispersal of the numbers from the...

  • python 3 HW11.2. Read a file and print its lines The function takes a single string...

    python 3 HW11.2. Read a file and print its lines The function takes a single string parameter, which is the name of a file. Complete the function to open the file for reading, read the lines of the file, and print out each line. The function takes a single string parameter, which function to open the file for reading, read the lines student.py def print_lines of file (filename): 1

  • Consider python please for this problem: I did a python program to read the first 20 lines from a...

    Consider python please for this problem: I did a python program to read the first 20 lines from a file and here is the code holder = 20 lines = 3 file = "123456.pssm" _file = ".txt" while 1:         out_file = input("[OUTPUT] - Please enter the name of file: ") + _file         if (not _file):         print("Filename not specified - Please Try Again\n")         continue     break with open(_file, "a") as e:     with open(file, "r") as f:         for num, line in enumerate(f, 1):                        ...

  • in python Complete the function read_r_line(fname, ln) to take a filename and a line number and...

    in python Complete the function read_r_line(fname, ln) to take a filename and a line number and return the contents of that line number from the file as a string. Hint: The code from Files03 will be helpful. Example: read_r_line('myf.txt', 5) will return 'I like peanut'

  • QUESTION17 The following Python script aims to read lines from a text file and add them...

    QUESTION17 The following Python script aims to read lines from a text file and add them to a list Marlout oft. 00 Example P Flag question If the text file contains the following lines wordt wordz words the list will be ['word, word2, Words' after the execution of the program Question: Rearrange the following statements on the correct (logical) form Note: The-indicates the indentation #--> Program infile=onen/fileName --infile = open(fileName, 'r') . . -try: --for line in infile: --infile =...

  • Complete the Python function read_third_line() to read the third line of the file, my2nd.txt. Return the...

    Complete the Python function read_third_line() to read the third line of the file, my2nd.txt. Return the third line as the output of function read_third_line(). Should start with def read_third_line(fname):

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT