Here is it
COMP 202 Winter 2022 Assignment 4 Due: Tuesday, April 12th, 11:59 p.m. Please read the entire PDF before starting. You must do this assignment individually. Part 1: 20 points Part 2: 40 points Part 3: 40 points Bonus: 8 points 100 points total It is very important that you follow the directions as closely as possible. The directions, while perhaps tedious, are designed to make it as easy as possible for the TAs to mark the assignments by letting them run your assignment, in some cases through automated tests. While these tests will never be used to determine your entire grade, they speed up the process significantly, allowing the TAs to provide better feedback and not waste time on administrative details. Plus, if the TA is in a good mood while grading, then that increases the chance of them giving out partial marks. :) To get full marks, you must follow all directions below: • Make sure that all file names and function names are spelled exactly as described in this document. Otherwise, a 50% penalty per question will be applied. • Make sure that your code runs without errors. Code with errors will receive a very low mark. • Write your name and student ID in a comment at the top of all .py files you hand in. • Name your variables appropriately. The purpose of each variable should be obvious from the name. • Comment your code. A comment every line is not needed, but there should be enough comments to fully understand your program. • Avoid writing repetitive code, but rather call helper functions! You are welcome to add additional functions if you think this can increase the readability of your code. • Lines of code should NOT require the TA to scroll horizontally to read the whole thing. • Vertical spacing is also important when writing code. Separate each block of code (also within a function) with an empty line. • Up to 30% can be removed for bad indentation of your code, omission of comments, and/or poor coding style (as discussed in class). Hints & tips • Start early. Programming projects always take more time than you estimate! • Do not wait until the last minute to submit your code. Submit early and often—a good rule of thumb is to submit every time you finish writing and testing a function. • Write your code incrementally. Don’t try to write everything at once. That never works well. Start off with something small and make sure that it works, then add to it gradually, making sure that it works every step of the way. • Read these instructions and make sure you understand them thoroughly before you start. Ask questions if anything is unclear! • Seek help when you get stuck! Check our discussion board first to see if your question has already been asked and answered. Ask your question on the discussion board if it hasn’t been asked already. Talk to your TA during office hours if you are having difficulties with programming. Go to an instructor’s office hours if you need extra help with understanding a part of the course content. – At the same time, beware not to post anything on the discussion board that might give away any part of your solution—this would constitute plagiarism, and the consequences would be unpleasant for everyone involved. If you cannot think of a way to ask your question without giving away part of your solution, then please drop by our office hours. • If you come to see us in office hours, please do not ask “Here is my program. What’s wrong with it?” We expect you to at least make an effort to start to debug your own code, a skill which you are meant to learn as part of this course. And as you will discover for yourself, reading through someone else’s code is a difficult process—we just don’t have the time to read through and understand even a fraction of everyone’s code in detail. – However, if you show us the work that you’ve done to narrow down the problem to a specific section of the code, why you think it doesn’t work, and what you’ve tried to fix it, it will be much easier to provide you with the specific help you require and we will be happy to do so. Revisions April 1: • Fixed some typos in some function names. • Fixed the example for most_contacted_student function. • Deleted the extra spaces from all_students.json. Make sure to download it again. • Added a clarification for dealing with empty lists in build_report method. • Deleted the example of the FileNotFoundError from load_file function – do not consider the case when this error happens, because then there is no need to close the file as it hasn’t been opened in the first place. • No need to deep copy the class attribute students in the class ContactTracker. • A disclaimer is added for ContactTracker class. • Assume no cycles can happen for min_distance_from_patient_zeros. The description is updated. • Default value for students attribute in the class ContactTracker has been added. • A change in the instructions of exception handling in load_file and write_in_file. Page 2 Warm-up questions (0 points) Do NOT submit this part, as it will not be graded. However, doing these exercises might help you to do the second part of the assignment, which will be graded. If you have difficulties with the questions of Part 1, then we suggest that you consult the TAs during their office hours; they can help you and work with you through the warm-up questions. You are responsible for knowing all of the material in these questions. Warm-up Question 1 (0 points) In the bookstore.py file created in the lecture videos, add a function called create_bookstore. The function takes as input a string indicating the name of the bookstore, and a list of strings each containing information about a book. The data related to a book is separated by tabs. The function should create a list of objects of type Book and then uses this list and the name to create and return a Bookstore object. Warm-up Question 2 (0 points) In the same module, add a function called create_bookstore_from_file. The function takes as input a filename containing information about all the books in this bookstore. Reading the file line by line, the function creates a list of objects of type Book. It then uses this list and the name of the file (without its extension) to create and return a bookstore. To test the function use the file 'City_Lights.txt'. Warm-up Question 3 (0 points) In the Bookstore class, add a method called plot_by_genre. The method should create a bar plot of all the books in the bookstore by genre. Label the x axis with ‘Genres’, y axis with ‘Number of Books’, and title the graph with ‘
- Books by genre’. The plot should be saved in a file named ‘_by_genre.png’. Test the method with the bookstore obtained by the previous function. Page 3 Assignment 4 The questions in this part of the assignment will be graded. The main learning objectives for this assignment to apply all the concepts that we have learned during the semester: • Work with (nested) lists and (nested) dictionaries using nested loops. • Understand how to test functions that return dictionaries. • Work with strings. • Understand how to write a docstring and use doctest. • Apply what you have learned about file IO. • Apply what you learned about about exception handling, and try, except, finally blocks. • Apply what you have learned about OOP – classes, methods, constructors, attributes, objects etc. Note that this assignment is designed for you to be practicing what you have learned in the videos up to and including Lecture 40 (OOP 5: Class and static methods). For this reason, you are NOT allowed to use anything seen after Lecture 40 or not seen in class at all. You will be heavily penalized if you do so. For full marks, in addition to the points listed on page 1, make sure to add the appropriate documentation string (docstring) to all the functions you write. The docstring must contain the following: • The type contract of the function. • A description of what the function is expected to do. • At least three (3) examples of calls to the function. You are allowed to use at most one example per function from this PDF. Examples For each question, we provide several examples of how your code should behave. All examples are given as if you were to call the functions from the shell. When you upload your code to codePost, some of these examples will be run automatically to test that your code outputs the same as given in the example. However, it is your responsibility to make sure your code/functions work for any inputs, not just the ones shown in the examples. When the time comes to grade your assignment, we will run additional, private tests that will use inputs not seen in the examples. You should make sure that your functions work for all the different possible scenarios. Also, when testing your code, know that mindlessly plugging in various different inputs is not enough—it’s not the quantity of tests that matters, it’s having tests that cover all of the possible scenarios, and that requires thinking about possible scenarios. Furthermore, please note that your code files should not contain any function calls in the main body of the program (i.e., outside of any functions). Code that contains function calls in the main body will automatically fail the tests on codePost and thus be heavily penalized. It is OK to place function calls in the main body of your code for testing purposes, but if you do so, make certain that you remove them before submitting. You can also test your functions by calling them from the shell. Please review what you have learned in Lecture 14 (Modules) if you’d like to add code to your modules which executes only when you run your files. Safe Assumptions For all questions on this assignment, you can safely assume that the type of the inputs (both to the functions and those provided to the program by the user) will always be correct. For example, if a function takes as Page 4 input a string, you can assume that a string will always be provided. At times you will be required to do some input validation, but this requirement will always be clearly stated. Otherwise, your functions should work with any possible input that respect the function’s description. For example, if the description says that the function takes as input a positive integer, then it should work with all integers greater than 0. If it mentions an integer,