Final Exam Program 2 Due: 11:59 PM on 2020-05-14

This program is the second of two programming problems on the final exam; it will test your use of functions, files, lists, and loops, as well as proper documentation and style.

You must complete this exercise on your own, without the help of others. You may use the textbooks, your notes, your previous assignments, the notes and examples on our course site and the Python library documentation. Use of any other source, such as Google, is not permitted. You may not work with, discuss, or in any way collaborate with anyone else. You may only ask the tutors or ASI for help with hardware problems or difficulties submitting your program.

You are encouraged to reuse useful code from your assignments or our class examples. Partial credit will be awarded, so if you can’t solve the whole problem, get as far as you can. If you are stuck, contact me. While I cannot help you solve the programming problem itself, I may be able to suggest general problem-solving strategies, help with conceptual difficulties about Python, and/or direct you to relevant examples from class.

The use of imported modules is not permitted.

Calculate your grade

For this programming problem you will implement functions that calculate an estimate of your final grade in CS 150.

Your code will open a text file, read in all the data, use that data to calculate a score out of 100, and then translate that score into a letter grade.

Task 1: function read_scores to get data from a file

You are given a text file with information about all assignments in this course. Modify the text file so that it contains all the scores you earned. Each line of the file has information about a different assignment/assessment. The comma-separated data on a line provides: the assignment name, its weight, the max possible score, and your own score. For example, the line

lab8,3.89,25,23

would denote that Lab 8 contributes 3.89/100 towards your final grade, and you earned a score of 23/25 on that assignment.

Write a function read_scores that takes a filename as its input parameter, and returns a tuple (weights, max_scores, my_scores) where weights is a list of all assignment weights (the number in position 1 on each line), max_scores is a list of the maximum scores (the number in position 2 on each line), and my_scores is a list of your own scores (the number in position 3 on each line).

For example:

>>> (weights, max_scores, my_scores) = read_scores('scores.txt')
>>> max_scores
[10.0, 10.0, 10.0, 10.0, 3.0, 3.0, 3.0, 1.0, 3.0, 3.0, 3.0, 3.0, 2.0, 25.0, 30.0, 25.0, 25.0, 25.0, 25.0, 25.0, 25.0, 25.0, 100.0, 100.0]
>>> 

Sample files scores0.txt, scores74.txt, and scores100.txt are provided. You are encouraged to make your own copy called scores.txt that you fill with your own scores. Make sure that your code works for any properly formatted data file (such as the ones provided) and that no filenames are hard-coded in your program when you submit it.

Task 2: function final_score to calculate a final score

Write a function final_score that takes a filename as its parameter, and returns an integer that is the final weighted grade given all scores in the given file.

For example:

>>> final_score('scores74.txt')
74

Given a sample file small.txt containing the data

lab1,25,30,27
midterm,35,100,80
final,40,100,95

The function call final_score('small.txt') would return the score 89 by computing 25 * (27/30) + 35 * (80/100) + 40 * (95/100) = 88.5 and then rounding that floating point value to return the integer 89.

It is not required, but if you wish you can write a version of final_score that drops the lowest quiz grade, in order to get a better estimate of your actual final score.

Task 3: function letter_grade to assign a letter grade

Write a function letter_grade that assigns a letter grade to a number according to the following rubric:

 A  : 90 - 100
 A- : 86 - 89
 B+ : 83 - 85
 B  : 80 - 82
 B- : 76 - 79
 C+ : 73 - 75
 C  : 70 - 72
 C- : 66 - 69
 D  : 60 - 65
 F  : < 60

For example:

>>> avg = final_score('scores74.txt')
>>> avg
74
>>> letter_grade(avg)
'C+'

Specifications

  1. Function read_file takes a filename as a parameter, and returns a tuple of lists (weights, max_scores, my_scores)

  2. Function final_score takes a filename as a parameter, and returns an integer that is the final weighted grade given all scores in the given file. The function should compute a floating point number and then round to the nearest integer to return. For example, the computed average 73.42 would result in the integer 73 returned, while the average 73.5 would result in the integer 74 returned.

  3. Function letter_grade takes an integer as a parameter and returns a letter grade among the options “A, A-, B+, B, B-, C+, C, C-, D, F” as outlined in the rubric above.

  4. No main program is required; and when imported, your program should do nothing.

A few tips

When you write a function, test it as a stand-alone function from the Python shell.

If variable s holds the comma-separated string 'a,b,c' then s.split(',') evaluates to a list containing the separate strings ['a', 'b', 'c'].

For example:

>>> s = 'a,b,c'
>>> s.split(',')
['a', 'b', 'c']

You may not use any modules to deal with comma-separated strings. Just use simple string methods such as we have used throughout the course.

To round a floating point number x to the nearest integer (without using any modules), you can use the expression int(x + 0.5)

When you’re done

Make sure that your program is properly documented:

And use good style:

Submit your program via Gradescope. Your program file must be named fp_scores.py. You can submit multiple times, with only the most recent submission (before the due date) graded. Note that the tests performed by Gradescope are limited. Passing all of the visible tests does not guarantee that your submission correctly satisfies all of the requirements of the assignment.

Gradescope will import your file for testing so make sure that no code executes on import.

Grading

Feature Points
read_file defined with one parameter 1
read_file functionality 5
final_score defined with one parameter 1
final_score functionality 5
letter_grade defined with one parameter 1
letter_grade functionality 5
Style 4
Documentation 3
Total 25