Question
Include the following statements in your .py file for testing. print(Part c) allM = generate(M) sortedM = sortAll(M) print(Sorted magic squares to,M) for m in
Include the following statements in your .py file for testing.
print("Part c")
allM = generate(M)
sortedM = sortAll(M)
print("Sorted magic squares to",M)
for m in sortedM:
print(m)
print("==========")
print("Part d")
P = [[6,16,8],[12,10,8],[12,4,14]]
answer = decompose(P)
print("There are",len(answer),"answers")
for a in answer:
print(P,"=",a[0],"+",a[1])
The output would be:
Part c
Sorted magic squares to [[6, 1, 8], [7, 5, 3], [2, 9, 4]]
[[2, 7, 6], [9, 5, 1], [4, 3, 8]]
[[2, 9, 4], [7, 5, 3], [6, 1, 8]]
[[4, 3, 8], [9, 5, 1], [2, 7, 6]]
[[4, 9, 2], [3, 5, 7], [8, 1, 6]]
[[6, 1, 8], [7, 5, 3], [2, 9, 4]]
[[6, 7, 2], [1, 5, 9], [8, 3, 4]]
[[8, 1, 6], [3, 5, 7], [4, 9, 2]]
[[8, 3, 4], [1, 5, 9], [6, 7, 2]]
==========
Part d
There are 2 answers
[[6, 16, 8], [12, 10, 8], [12, 4, 14]] = [[4, 9, 2], [3, 5, 7], [8, 1, 6]] + [[2, 7, 6], [9, 5, 1], [4, 3, 8]]
[[6, 16, 8], [12, 10, 8], [12, 4, 14]] = [[2, 7, 6], [9, 5, 1], [4, 3, 8]] + [[4, 9, 2], [3, 5, 7], [8, 1, 6]]
(c) [6 marks] While we can find the list of equivalent magic squares, it is harder to compare them. Let us sort them in a standard order for ease of comparison. Treat the magic square as a list of 9 elements, e.g. the example magic square above is considered [6,1,8,7,5,3,2,9,4]. Magic square Mi is before magic square M2 if the first element of Mi is smaller than the first element of M2. If the first element is the same, compare the second element, and so on to determine the ordering. Write a Python function before (M1,M2) to determine whether Mi is before M2 in standard order. def before (M1, M2): A function to determine whether Mi is before M2 in standard order Input: magic squares Ml and M2 Output: return True if Mi is before M2, False otherwise With your Python function before, it is easy to sort all the equivalent magic squares in standard order using the following simple sorting function. def sortAll (M): allM = generate (M) sortedM = list() while allm: small, where = allm[0], 0 for i in range (1, len (allm)): if before (allm[i], small): small, where = allM[i], i sortedM.append(small) del (allM(where]) return sortedM (d) [6 marks] Given any two magic squares, a new magic square can be generated by adding the corresponding elements from the two magic squares in each cell. For example, adding the two magic squares on the left yields the magic square on the right. 76 4 92 6 168 151 +357 = 12 10 8 124 8 14 It is easy to compute M=M + M2, if we know M, and M2. It is harder to do the reverse, i.e., to decompose M back to M, and M2. Assuming that M and M2 are equivalent to the example matrix above, write a Python function to decompose a given magic square M into its components My and M2 making use of your Python functions defined above, or otherwise. Note that you do not need to sort the answer. Any ordering is acceptable. def decompose (M): A function to decompose M such that M = Ml + M2 Input: magic square M Output: return a list of [[M1, M2), (Ml',M2' l,...] (c) [6 marks] While we can find the list of equivalent magic squares, it is harder to compare them. Let us sort them in a standard order for ease of comparison. Treat the magic square as a list of 9 elements, e.g. the example magic square above is considered [6,1,8,7,5,3,2,9,4]. Magic square Mi is before magic square M2 if the first element of Mi is smaller than the first element of M2. If the first element is the same, compare the second element, and so on to determine the ordering. Write a Python function before (M1,M2) to determine whether Mi is before M2 in standard order. def before (M1, M2): A function to determine whether Mi is before M2 in standard order Input: magic squares Ml and M2 Output: return True if Mi is before M2, False otherwise With your Python function before, it is easy to sort all the equivalent magic squares in standard order using the following simple sorting function. def sortAll (M): allM = generate (M) sortedM = list() while allm: small, where = allm[0], 0 for i in range (1, len (allm)): if before (allm[i], small): small, where = allM[i], i sortedM.append(small) del (allM(where]) return sortedM (d) [6 marks] Given any two magic squares, a new magic square can be generated by adding the corresponding elements from the two magic squares in each cell. For example, adding the two magic squares on the left yields the magic square on the right. 76 4 92 6 168 151 +357 = 12 10 8 124 8 14 It is easy to compute M=M + M2, if we know M, and M2. It is harder to do the reverse, i.e., to decompose M back to M, and M2. Assuming that M and M2 are equivalent to the example matrix above, write a Python function to decompose a given magic square M into its components My and M2 making use of your Python functions defined above, or otherwise. Note that you do not need to sort the answer. Any ordering is acceptable. def decompose (M): A function to decompose M such that M = Ml + M2 Input: magic square M Output: return a list of [[M1, M2), (Ml',M2' l,...]Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started