, , ,

[SOLVED] Cmput 175 – lab 3: magic square

$25

File Name: Cmput_175_____lab_3__magic_square.zip
File Size: 310.86 KB

Categories: , , , Tags: , , ,
5/5 - (1 vote)

Goal: Review classes/objects, importA magic square is an n by n grid of cells filled with unique positive integers (all greater than 0)
arranged so that all lines in the square (horizontals, verticals, diagonals) add up to the same sum.For example, these are magic squares:
10 3 8
5 7 9
6 11 4
But these are NOT magic squares:
10 13 8
5 7 9
6 11 4A colleague has written a program to read in data for various squares to check if they are magic
squares. She wrote it assuming that there was a MagicSquare class that has the methods
described in Part 1 below. However, that class isn’t finished yet: drawSquare and isMagic are
incomplete. It is up to you to finish the class and test it all, so that an instance of it can then be
used in your colleague’s program.Part 1:
Download and save a copy of lab3_MagicSquare.py. This file contains the skeleton code for a
class called MagicSquare, which you will complete.
1. The __init__(self, n) method has been completed for you. Notice that this method initializes
two attributes: square and size. The size attribute defines how many rows (and columns) are
in the square. The square attribute represents a square with n*n empty cells – it is a list of
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
10 10 8
5 7 9
6 11 4
10 8
5 7 9
6 11 4
21
21
21
21
21 21 21
21
21
31
21
21
21 31
21
21
34
34
34
34
34
34
34 34 34
34
Square is not complete
(one cell is not filled).
Cell contents are not
unique (notice 10 is
present twice).lists, where each internal list represents a row in the square. The value 0 is used to represent
an empty square.
Run the test provided (i.e. by running lab3_MagicSquare.py) to test this method before
moving on:
self.square should be [[0, 0, 0], [0, 0, 0], [0, 0, 0]]2. The cellIsEmpty(row, col) method has been completed for you. This method checks the
contents of the square at the given row index (integer) and column index (integer). If the
cell at that square is “empty”, the method returns True. Otherwise, the method returns False.
Test this method before moving on. You can pick any cell in the 3×3 test square.3. Complete the drawSquare method so that it displays the current contents of the square,
along with the column indices on top of the square, and the row indices to the left of the
square. If the content of a given cell is 0, a period (‘.’) should be displayed.To simplify the formatting, you can assume that only the digits 1-99 can be used as values in
the cells, and you should center the contents of each cell in a field width of 4 places (see
sample output below). You may also assume that the biggest square that will be tested is
10*10, so you only need to worry about single digit indices.
Test your method before moving on:
When self.square is [[0, 0, 0], [0, 0, 0], [0, 0, 0]], the following
should be printed:0 1 2
+————–+
0 | . | . | . |
+————–+
1 | . | . | . |
+————–+
2 | . | . | . |
+————–+4. The update(row, col, num) method has been completed for you. This method checks if the
cell at the provided row index (integer) and column index (integer) is “empty”. This method
also checks that num (integer) does not already exist in another cell in the square. If both of
these conditions are met, the contents of the cell are updated to hold the unique num, and the
method returns True. If the cell is not empty or the number already exists in another cell, the
contents of the cell are not changed, and the method returns False. Test this method before
moving on by inserting a 10 in row 0, column 0 (just like in the first diagram at the
beginning of this lab description). What happens when you try to enter a new number
in that same cell?5. The isFull() method has been completed for you. This method returns True if there are no
“empty” cells, False otherwise. Test this method. Which cases should you consider?
6. Complete the isMagic() method. This method returns True if the square is complete, only
contains unique numbers, and all lines sum up to the same value. Otherwise, it returns
False. Test this method. Which cases should you consider?
Demo your completed class to your TA, along with your tests. Be ready to explain which
aspects you tested and why.Part 2:
1. Once you have fully tested your MagicSquare class, download your colleague’s program
(lab3.py) from eClass and save it in the same folder as your lab3_MagicSquare.py. Also
download and save all of the square_i.txt files in the same folder. Run lab3.py.
*Notice that all of the testing code that you included under if __name__ == ‘__main__’:
in lab3_MagicSquare.py does not execute when you run lab3.py.Sample Output for Part 2:
Reading from square_1.txt…
0 1 2 3
+——————-+
0 | 16 | 3 | 2 | 13 |
+——————-+
1 | 5 | 10 | 11 | 8 |
+——————-+
2 | 9 | 6 | 7 | 12 |
+——————-+
3 | 4 | 15 | 14 | 1 |
+——————-+
This square is MAGIC
=========================
Reading from square_2.txt…
0 1 2
+————–+
0 | 2 | 7 | 6 |
+————–+
1 | 9 | 5 | 1 |
+————–+
2 | 4 | 3 | 8 |
+————–+
This square is MAGIC
=========================
Reading from square_3.txt…
0 1 2 3 4
+————————+
0 | 17 | 24 | 1 | 8 | 15 |
+————————+
1 | 23 | 5 | 7 | 14 | 16 |
+————————+
2 | 4 | 6 | 13 | 20 | 22 |
+————————+
3 | 10 | 12 | 19 | 21 | 3 |
+————————+
4 | 11 | 18 | 25 | 2 | 9 |
+————————+
This square is MAGIC
=========================
Reading from square_4.txt…
0 1 2 3 4
+————————+
0 | 17 | . | 1 | 8 | 15 |
+————————+
1 | 23 | 5 | 7 | 14 | 16 |
+————————+
2 | 4 | 6 | 13 | 20 | 22 |
+————————+
3 | 10 | 12 | 19 | 21 | 3 |
+————————+
4 | 11 | 18 | 25 | 2 | 9 |
+————————+
Nothing magic about this square
=========================
Reading from square_5.txt…
0 1 2
+————–+
0 | 2 | 7 | 6 |
+————–+
1 | 9 | 5 | 1 |
+————–+
2 | 4 | 3 | . |
+————–+
Nothing magic about this square
=========================
Reading from square_6.txt…
0 1
+———+
0 | 3 | 6 |
+———+
1 | 9 | 4 |
+———+
Nothing magic about this square
=========================

Shopping Cart
[SOLVED] Cmput 175 – lab 3: magic square[SOLVED] Cmput 175 – lab 3: magic square
$25