Question

In: Computer Science

Python: Lo Shu Magic Square The Lo Shu Magic Square is a grid with 3 rows...

Python:

Lo Shu Magic Square

The Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in figures below. The Lo Shu Magic Square has the following properties:

The grid contains the numbers 1 through 9 exactly.

The sum of each row, each column, and each diagonal all add up to the same number. This is shown in Figure B.

In a program you can stimulate a magic square using a two-dimensional list. Write a function that accepts a two-dimensional list as an argument and determines whether the list is a Lo Shu Magic Square. Test the function in a program.

Figure A

4

9

2

3

5

7

8

1

6

Figure B

4

9

2

3

5

7

8

1

6

Solutions

Expert Solution

PYTHON CODE

# function to add a row for the given index

def sumRow(m,index):

    # decrement by 1 , since index starts at zero
    index=index-1

    # variable to hold the sum of values in a row
    total=0
    try:

        # loop through the list
        for i in range(len(m)):

            # adding every value in the inner list
            for j in range(len(m[i])):

                if i==index:
                    total+=m[i][j]
    except:
        print('Index Error')

    # returning the total
    return total


def sumCol(m,col):

    # decrement by 1 , since index starts at zero
    column=col-1

    # variable to hold the sum of values in a row
    total=0
    try:

        # loop through the list
        for i in range(len(m)):

            # adding every value in the inner list
            for j,v in enumerate(m[i]):

                if j==column:
                    total+=v
    except:
        print('Index Error')

    # returning the total
    return total


def sumdiagonal(m):

    d1=m[0][0]+m[1][1]+m[2][2]
    d2=m[0][2]+m[1][1]+m[2][0]

    return (d1,d2)

def main(m):

    # calling the sumRow() for row 1
    s=sumRow(m,1)  

    # calling the sumRow() for row 1
    r=sumRow(m,2)
  
    # calling the sumRow() for row 1
    t=sumRow(m,3)
      
    c1=sumCol(m,1)
    c2=sumCol(m,2)
    c3=sumCol(m,3)

    d1,d2=sumdiagonal(m)

    if s == r == t == c1 == c2 == c3 == d1 ==d2:
        print('The given list is Lo Shu Magic Square')
    else:
        print('The given list is not a Lo Shu Magic Square')  


# testing
if __name__=="__main__":

    m=[[4,9,2],[3,5,7],[8,1,6]]
    main(m)
    m=[[1,2,3],[4,5,6],[7,8,9]]
    main(m)
  

  


SCREENSHOT FOR OUTPUT


Related Solutions

The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in...
The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in Figure 8-23. The Lo Shu Magic Square has the following properties: l The grid contains the numbers 1 through 9 exactly. l The sum of each row, each column, and each diagonal all add up to the same number. This is shown in Figure 8-24. In a program, you can simulate a magic square using a two-dimensional array. Design a program that initializes a...
The Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in figure. The Lo Shu Magic Square has the following properties:
Python programmingThe Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in figure. The Lo Shu Magic Square has the following properties:The grid contains the numbers 1 through 9 exactly.The sum of each row, each column, and each diagonal all add up to the same number as shown in figure.In a program you can simulate a magic square using a two-dimensional list. Write a function that accepts a two-dimensional list as an argument and determines...
write a java programming using 2D arrey. The Lo Shu Magic Square is a grid with...
write a java programming using 2D arrey. The Lo Shu Magic Square is a grid with 3 rows and 3 columns. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 through 9 (each number only once) The sum of each row, each column and each diagonal are the same In a program you can simulate a magic square using a two-dimensional array. Write a method that accepts a two-dimensional array as an argument, and...
Consider the m by n grid graph: n vertices in each of m rows, and m...
Consider the m by n grid graph: n vertices in each of m rows, and m vertices in each of n columns arranged as a grid, and edges between neighboring vertices on rows and columns (excluding the wrap-around edges in the toric mesh). There are m n vertices in total. a)What is the diameter of this graph? b) From the top left vertex to the bottom right vertex, how many shortest paths are there? Please explain.
An m × n grid graph has m rows of n vertices with vertices closest to...
An m × n grid graph has m rows of n vertices with vertices closest to each other connected by an edge. Find the greatest length of any path in such a graph, and provide a brief explanation as to why it is maximum. You can assume m, n ≥ 2. Please provide an explanation without using Hamilton Graph Theory.
1 How can we specify the number of rows and columns in a grid layout? 2....
1 How can we specify the number of rows and columns in a grid layout? 2. What happens when we don't have enough items to be displayed in a grid layout with a specified size? 3. How can we specify a grid layout component to occupy more than one columns or rows? 4. What happens if the components in a linear and grid layout exceed the layout size? How can we handle this problem so that all components can be...
You will put a possible magic square into a two dimensional array and determine if it...
You will put a possible magic square into a two dimensional array and determine if it is a magic square or not. Some of the code is completed. You can assume that it is a square array and that all of the integers are unique (no repeats) CODE: package TwoDimensionalArrays; public class MagicSquare {    public static boolean checkMagicSquare(int[][]array)    {   //Pre: Assume that the array is a square two dimensional array        //Pre: Assume that there are no...
There lays a 10x10 square grid in front of Claire and she is instructed to stand...
There lays a 10x10 square grid in front of Claire and she is instructed to stand in the lower left square of the grid. She is asked to make her way to the top right square of the grid but she can only move 1 square up or 1 square to the right at a time. 1. In how many ways could she make a path to the top right corner? 2. In how many ways could she make a...
Python 3: Create functions to draw each of these with sides of size side:triangle, square, pentagon,...
Python 3: Create functions to draw each of these with sides of size side:triangle, square, pentagon, hexagon The function should be the shape + "2d" Use import turtle and the turtle commands: forward(), left(), right(), pendown(), penup(), done(), color() Use the goto(X,Y) function to allow the shape to be drawn at a specific location Use the x,y, and side attributes for screen position and size
Write a Python program which adds up columns and rows of given table as shown in...
Write a Python program which adds up columns and rows of given table as shown in the specified figure. Example test case (the four lines below “Input cell value” are input from user, and the five lines below “Results:” are the output of the program.): Input number of rows/columns (0 to exit) 4 Input cell value: 25 69 51 26 68 35 29 54 54 57 45 63 61 68 47 59 Result:             25   69   51   26 171...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT