Python Question! NEEDS TO BE ANSWERED ASAP!!!!1 15 15I 153 15- 15 = 15 = 15 = 15
ID: 3679613 • Letter: P
Question
Python Question! NEEDS TO BE ANSWERED ASAP!!!!1
15 15I 153 15- 15 = 15 = 15 = 15 2 Lab Question 2 (Magic Square) [8 points]: Write a Python function that constructs a magic square. A magic square is a square array of numbers consisting of the distinct positive integers 1, 2, ..., n2 arranged such that the sum of the n numbers in any horizontal, vertical, or main diagonal line is always the same number. A magic square of length n-3 is to the right. See for example en.wikipedia.ora/wiki/Magic_square for the history of magic squares 3 Suppose m is a two-dimensional square indexed from 0 to n - 1 in both dimensions. For an odd number n, a simple algorithm for constructing magic squares exists: the square is constructed by filling in the numbers 1 to n2 in mi, j, starting at i= (n-1 2 and,-n-1 and incrementing i and j by one in each step, modulo n. Every n-th step j is decremented by one and i is left unchanged. In the square to the right, we start at 1 andj- 2 and put 1 in that location; incrementing i makes i 2 and incrementing j makes j- 3, where we need to put 2 (grey above), but modulo 3 makesj -0. VWe find the location of 3 similarly but then need to move one down and continue there with 4, etc. Write a function magicsquare (n) that returns a square as a list of lists (but the function itself does not print it) and a function printsquare (s) that takes a list of lists and prints the square such that the numbers are aligned and there are two spaces between the numbers. For example:Explanation / Answer
def main(): square1 = [[8, 1, 6], [3, 5, 7], [4, 9, 2]] square2 = [[8, 9, 6], [3, 1, 7], [4, 2, 5]] square3 = [[9, 3, 6], [5, 1, 4], [2, 7, 8]] square4 = [[4, 9, 2], [3, 5, 7], [8, 1, 6]] square5 = [[5, 3, 8], [4, 2, 7], [6, 9, 1]] square6 = [[5, 1, 7], [9, 4, 2], [6, 3, 8]] square7 = [[7, 9, 6], [2, 3, 8], [5, 4, 1]] square8 = [[8, 3, 4], [1, 5, 9], [6, 7, 2]] # check to see if square one is true for x in square1: for y in range(3): if sum(x) == sum (square1[y][y] for y in range(3)): if sum(x)== sum(x[y] for x in square1): checks1 = 'Is a Lo Shu Magic Square' else: checks1= 'Is Not a Lo Shu Magic Square' print("Square1 is", checks1) #check to see if square2 is true for x in square2: for y in range(3): if sum(x) == sum (square2[y][y] for y in range(3)): if sum(x)== sum(x[y] for x in square2): checks1 = 'Is a Lo Shu Magic Square' else: checks1 = 'Is Not a Lo Shu Magic Square' print('Square2 is',checks1) #check to see if square3 is true for x in square3: for y in range(3): if sum(x) == sum (square3[y][y] for y in range(3)): if sum(x)== sum(x[y] for x in square3): checks1 = 'Is a Lo Shu Magic Square' else: checks1 = 'Is Not a Lo Shu Magic Square' print('Square3 is',checks1) #check to see if square4 is true for x in square4: for y in range(3): if sum(x) == sum (square4[y][y] for y in range(3)): if sum(x)== sum(x[y] for x in square4): checks1 = 'Is a Lo Shu Magic Square' else: checks1 = 'Is Not a Lo Shu Magic Square' print('Square4 is',checks1) #check to see if square5 is true for x in square5: for y in range(3): if sum(x) == sum (square5[y][y] for y in range(3)): if sum(x)== sum(x[y] for x in square5): checks1 = 'Is a Lo Shu Magic Square' else: checks1 = 'Is Not a Lo Shu Magic Square' print('Square5 is',checks1) #check to see if square6 is true for x in square6: for y in range(3): if sum(x)== sum(x[y] for x in square6): if sum(x) == sum (square6[y][y] for z in range(3)): checks1 = 'Is a Lo Shu Magic Square' else: checks1 = 'Is Not a Lo Shu Magic Square' print('Square6 is',checks1) #check to see if square7 is true for x in square7: for y in range(3): if sum(x) == sum (square7[y][y] for y in range(3)): if sum(x)== sum(x[y] for x in square7): checks1 = 'Is a Lo Shu Magic Square' else: checks1 = 'Is Not a Lo Shu Magic Square' print('Square7 is',checks1) #check to see if square8 is true for x in square8: for y in range(3): if sum(x) == sum (square8[y][y] for y in range(3)): if sum(x)== sum(x[y] for x in square8): checks1 = 'Is a Lo Shu Magic Square' else: checks1 = 'Is Not a Lo Shu Magic Square' print('Square8 is',checks1) main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.