CS 115 Homework 11 Individual (Lists, Sentinel Logic)
65 Points
Due by Friday April 23, midnight
Submit your individual file with the Canvas link.
Educational goals of this lab - verify that every student can
- create and use lists
- use string methods
- write sentinel logic
Preparations:
Watch these videos
(45 points) Individual Problem: Payroll for ACME
Problem Description
- ACME Inc. needs to calculate their payroll.
Each line of input will have an employee (first) name, number of hours worked and
hourly rate. There may be zero or more employees.
The data for one employee will all be on one line.
The data will be delimited by commas.
- Your program needs to input each line of data, and calculate the
paycheck for the employee.
- The paycheck is just the number of hours worked times the hourly rate.
- The input halts when the user presses just Enter. Remember input
returns an empty string in that case.
- After the data is all processed, report the total payroll (total of
all paychecks).
- Then print out the list of employees and their paychecks, sorted by
employee name, ascending.
Sample run 1
Payroll for ACME
Press Enter to stop
Enter name, comma, hours worked, comma, hourly rate: Joe, 30, 5.50
Enter name, comma, hours worked, comma, hourly rate: Bill, 40.0,10
Enter name, comma, hours worked, comma, hourly rate: Sam,35, 12.50
Enter name, comma, hours worked, comma, hourly rate:
Total Payroll $1002.50
Employees with Paychecks
Bill $400.00
Joe $165.00
Sam $437.50
By the end of the program the list would have have been:
[['Bill', 400.0], ['Joe', 165.0], ['Sam', 437.50]]
Sample run 2 (no employees)
Payroll for ACME
Press Enter to stop
Enter name, comma, hours worked, comma, hourly rate:
Total payroll $0.00
(10 points) Test Cases
Calculate the paycheck amount for each employee, and the total payroll.
Place at the bottom of the file as usual.
Description | Employee, hours, rate | Paychecks, Total Payroll |
Normal case | Mary, 11.50,10 Johnny,15,15.0 Carl, 20, 20 Enter | ________A._________ |
No employees | Enter | _______B._______ |
(15 points) Documentation / Design
- Of course give the three P's for both the main function and
the other function described in the implementation below.
- Document each control structure that you use.
- Do NOT just repeat code in the comment; that is a waste of time for you and us.
(45 points) Implementation
- You will need a split method, think about the delimiter. Use "," NOT ", ".
That is, don't use a string that has a comma followed by a space.
There IS a difference! You want your code to work even if the human
forgets the space after the comma or puts two spaces!
- Your program builds a list where each row
contains [the employee name (str), the amount of their paycheck (float)].
Note: There are plenty of other ways to solve this problem.
This list structure is the one that I want.
- You can expect that both numbers could be floats.
- You will need to start with an empty list and accumulate entries
onto it. Append or concatenation will be useful here.
A bit of syntax
A bit of notation that may help.
If you have a 2-dimensional list that looks like
[[3,9], [4,2]] called lst, referring to lst[0] gives you a one-dimensional
list [3,9]. If you want to refer to one of the elements of THAT list
you can use lst[0][0] (which would be 3) or lst[0][1] (which would be 9).
Subscripts are written right next to each other.
Some people like another way: they would say item = lst[0], which
makes item the first element of lst, which is [3,9]. Then they use item[0] (which is 3) and item[1] (which is 9).
Either way works!
- You can assume the data input is correctly formatted. If a user
forgets a comma, the program should crash.
- Format the money amounts with 2 decimals.
- (12 points) A function definition
- You must ALSO write a small function which accepts the list of
raw data, coming from one line of user input, and calculates the amount
of pay the employee gets. It has one parameter, the list that
contains a name, hours worked, and hourly rate, ALL as strings.
It returns one number, the (numeric) amount the employee gets paid.
It does NOT change the list that is the parameter.
This function can be literally 2 lines long.
The function must have the usual 3 P's documentation.
You must call the function inside the main loop where the big list is created.
- Submit your finished program with the Canvas link.