Question: Include the following statements in your .py file for testing. M = [[6,1,8],[7,5,3],[2,9,4]] print(Part a) print(Given,M) N=flip(M,'hori') print(Flip horizontal,N) N=flip(M,'vert') print(Flip vertical,N) N=rotate(M,'clock',1) print(Rotate clockwise

 Include the following statements in your .py file for testing. M

Include the following statements in your .py file for testing.

M = [[6,1,8],[7,5,3],[2,9,4]]

print("Part a")

print("Given",M)

N=flip(M,'hori')

print("Flip horizontal",N)

N=flip(M,'vert')

print("Flip vertical",N)

N=rotate(M,'clock',1)

print("Rotate clockwise once",N)

N=rotate(M,'counter',1)

print("Rotate counter-clockwise once",N)

N=rotate(M,'clock',2)

print("Rotate clockwise twice",N)

N=rotate(M,'counter',2)

print("Rotate counter-clockwise twice",N)

N=rotate(M,'clock',3)

print("Rotate clockwise thrice",N)

N=rotate(M,'counter',3)

print("Rotate counter-clockwise thrice",N)

print("==========")

print("Part b")

allM = generate(M)

print("Equivalent magic squares to",M)

for m in allM:

print(m)

4. [25 marks) (Magic square) A magic square is a 3x3 square with numbers filled in, such that the sum of each row (three rows), the sum of each column (three columns) and the sum of each diagonal (two diagonals) are the same. The following is a magic square filled with numbers 1 to 9, and the sum is always 15. 618 753 29 Given any magic square, one can generate equivalent magic squares, by rotating it clockwise or counter-clockwise. You could rotate it once, twice, or three times. Rotating it four times comes back to the original one. You can also generate equivalent magic squares by flipping it either vertically or horizontally. One may want to check how many different equivalent magic squares can be generated by rotation and/or flipping. Here are three examples by rotating the magic square above clockwise once, counter-clock twice and flipping horizontally, respectively: 2 7 649 2 294 9 5 1 3 5 7 7 53 816 6118 438 (a) [7 marks) Write two Python functions flip(M, d) and rotate (M,d,n) to generate a new magic square from an input magic square M based on the requested operation. def flip (M,d): A function to flip a magic square M in direction d (d is either vert or hori) Input: magic square M, direction d Output: return flipped magic square def rotate (M, d, n): A function to rotate a magic square M in direction d (d is either clock or counter) for n times Input: magic square M, direction d, times n Output: return rotated magic square (b) [6 marks] Based on your functions, write a Python function generate (M) to return a list of all the equivalent magic squares M such that can be generated from M by a call to rotate, a call to flip, or a call to both (flip then rotate or rotate then flip). The set of equivalent magic squares includes Mitself. def generate (M): A function to return all the equivalent magic squares to M, including M Input: magic square M Output: return the list of equivalent magic squares

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!