0% found this document useful (0 votes)
31 views

Arrays

This document provides instructions for an assignment involving Python programs that manipulate lists, dictionaries and strings. It includes 3 questions - the first involves right aligning a user-input list of strings, the second defines utility functions for a 4x4 grid, and the third completes code for a 2048 game using those utility functions.

Uploaded by

Takudzwa Dudu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Arrays

This document provides instructions for an assignment involving Python programs that manipulate lists, dictionaries and strings. It includes 3 questions - the first involves right aligning a user-input list of strings, the second defines utility functions for a 4x4 grid, and the third completes code for a 2048 game using those utility functions.

Uploaded by

Takudzwa Dudu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Version 22/09/2023 08:32:40

CSC1015S Assignment 9
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 assignments, up to
10 marks will be deducted for deficiencies.

Question 1 [30 marks]


Write a Python program called ‘right-align.py’ where the user can repeatedly enter a list of
strings. Use the sentinel "DONE" to end the input list. The list of strings is then printed out right-aligned
with the longest string.

(NOTE: The input from the user is shown in bold font.)


Sample I/O:
Enter strings (end with DONE):
Tumelo
Marcus
Rufaro
Dominique
Amanda
Elizabeth
Panayioti
Amiera
Absolom

Page 1 of 5
Version 22/09/2023 08:32:40

Oge
Christopher
DONE

Right-aligned list:
Tumelo
Marcus
Rufaro
Dominique
Amanda
Elizabeth
Panayioti
Amiera
Absolom
Oge
Christopher

Question 2 [30 marks]

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.)

The functions you need to write are as follows:

def create_grid(grid):
"""create a 4x4 array of zeroes within grid"""

def print_grid (grid):


"""print out a 4x4 grid in 5-width columns within a box"""

def check_lost (grid):


"""return True if there are no 0 values and there are no

adjacent values that are equal; otherwise False"""

def check_won (grid):


"""return True if a value>=32 is found in the grid; otherwise

False"""

def copy_grid (grid):


"""return a copy of the given grid"""

Page 2 of 5
Version 22/09/2023 08:32:40

def grid_equal (grid1, grid2):


"""check if 2 grids are equal - return boolean value"""

Note: these functions are described using docstrings.

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 |
+--------------------+

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

Page 3 of 5
Version 22/09/2023 08:32:40

Question 3 [40 marks]

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:

def push_up (grid):


"""merge grid values upwards"""

def push_down (grid):


"""merge grid values downwards"""

def push_left (grid):


"""merge grid values left"""

def push_right (grid):


"""merge grid values right"""

The original game can be played at: http://gabrielecirulli.github.io/2048/

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.

Page 4 of 5
Version 22/09/2023 08:32:40

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 |
+--------------------+
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 right-align.py, util.py, and push.py.

Page 5 of 5

You might also like