Description
1 Part 1 (30 points)
For this part of the assignment, you are required to write four short programs. For this part you need to submit 4 files:
a4_Q1_xxxxxx.py, a4_Q2_xxxxxx.py, a4_Q3_xxxxxx.py and a4_Q4_xxxxxx.py .
1.1 Question 1: (5 points)
Implement a Python function named number_divisible that takes a list of integers and a integer n as input parameters
and returns the number of elements in the list that are divisible by n. Then, in the main, your program should ask the
user to input integers for the list and an integer for n, then it should call the function number_divisible, and print
the result. In this question you may assume that the user will follow your instructions and enter a sequence of integers
separated by spaces for the list and an integer for n. You can use str method .strip and .split to handle the user input.
Here is a way to ask a user for a list:
raw_input = input(“Please input a list of numbers separated by space: “).strip().split()
But now raw_input is a list of strings that look like integers so you need to create a new list that is a list of equivalent
integers.
Function call example:
2
>>> number_divisible([6, 10, 2, 3, 4, 5, 6, 0], 3)
4
An example run of the program:
Please input a list of integers separated by spaces: 1 2 3 0 5 -6 995
Please input an integer: 2
The number of elements divisible by 2 is 3
1.2 Question 2: (5 points)
A run is a sequence of consecutive repeated values. Implement a Python function called two_length_run that takes a
list of numbers as input parameter and returns True if the given list has at least one run (of length at least two), and
False otherwise. Make sure the function is efficient (i.e. it stops as soon as the answer is known). Then, in the main, your
program should ask the user to input the list, then it should call two_length_run function, and print the result. You can
obtain a list of numbers from the user as explained in Question 1.
Four examples of program runs:
Please input a list of numbers separated by space: 1 4 3 3 4
True
Please input a list of numbers separated by space: 1 2 3 3 3 4.5 6 5
True
Please input a list of numbers separated by space: 1.0 2 3.7 4 3 2
False
Please input a list of numbers separated by space: 7.7
False
Function call examples:
>>> two_length_run( [2.7, 1.0, 1.0, 0.5, 3.0, 1.0] )
True
>>>
>>> two_length_run([1.0,1])
True
1.3 Question 3: (10 points)
As mentioned, a run is a sequence of consecutive repeated values. Implement a Python function called longest_run
that takes a list of numbers and returns the length of the longest run. For example in the sequence:
2, 7, 4, 4, 2, 5, 2, 5, 10, 12, 5, 5, 5, 5, 6, 20, 1 the longest run has length 4. Then, in the main, your
program should ask the user to input the list, then it should call longest_run function, and print the result. You can
obtain a list of numbers from the user as explained in Question 1.
Five examples of program runs:
Please input a list of numbers separated by space: 1 1 2 3.0 3 3 3 3 6 5
5
Please input a list of numbers separated by space: 6 6 7 1 1 1 1 4.5 1
4
Please input a list of numbers separated by space: 6 2.4 4 8 6
1
Please input a list of numbers separated by space: 3
1
Please input a list of numbers separated by space:
0
Function call example:
longest_run([6, 6, 7, 1.0, 1.0, 1.0, 1, 4.5, 1])
4
3
1.4 Question 4: (10 points)
In the questions you are provided with starter code a4_Q4_xxxxxx.py and 5 files file1.txt, …, file5.txt. As usual
you cannot modify the given parts of the code. For this question you need to code the two missing functions clean_up
and is_rigorous What these functions should do is described in the docstrings. Some test cases for these two functions
can also be found in the docstrings. The provided program asks for the name of a file. The files your program will be
tested with will have one character per line (like bo1.txt). Then the function read_raw returns a list of characters from
the file. Here are some example runs:
RUN 1:
Enter the name of the file: file1.txt
Before clean-up:
[‘D’,’F’,’B’,’G’,’$’,’$’,’$’,’A’,’A’,’C’,’G’,’D’,’A’,’$’,’C’,’*’,’P’,’E’,’D’,’*’,’D’,’D’,’E’,’B’,’$’,’#’,’D’,’D]
After clean-up:
[‘$’, ‘$’, ‘$’, ‘$’, ‘A’, ‘A’, ‘B’, ‘B’, ‘C’, ‘C’, ‘D’, ‘D’, ‘D’, ‘D’, ‘D’, ‘D’, ‘E’, ‘E’, ‘G’, ‘G’]
This list has no * but is not rigorous and it has 20 characters.
RUN 2:
Enter the name of the file: file2.txt
Before clean-up:
[‘A’, ‘*’, ‘$’, ‘C’, ‘*’, ‘*’, ‘P’, ‘E’, ‘D’, ‘D’, ‘#’, ‘D’, ‘E’, ‘B’, ‘$’, ‘#’]
After clean-up:
[‘#’, ‘#’, ‘$’, ‘$’, ‘D’, ‘D’, ‘E’, ‘E’]
This list is now rigorous; it has no * and it has 8 characters.
RUN 3:
Enter the name of the file: file3.txt
Before clean-up:
[‘A’, ‘B’, ‘*’, ‘C’, ‘*’, ‘D’, ‘*’, ‘*’, ‘*’, ‘E’]
After clean-up:
[]
This list is now rigorous; it has no * and it has 0 characters.
RUN 4:
Enter the name of the file: file4.txt
Before clean-up:
[‘A’, ‘A’, ‘A’, ‘A’, ‘A’, ‘A’, ‘A’]
After clean-up:
[‘A’, ‘A’, ‘A’, ‘A’, ‘A’, ‘A’]
This list has no * but is not rigorous and it has 6 characters.
RUN 5:
Enter the name of the file: file5.txt
Before clean-up:
[]
After clean-up:
[]
This list is now rigorous; it has no * and it has 0 characters.
2 Part 2: Single Player Rummy Game with Dice and strange deck
(80 points)
For this part, you will program a card game described here:
https://www.classicgamesandpuzzles.com/Old-Maid.html
4
You will implement the two player version. One player will be the computer (i.e. your program) and the other a user
of your program. In what follows, let’s refer to the computer player as Robot and user player as Human. You may assume
that Robot will always deal the cards.
As part of this assignment I provided the file called a4_GAME_xxxxxx.py. Replace xxxxxx in the file name with
your student number. You should open that file and run it to see what it does already. All of your code must go
inside of that file. The file already has some functions that are fully coded for you and other functions for which only
docstrings and partial or no code are provided. Designing your program by decomposing it into smaller subproblems
(to be implemented as functions) makes programming easer, less prone to errors and makes your code more readable.
No part of the given code can be changed. Your code must go into clearly indicated places. No code can be added to
the main. You can design some extra functions of your own if you like.
Functions make_deck, wait_for_player and shuffle_deck are already fully coded for you.
You need to develop the remaining functions: deal_cards, remove_pairs, print_deck, get_valid_input, and
play_game. The functions must meet the requirements as specified in their docstrings (and as implied by the example program runs below and as implied by the video instructions).
The main bulk of your code (the game playing part) will go into the function called play_game. That function
should use/call the other functions that you are required to develop (i.e. deal_cards, remove_pairs, print_deck, and
get_valid_input).
When developing function get_valid_input you may assume that Human will enter integer when asked for an
integer, but you may not assume that it will be in the correct range.
The function get_valid_input gets the input from Human about which face-down card of Robot it wants. When it
is Robot’s turn to play you must implement it such that Robot takes a random card from Human. Also recall that what
Human calls 3rd card, for example, is in position/index 2 in Robot’s deck (as it is represented by a list).
Study the example of the program run below carefully to understand how your program should behave. The behaviour of the program that you see in the run is required –s all aspects of it. Also watch the video I made to get even
better idea of how your program must behave. The video can be found here: https://youtu.be/Dmh5Rsdwn18
Some suggestions:
• Study the provided code and understand what it does and how it should be used.
• Spend some time thinking about various parts of the game that need to be implemented. For example, it needs to
be able to display Human’s deck to Human, it needs to be able to ask Human for what card she wants, it needs to
be able to remove pairs from either Human or Robot’s deck … etc. The provided functions do quite of bit of that
job for you.
• When you are coding individual functions recall that you can test each function in the shell without finishing
the remaining functions. For example, when implementing function remove_pairs you can test it in the shell by
typing something like:
>>> remove_pairs([’10♣’, ‘2♣’, ‘5q’, ‘6♣’, ‘9♣, ‘Aq’, ’10q’])
The shell should display (with cards not necessarily in this order):
[‘2♣’, ‘5q’, ‘6♣’, ‘9♣’, ‘Aq’]
Thus you can code and test your functions one by one (without completing the other parts)
• The game alternates between Robot and Human. Think about how you can represent whose turn it is to play, in
your program. One way is to have a variable that you set to zero when it is Robot’s turn and to one when it is
Human’s turn. You also need to figure out what to test to see if the game is over.
2.1 Testing Part 2
Test runs for Part 2 are in the file entitled A4-game-runs.pdf The behaviour implied by the tests runs should be considered as required specifications in addition to what is explained above.
5