Assignment 5
Using Loops and Lists
Submit before 11:30 PM Friday October 20
Overview
You will write four functions that practice the use of loops and lists. You will also read and write to files.
Read or review section 4.3 for reading and writing to files. Sections 5.1 and 5.2 provide a good review of if statements and loops, including how to use them together.
Pay close attention to whether you intend to loop by-value or by-index!
Python Functions and Scripts
Write and test Python functions for each of the following. When writing a function, include a docstring (see end of section 3.3 for explanation).
- Write a function payroll that takes two string parameters, the first of which represents the name of an input file and the second of which represents the name of an output file. The input file contains lines with the following format: employee_id pay_rate hours where employee_id is an integer, pay_rate is a floating point value representing the pay rate per hour, and hours is an integer representing a number of hours worked. There will be some whitespace between the two values, although you may not make any assumptions about how much or what type. The function reads the input file line by line, computing the total pay (i.e. the product of the hours times the pay_rate -- we are not assuming overtime is computed for any of the values) and writing the employee_id with each amount to a newline in the output file. If the file is empty, the function should not write any numbers to the output file. Note that the strings read from the file will need to be evaluated before you can use them for computations. If the output file exists, it should be cleared as it is opened. Don't forget to close both the input and output files. Here's a zip folder of an example file and a demo Python script for reading it.
- Write a function extractInt that takes a string as a parameter and returns an integer constructed out of the digits that appear in the string. The digits in the integer should appear in the same order as the digits in the string. If the string does not contain any digits or an empty string is provided as a parameter, the value 0 should be return.
- Write a function n_letter that takes a list lst of strings and a non-negative (>= 0) integer n as a parameter and returns a new list containing all of the strings in lst that have length n. If n is negative or lst is empty the function should return an empty list. The original list should not be modified. Hint: start with a new empty list and add qualifying strings to it.
- Implement a function valueDiff that takes a list of integers as one parameter and one integer as a second parameter. The function returns True if the difference between every adjacent pair of integers in the list produces a value equal to the second parameter. If there is any pair of adjacent integers in the list whose difference does not produce the second parameters as the result when they are subtracted (ignoring the sign), the function should return False. If the function is given a list with one element, an empty list, or a negative value for the difference, the function should return False. Hint: Use an indexed loop (e.g. one that directly manipulates indices using the range() function)!
Sample Runs
Below are example runs of the functions:
>>> payroll("records.txt", "pay.txt")
>>> payContents = open("pay.txt").read()
>>> print(payContents)
1 1035.0
2 454.0
3 558.0
>>> extractInt("24 hours and 7 days a week he 8")
2478
>>> extractInt("No digits here!")
0
>>> n_letter(["cat", "lion", "zebra", "horse", "ant", "bear"], 4)
['lion', 'bear']
>>> n_letter(["cat", "lion", "zebra", "horse", "ant", "bear"], -4)
[]
>>> n_letter([], 4)
[]
>>> value_diff([3, 6, 9, 6, 9], 3)
True
>>> value_diff([3, 6, 8, 6, 9], 3)
False
>>> value_diff([3, 6, 9, 6, 9], -3)
False
>>> value_diff([3], 3)
False
>>> value_diff([], 3)
False
Deliverables
Create a text file called assn5.txt that contains the following:
- A statement that summarizes your completion of the assignment. It should contain
the following information:
- Any help or resources that you used in completing the assignment.
- A summary of your experience possibly including your approach, difficulties and time you spent.
- For each of your Python scripts or functions:
- A listing of the code
- Running examples that demonstrate that your code works correctly
Submit your assn5.txt file to D2L.