Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
ASSIGNMENT 3
COMP202
Please read the entire PDF before starting. You must do this assignment individually.
Question 1: 35 points
Question 2: 65 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, which allows the TAs to provide better
feedback and not waste time on administrative details. Plus, if the TA is in a good mood while he or she is
grading, then that increases the chance of them giving out partial marks. :)
Up to 30% can be removed for bad indentation of your code as well as omitting comments, or poor coding
structure.
To get full marks, you must:
• Follow all directions below.
– In particular, make sure that all file names and function names are spelled exactly as described
in this document. Otherwise, a 50% penalty will be applied.
• Make sure that your code runs.
– Code with errors will receive a very low mark.
• Write your name and student ID as 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 work.
– 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.
1
Part 1 (0 points): Warm-up
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)
Write a function same_elements which takes as input a two dimensional list and returns true if all the
elements in each sublist are the same, false otherwise. For example,
>>> same_elements([[1, 1, 1], ['a', 'a'], [6]])
True
>>> same_elements([[1, 6, 1], [6, 6]])
False
Warm-up Question 2 (0 points)
Write a function flatten_list which takes as input a two dimensional list and returns a one dimensional
list containing all the elements of the sublists. For example,
>>> flatten_list([[1, 2], [3], ['a', 'b', 'c']])
[1, 2, 3, 'a', 'b', 'c']
>>> flatten_list([[]])
[]
Warm-up Question 3 (0 points)
Complete the case study on multidimensional lists presented in class on Friday, March 12. You can find
the instructions on myCourses (Content > Live sessions > Case Studies > Case Study 2).
Warm-up Question 4 (0 points)
Write a function get_most_valuable_key which takes as input a dictionary mapping strings to integers.
The function returns the key which is mapped to the largest value. For example,
>>> get_most_valuable_key({'a' : 3, 'b': 6, 'g': 0, 'q': 9})
'q'
Warm-up Question 5 (0 points)
Write a function add_dicts which takes as input two dictionaries mapping strings to integers. The
function returns a dictionary which is a result of merging the two input dictionary, that is if a key is in
both dictionaries then add the two values.
>>> d1 = {'a':5, 'b':2, 'd':-1}
>>> d2 = {'a':7, 'b':1, 'c':5}
>>> add_dicts(d1, d2) == {'a': 12, 'b': 3, 'c': 5, 'd': -1}
True
Warm-up Question 6 (0 points)
Create a function reverse_dict which takes as input a dictionary d and returns a dictionary where the
values in d are now keys mapping to a list containing all the keys in d which mapped to them. For
example,
>>> a = reverse_dict({'a': 3, 'b': 2, 'c': 3, 'd': 5, 'e': 2, 'f': 3})
>>> a == {3 : ['a', 'c', 'f'], 2 : ['b', 'e'], 5 : ['d']}
True
Note that the order of the elements in the list might not be the same, and that’s ok!
Page 2
Part 2
The questions in this part of the assignment will be graded.
The main learning objectives for this assignment are:
• Apply what you have learned about one-dimensional and multi-dimensional lists.
• Apply what you have learned about dictionaries.
• Understand how to test functions that return dictionaries.
• Solidify your understanding of working with loops and strings.
• Understand how to write a docstring and use doctest when working with dictionaries.
• Learn to identify when using the function enumerate can help you write a cleaner code.
• Apply what you have learned about file IO and string manipulation.
Note that the assignment is designed for you to be practicing what you have learned in the
videos up to and including Week 11.1. For this reason, you are NOT allowed to use anything
seen after Week 11.1 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 3 examples of calls to the function (except when the function has only one possible output,
in which case you can provide only one example). 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 check 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 may use inputs not seen in
the examples.
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 does not conform to this restriction will
automatically fail the tests on codePost and 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. Please review what you have learned in video 5.2 if you’d like to add code to your modules
which executes only when you run your files.
Page 3
Question 1: Game of Life (35 points)
For this question, you will have to write a Python program that implements a simple version of Conway’s
Game of Life
This is a zero-player game, which means that it is completely determined by the initial state provided.
For our purpose, we will represent the universe of the Game of Life as a finite rectangular two-dimensional
list (i.e., a matrix). Each element denotes a cell in the universe: if the cell is alive the value of the element
is 1, if the cell is dead the value of the element is 0. Whether a cell is alive or dead in the next generation
of the universe depends on its relation with its neighboring cells (the cells that are horizontally, vertically,
or diagonally adjacent to it). The next generation of a cell is determined by the following rules:
• Any live cell with fewer than two live neighbors dies, as if caused by under-population.
• Any live cell with two or three live neighbors lives on to the next generation.
• Any live cell with more than three live neighbors dies, as if caused by over-population.
• Any dead cell with exactly three live neighbors becomes a live cell, as if caused by reproduction.
Otherwise the cell will remain dead.
In this question, you will implement a function that will generate a given number of strings representing
the corresponding number of generations of a specified universe.
To complete this task, you will need to implement all the functions listed below. All the code for this
question must be placed in a file named game_of_life.py. Note that you are free to write more functions
if they help the design or readability of your code.
Attention: please note that none of the functions listed below should modify any of their input objects.