8. Arrays
8. Arrays
Assignment Instructions
This assignment involves constructing Python programs that manipulate lists, dictionaries and
strings. You can use the following built-in functions in Python: map( ), math.sqrt( ),
split( ).
NOTE Your solutions to this assignment will be evaluated for correctness and for the following
qualities:
• Documentation
o Use of comments at the top of your code to identify program purpose,
author and date.
o Use of comments within your code to explain each non-obvious functional
unit of code.
• General style/readability
o The use of meaningful names for variables and functions.
• Algorithmic qualities
o Efficiency, simplicity
These criteria will be manually assessed by a tutor and commented upon. In this assignment, up to
10 marks will be deducted for deficiencies.
From the given marks, count the number of marks that satisfy the conditions of each category. For
example, for the following list of marks: 32 67 54 90 77, there is one fail, one 3rd, one lower 2nd, no
upper 2nds and two firsts. Then print out horizontal bars, using "X", that correspond to the counters,
along with some hard-coded axes and labels to represent a histogram.
You should use an array/list to store the counters.
Page 1 of 5
Version 2024/04/09 10:35:03
Sample IO (The input from the user is shown in bold font – do not program this):
Enter a space-separated list of marks:
12 23 34 45 56 67 78 89 90
1 |XXX
2+|
2-|X
3 |X
F |XXXX
Write a module of utility functions called util.py for manipulating 2-dimensional arrays of size
4x4. (These functions will be used in Question 3.)
def create_grid(grid):
"""create a 4x4 array of zeroes within grid"""
Use the testutil.py test program to test your functions. This program takes a single integer
input value and runs the corresponding test on your module. This is a variant of unit testing, where
test cases are written in the form of a program that tests your program. You will learn more about
unit testing in future CS courses.
Sample IO (The input from the user is shown in bold font – do not program this):
2
+--------------------+
|2 2 |
| 4 8 |
| 16 128 |
|2 2 2 2 |
+--------------------+
Page 2 of 5
Version 2024/04/09 10:35:03
Sample IO (The input from the user is shown in bold font – do not program this):
7
True
Sample IO (The input from the user is shown in bold font – do not program this):
17
4 4
16 16
2 2
64 4
64 16
64 2
Sample IO (The input from the user is shown in bold font – do not program this):
9
True
Sample IO (The input from the user is shown in bold font – do not program this):
18
True
2048 is a puzzle game where the goal is to repeatedly merge adjacent numbers in a grid until the
number 2048 is found. Your task in this question is to complete the code for a 2048 program, using
the utility module (util.py) from Question 2 and a supplied main program (2048.py).
The heart of the game is the set of merging functions that merge adjacent equal values and
eliminate gaps - you are required ONLY to write these functions in a module named push.py:
Page 3 of 5
Version 2024/04/09 10:35:03
Note:
• The check_won() function from util.py assumes you have won when you reach 32 -
this is simply to make testing easier.
• The random number generator has been set to generate the same values each time for
testing purposes.
Sample IO (The input from the user is shown in bold font – do not program this):
+--------------------+
| |
| |
| 2 |
|2 |
+--------------------+
Enter a direction:
l
+--------------------+
| |
| |
|2 |
|2 2 |
+--------------------+
Enter a direction:
u
+--------------------+
|4 2 |
| |
| |
| 4 |
+--------------------+
Enter a direction:
d
+--------------------+
| 2 |
| |
| |
|4 4 2 |
+--------------------+
Page 4 of 5
Version 2024/04/09 10:35:03
Enter a direction:
r
+--------------------+
| 2 |
| |
| 2 |
| 8 2 |
+--------------------+
Enter a direction:
x
Submission
Create and submit a Zip file called ‘ABCXYZ123.zip’ (where ABCXYZ123 is YOUR student number)
containing util.py, push.py and histogram.py.
Page 5 of 5