CSC401-Spring2021-Final CSC 401 Spring 2021 Introduction to Programming Final Exam Logistics 1. Download the program template and other materials from D2L. The files to download are: a....

see files


CSC401-Spring2021-Final CSC 401 Spring 2021 Introduction to Programming Final Exam Logistics 1. Download the program template and other materials from D2L. The files to download are: a. CSC401-Spring2021-Final.pdf (this file) b. csc401-final.zip which contains i. csc401-final.py (template file) ii. numbers.txt (input to one of the problems) iii. no-nums.txt (more input) 2. Extract files from the zip. Make sure all files remain in the same folder 3. Perform the exam by completing the functions that are sketched out in the template. The template contains the first line of each function required for the first 3 problems. Your assignment is to make each function behave as defined below. Problems 4 to 8 are on object oriented programming, so the starting point is just the beginning of some of the classes. 4. Be careful to not relocate the test strings. They belong directly after the first line of the function: def my_function(): ‘’’ test string more test ‘’’ # your code starts here 5. If you cannot solve the entire problem each time, find some part of the problem that you can solve. I can give you partial credit for a partial answer but a skipped question gets zero points. 6. None of these problems require any imports. You are not permitted to add any imports. 7. None of these problems require the use of input() 8. When you have completed the exam, upload your completed .py file to the final submission folder that will be provided on D2L. Pay close attention to the time. Late exams will not be accepted. 1. identical_values(top) (20 points) a. goal : use two loops to generate and print the simple equations shown below: 1 + 1 = 2 2 + 2 = 4 3 + 3 = 6 4 + 4 = 8 5 + 5 = 10 The value before the plus sign is supplied by 1 loop while the value after the plus is supplied by the other loop. Notice we only print when the loop values equal each other. The parameter top tells you how many equations to print. b. parameters: i. top : the ending value for the exercise. Produce output for this value and then stop c. hints : i. Notice that 0 + 0 is not printed ii. Pay very close attention to the format. “1+1=2” will not match “1 + 1 = 2” iii. While you could clearly get the same output with 1 loop, the programming problem is intended to show that you know how to work with nested loops, so use 2 loops. d. example usage: >>> identical_values(5) 1 + 1 = 2 2 + 2 = 4 3 + 3 = 6 4 + 4 = 8 5 + 5 = 10 >>> identical_values(0) >>> identical_values(1) 1 + 1 = 2 2. sum_file(file_name) (20 points) a. goal: Read a file of numbers and add them up. Return the result. i. Some of the values in the file are not legitimate numbers so you have to guard against that. Invalid values should be skipped. ii. Do not let the function end because of bad data. Just print ‘skipping ‘ + the bad value, and move on. iii. If the file contains no valid numbers, the function still returns 0. iv. If the file_name refers to a file that does not exist or other file oriented failures occur, print “Unable to open or read file: ___” and fill in the file name. b. parameters i. file_name : this is the name of the file to process. One or more files will be provided with the test but you should use additional files in your testing c. hints i. Do not depend on the numbers being all ints or all floats ii. It is acceptable for the result value to be a float or an int iii. Make sure your source file is in the same directory as the data files. iv. The “skipping …” message is printed but the result of the function is returned d. example usage: >>> cnt = sum_file('numbers.txt') skipping a skipping skipping cat >>> cnt 220.2699 >>> cnt = sum_file('no-nums.txt') skipping a skipping b skipping c skipping d skipping e >>> cnt 0 >>> cnt = sum_file('no such file') Unable to open or read file: no such file 3. write_lists(file_name, listOne, listTwo) (20 points) a. goal: Given 2 lists of values, write them to a given file name, so that listOne is written as column 1 and listTwo is written as column 2. You may not use the zip function for this task. Write a space between each value on any given line b. parameters i. file_name : the name of the file to write to. ii. listOne : 1 of 2 lists to be written to the output. This list should be the left side of the output iii. listTwo : 2 of 2 lists to be written to the output. This list should be the right side of the output c. hints i. For the simple case, you can count on the lists being the same length but consider the extra credit option at the end of this problem ii. The data in the lists might be a variety of data types, don’t assume they are all numbers or all strings or all the same as each other. iii. The only output from this problem is the file that it produces. You need to examine the file for yourself, to verify you are getting the values and format specified above d. example usage >>> write_lists(‘my_nums.txt’, [1,2,3,4,5],[‘a’,’b’,’c’,’d’,’e’]) (the contents of my_nums.txt would be) 1 a 2 b 3 c 4 d 5 e e. extra credit (5 points) Improve this function so that it can handle lists that are not the same length. i. the first list could be longer than the other ii. the first list could be shorter than the other If the shorter list is the first one, provide space for the missing value so that the right column still lines up: 1 a 2 b 3 c d e Be careful though. Don’t break your original answer! Also, do problems 4 - 8 first. Overview The following sets of tasks produce the classes of a simulation of a public library. The essential classes are the Library class and the Book class. An instance of Library contains Books. The basic operations to be implemented are: ● create a Book class ● create a Library that contains Books ● support borrow and return operations on the Library The details of these options are given below. Please notice that no user interface should be built for these tasks. Item 10 below involves writing tests for these classes but even here no user interface should be built. Read all of the tasks below before you start coding. This will give you a better understanding of how all the parts fit together. Tips: - Don’t forget the self parameter - Details 4. Book class (10 points) Define class to represent a book in a library. A Book has attributes of title, author and lending_status. Provide a constructor with these parameters. There are no defaults for title and author but lending_status should default to “in” (meaning the book is in the library. The other possible status is “out”) 5. Library class (10 points) Define a class called Library. a. Its constructor has no parameters. b. Its constructor should create an instance variable that is a container to store books in. Books will be retrieved by title, so consider which container type stores things by name the best c. The constructor should add several books to the container so that the library starts out stocked with books. To keep things simple, there is only 1 copy of each book in this library 6. borrow(self, book_name) (10 points) goal: define a borrow method for Library. The method, if successful, returns the Book matching the given book_name. There are rules however: a. a book cannot be borrowed if it is already out. Check the lending_status. If borrow() is called for a book that is already out, raise a ValueError, with a meaningful error message b. The library might not have a book by the given name. When this happens, raise a KeyError with a meaningful error message. Don’t depend on the container itself to provide a good message c. when a book is successfully borrowed, set lending_status to “out” before the method returns the book 7. return_it(self, a_book) (5 points) goal: give a Book back to the library. Notice the function is not named return(). We need to avoid the confusion between the keyword return and the name of this function. The only thing that must be done in this method is, set the lending_status to “in”. The body of this method should be very short. It could be only 1 line. Nothing is returned or printed 8. library_test() (5 points) goal: Run the library test and make any necessary corrections. Once you have implemented the Book and Library classes, enable the libary_test() function by doing the following: a. Find the string within the test that says “REPLACE-WITH-YOUR-OWN-TITLE” and replace it with a title of a book that exists in your Library, which you created during step 5.c above. For example, if you added “Moby Dick” as a book to your library, change the the code to: test_book_title = 'Moby Dick' b. Uncomment the commented out part of the test Run your code. If libary_test() does not pass, make corrections within Bank and/or Library. # CSC-401 Final # Spring 2021 # Author: # # Don't forget to delete 'pass' from each function # Don't forget to provide function and class descriptions # def identical_values(top): ''' Test >>> identical_values(5) 1 + 1 = 2 2 + 2 = 4 3 + 3 = 6 4 + 4 = 8 5 + 5 = 10 >>> identical_values(0) >>> identical_values(1) 1 + 1 = 2 ''' pass def sum_file(file_name): ''' Test >>> cnt = sum_file('numbers.txt') skipping a skipping skipping cat >>> cnt 220.2699
Jun 12, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here