Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Write and test the following function: def file_head(fh, linecount) : OUTAWNY Prints first linecount lines of fh. Line numbering starts at 0. If length

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
1. Write and test the following function: def file_head(fh, linecount) : OUTAWNY Prints first linecount lines of fh. Line numbering starts at 0. If length of file is shorter than linecount, stops printing after last line of file. Use: file_head(fh, linecount) 9 Parameters : 10 fh - file to process (file handle - open for reading) 11 linecount - number of lines to print (int > 0) 12 Returns : 13 None 14 15 Add the function to a PyDev module named functions . py . Test it from to1 . py. The function does not ask for input - that is done by your test program. Test the function with any py file. Sample execution: fh = open("functions. py", "r", encoding="utf-8") file_head(fh, 5) Assignment 9 Functions Author : David Brown2. Write and test the following function: def file_integers(fh) : WNP Extracts positive integers from a file into a list of integers. OUT A Numbers are comma-delimited. Non-numeric tokens are ignored. Use: numbers = file_integers(fh) Parameters : 9 fh - file to process ( (file handle - open for reading) 10 Returns : 11 numbers - a list of integers from fh (list of int) 12 13 Add the function to a PyDev module named functions . py . Test it from t02. py. The function does not ask for input and does no printing - that is done by your test program. Sample execution: fh = open("numbers. txt", "r", encoding="utf-8") NP file_integers(fh) [10, 20, 30, 40, 50, 60, 90, 80, 70, 11, 22, 13, 14, 14, 20] Test the function with numbers.txt.3. Write and test the following function: def file_stats(fh) : VOUTAWNY Evaluates the contents of a file. Use: ucount, Icount, dcount, wcount = file_stats(fh) Parameters : fh - file to process (file handle - open for reading) Returns : ucount - The number of uppercase letters in the file (int) Icount - The number of lowercase letters in the file (int) WNES dcount - The number of digits in the file (int) wcount - The number of whitespace characters in the file (int) 14 15 Add the function to a PyDev module named functions . py . Test it from t03. py. The function does not ask for input and does no printing - that is done by your test program. Sample execution: fh = open("functions.py", "r", encoding="utf-8") file_stats(fh) (52, 2304, 44, 1512) Test the function with addresses.txt.4. Write and test the following function: 1 def flattenCmatrix): 2 """ 3 _______________________________________________________ 4 Flatten the contents of ZD list matrix. A 'flattened' list is a 5 2D list that is converted into a 1D list by rows. 6 matrix must be unchanged. 7 Use: flat = matrix_flatten(matrix): g _______________________________________________________ 9 Parameters: 10 matrix a 2D list (ZD list of int) 11 Returns: 12 flat - the flattened version of matrix (list of int) 13 14 """ Add the function to a PyDev module named functions . py. Test it from t04 . py. The function does not ask for input and does no printing - that is done by your test program. Sample execution: 1 | flatten([[1, 2, 9], [3, 7, 12], [4, 2, 01]) _____________________________________ 'I i [1: 2: 9: 3: 7r 12: 4r 2r 0]: _____________________________________ J Test the function with three different sets of parameters. 5. Write and test the following function: 1 def matrix_rotate_right(matrix): 2 """ 3 _______________________________________________________ 4 Returns a copy of a 20 matrix rotated to the right. 5 a must be unchanged. 6 Use: rotated = matrix_rotate_rightCmatrix) 7 _______________________________________________________ 8 Parameters: 9 matrix a 20 list of values (2d list of int/float) 10 Returns: 11 rotated the rotated version of matrix (ZD list of int/float) 12 13 """ Add the function to a PyDev module named functions . py. Test it from t85 . py. The function does not ask for input and does no printing - that is done by your test program. Sample execution: 1 | matrix.rotate_right([[1, 2, 9, 3], [7, 12, 4, 21]) r ____________________________________________ 'I EH7. 1], [12. 21. [4. 91. [2. 8115 I. ____________________________________________ J Displayed in a formatted way: IIEE IIEE is rotated to: Test the function with three different sets of parameters. Bonus Task Write and test the following function: def get_addresses(fh) : OUTAWNY Reads a addresses from fh into a list of addresses. Addresses are stored in fh in the form: name street city postal code 10 11 Each address in the list of addresses is a list of the form: 12 [name, street, city, postal code] 13 Use: addresses = get_addresses(fh) 14 15 Parameters : 16 fh - file to process (file handle - open for reading) 17 Returns : 18 addresses - the addresses from fh (list of str) 19 20 Add the function to a PyDev module named functions . py . Test it from t06. py. The function does not ask for input and does no printing - that is done by your test program. Sample execution (output formatted for readability): 1 fh = open("addresses. txt", "r", encoding="utf-8") N get_addresses(fh) 'David Brown' , 'King St', 'Waterloo, ON' , 'Q10 101'], 'Tasmin Brown' , 'Awesome Avenue' , 'Waterloo, ON' , 'UIF 5D4' ] Test the function with addresses.txt. This task is worth bonus marks

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Programming questions

Question

What are 3 Vs in big data analytics?

Answered: 1 week ago