Question

Magic Square question for Python

You have this solution in Java, I am interested in this same solution for Python.


One interesting application of two-dimensional arrays is magic squares. A magic square is a square matrix in which the sum of every row, every column, and bothdiagonals is the same. Magic squares have been studied for many years, and there are some particularly famous magic squares. In this exercise you will write code todetermine whether a square is magic.

You should find that the first, second, and third squares in the input are magic, and that the rest (fourth through seventh) are not. Note that the -1 at the bottomtells the test program to stop reading.

Copy the data given and save NotePad in a file called magicData.txt.
3
8 1 6
3 5 7
4 9 2
7
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
4
48 9 6 39
27 18 21 36
15 30 33 24
12 45 42 3
3
6 2 7
1 5 3
2 9 4
4
3 16 2 13
6 9 7 12
10 5 11 8
15 4 14 1
5
17 24 15 8 1
23 5 16 14 7
4 6 22 13 20
10 12 3 21 19
11 18 9 2 25
7
30 39 48 1 10 28 19
38 47 7 9 18 29 27
46 6 8 17 26 37 35
5 14 16 25 34 45 36
13 15 24 33 42 4 44
21 23 32 41 43 12 3
22 31 40 49 2 20 11
-1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code is

import java.io.File;

import java.io.IOException;

import java.util.Scanner;

class MagicSquare {

int[][] square;

public MagicSquare(int size)

{

square=new int[size][size];

}

//--------------------------------------

//return the sum of the values in the given row

//--------------------------------------

private int sumMagicRow(int row)

{

int sum = 0;

for(int i=0;i

sum+=square[row][i];

return sum;

}

//--------------------------------------

//return the sum of the values in the given column

//--------------------------------------

private int sumMagicCol(int col)

{

int sum = 0;

for(int i=0;i

sum+=square[i][col];

return sum;

}

//--------------------------------------

//return the sum of the values in the main diagonal

//--------------------------------------

private int sumMagicDiagMain()

{

int sum=0;

for(int i=0;i

sum+=square[i][i];

return sum;

}

//--------------------------------------

//return the sum of the values in the other ("reverse") diagonal

//--------------------------------------

private int sumMagicDiagRev()

{

int sum=0;

for(int i=0;i

sum+=square[i][i];

return sum;

}//--------------------------------------

//return true if the square is magic (all rows, cols, and diags// have same sum), false otherwise

//--------------------------------------

public boolean isMagicSquare()

{

if(sumMagicDiagRev()==sumMagicDiagMain() && sumMagicDiagMain()==sumMagicRow(0))

{

for(int i=0;i

{

if(sumMagicRow(0)!=sumMagicRow(i))

return false;

if(sumMagicRow(0)!=sumMagicCol(i))

return false;

}

return true;

}

return false;

}

//--------------------------------------

//compute and display sums of square including row, column, main diagonal, and other diagonal

//--------------------------------------

public void printMagicSquareSums()

{

for(int i=0;i

System.out.println("Sum of row "+(i+1)+" is "+sumMagicRow(i));

for(int i=0;i

System.out.println("Sum of Column "+(i+1)+" is "+sumMagicCol(i));

System.out.println("Sum of main diagonal is: "+sumMagicDiagMain());

System.out.println("Sum of other diagonal is: "+sumMagicDiagRev());

}

//--------------------------------------

//read info into the square from the input stream associated with//the Scanner parameter

//--------------------------------------

public void readSquare(Scanner scan)

{

for (int row = 0; row < square.length; row++)

for (int col = 0; col < square.length; col++)

square[row][col] = scan.nextInt();

}

//--------------------------------------

//print the contents of the square, neatly formatted

//--------------------------------------

public void printSquare()

{

for (int row = 0; row < square.length; row++)

{

for (int col = 0; col < square.length; col++)

System.out.print(square[row][col]+" ");

System.out.println();

}

}

}

// ****************************************************************

// MagicSquareTest.java

//// Text below is to be filled by student.

//// ****************************************************************

public class MagicSquareTest

{

public static void main(String[] args) throws IOException

{

Scanner scan = new Scanner(new File("magicText.txt"));

// make sure that the file magicData is in the current directory

int count = 1;

//count which square we're on

int size = scan.nextInt();

//size of next square

//Expecting -1 at bottom of input file

while (size != -1)

{

  

//create a new Square of the given size

MagicSquare s = new MagicSquare(size);

  

//call its read method to read the values of the square

System.out.println("\n***** Square " + count + " *****");

s.readSquare(scan);

//print the square id

s.printSquare();

//print the sums

s.printMagicSquareSums();

//determine and print whether it is a magic square

System.out.println("It is a magic square: "+s.isMagicSquare());

  

  

  

//get size of next square

size = scan.nextInt();

count++;

}

}

}

The output is


***** Square 1 *****
8 1 6
3 5 7
4 9 2
Sum of row 1 is 15
Sum of row 2 is 15
Sum of row 3 is 15
Sum of Column 1 is 15
Sum of Column 2 is 15
Sum of Column 3 is 15
Sum of main diagonal is: 15
Sum of other diagonal is: 15
It is a magic square: true

***** Square 2 *****
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
Sum of row 1 is 175
Sum of row 2 is 175
Sum of row 3 is 175
Sum of row 4 is 175
Sum of row 5 is 175
Sum of row 6 is 175
Sum of row 7 is 175
Sum of Column 1 is 175
Sum of Column 2 is 175
Sum of Column 3 is 175
Sum of Column 4 is 175
Sum of Column 5 is 175
Sum of Column 6 is 175
Sum of Column 7 is 175
Sum of main diagonal is: 175
Sum of other diagonal is: 175
It is a magic square: true

***** Square 3 *****
48 9 6 39
27 18 21 36
15 30 33 24
12 45 42 3
Sum of row 1 is 102
Sum of row 2 is 102
Sum of row 3 is 102
Sum of row 4 is 102
Sum of Column 1 is 102
Sum of Column 2 is 102
Sum of Column 3 is 102
Sum of Column 4 is 102
Sum of main diagonal is: 102
Sum of other diagonal is: 102
It is a magic square: true

***** Square 4 *****
6 2 7
1 5 3
2 9 4
Sum of row 1 is 15
Sum of row 2 is 9
Sum of row 3 is 15
Sum of Column 1 is 9
Sum of Column 2 is 16
Sum of Column 3 is 14
Sum of main diagonal is: 15
Sum of other diagonal is: 15
It is a magic square: false

***** Square 5 *****
3 16 2 13
6 9 7 12
10 5 11 8
15 4 14 1
Sum of row 1 is 34
Sum of row 2 is 34
Sum of row 3 is 34
Sum of row 4 is 34
Sum of Column 1 is 34
Sum of Column 2 is 34
Sum of Column 3 is 34
Sum of Column 4 is 34
Sum of main diagonal is: 24
Sum of other diagonal is: 24
It is a magic square: false

***** Square 6 *****
17 24 15 8 1
23 5 16 14 7
4 6 22 13 20
10 12 3 21 19
11 18 9 2 25
Sum of row 1 is 65
Sum of row 2 is 65
Sum of row 3 is 65
Sum of row 4 is 65
Sum of row 5 is 65
Sum of Column 1 is 65
Sum of Column 2 is 65
Sum of Column 3 is 65
Sum of Column 4 is 58
Sum of Column 5 is 72
Sum of main diagonal is: 90
Sum of other diagonal is: 90
It is a magic square: false

***** Square 7 *****
30 39 48 1 10 28 19
38 47 7 9 18 29 27
46 6 8 17 26 37 35
5 14 16 25 34 45 36
13 15 24 33 42 4 44
21 23 32 41 43 12 3
22 31 40 49 2 20 11
Sum of row 1 is 175
Sum of row 2 is 175
Sum of row 3 is 175
Sum of row 4 is 175
Sum of row 5 is 175
Sum of row 6 is 175
Sum of row 7 is 175
Sum of Column 1 is 175
Sum of Column 2 is 175
Sum of Column 3 is 175
Sum of Column 4 is 175
Sum of Column 5 is 175
Sum of Column 6 is 175
Sum of Column 7 is 175
Sum of main diagonal is: 175
Sum of other diagonal is: 175
It is a magic square: true

The text file should be edited as given below otherwise it will generate inputMismatchException

3
8 1 6
3 5 7
4 9 2
7
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
4
48 9 6 39
27 18 21 36
15 30 33 24
12 45 42 3
3
6 2 7
1 5 3
2 9 4
4
3 16 2 13
6 9 7 12
10 5 11 8
15 4 14 1
5
17 24 15 8 1
23 5 16 14 7
4 6 22 13 20
10 12 3 21 19
11 18 9 2 25
7
30 39 48 1 10 28 19
38 47 7 9 18 29 27
46 6 8 17 26 37 35
5 14 16 25 34 45 36
13 15 24 33 42 4 44
21 23 32 41 43 12 3
22 31 40 49 2 20 11
-1

Do give a thumbs up and in case there are doubts leave a comment

Add a comment
Know the answer?
Add Answer to:
Magic Square question for Python
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand h...

    C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand how to traverse a two dimensional array Code and run a program that processes a two dimensional array Instructions: A magic square is a matrix (two dimensional arrays) in which the sum of each row, sum of each column, sum of the main diagonal, and sum of the reverse diagonal are all the same value. You are to code a program to determine...

  • Java Magic Square: A n x n matrix that is filled with the numbers 1, 2,...

    Java Magic Square: A n x n matrix that is filled with the numbers 1, 2, 3,.... n^2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. I need to write an application that gets 16 values as user input, in any order. Store these values in an ArrayList. When the numbers are put into a square, this would be a 4x4 two-dimensional array....

  • Game   Point_Differential   Assists   Rebounds   Turnovers   Personal_Fouls 1   15   15   38   11   9 2   36   20   43 &

    Game   Point_Differential   Assists   Rebounds   Turnovers   Personal_Fouls 1   15   15   38   11   9 2   36   20   43   8   13 3   16   21   29   7   13 4   45   22   46   11   11 5   12   11   40   7   22 6   -10   10   31   13   26 7   11   19   45   11   7 8   12   16   32   16   14 9   3   16   27   18   15 10   19   9   34   17   17 11   40   16   41   9   17 12   44   12   29   9   22 13   16  ...

  • Write a program in C that stores the result of all multiplications between 1 and 9...

    Write a program in C that stores the result of all multiplications between 1 and 9 in a two-dimensional int array and then prints the contents of the array to the screen. Also, write some comments in the code. Example: Expected output: 1 4 6 7 8 8 1 12 14 16 18 4 3 6 9 12 15 18 21 24 27 4 8 12 16 20 24 28 32 36 5 1e 15 20 25 30 35 40...

  • A study of reading comprehension in children compared three methods of instruction. The three methods of...

    A study of reading comprehension in children compared three methods of instruction. The three methods of instruction are called Basal, DRTA, and Strategies. As is common in such studies, several pretest variables were measured before any instruction was given. One purpose of the pretest was to see if the three groups of children were similar in their comprehension skills. The READING data set described in the Data Appendix gives two pretest measures that were used in this study. Use one-way...

  • NB: All C programs should be compiled using C Compiler application. The code should be shown...

    NB: All C programs should be compiled using C Compiler application. The code should be shown on the document. A screen shot of the running program given as output on the assignment. a). Describe the types of arrays and operations that can be performed of them. [10] b). Write a program to print the multiplication table from 1*1 to 12*10. [10] Example MULTIPLICATION TABLE 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12...

  • in Java programming Using IF statement, write a program that prints multiplication table for 1-10. Sample...

    in Java programming Using IF statement, write a program that prints multiplication table for 1-10. Sample Program Output. 1     2     3     4     5     6     7     8     9     10       1     1     2     3     4     5     6     7     8     9     10       2     2     4     6     8     10    12    14    16    18    20       3     3     6     9     12    15    18    21    24    27    30       4     4     8     12    16    20    24    28    32    36    40       5     5     10   ...

  • calculate the expected absorbace at 280 nm of a 1.5 mg/ml solution of bovine serum albumin...

    calculate the expected absorbace at 280 nm of a 1.5 mg/ml solution of bovine serum albumin based on the amino acid composition. all cysteins are involved in disulfide bonds. Calculate the expected A280 of a 1.50 mg/ml solution of bovine serum albumin, based on the amino acid composition (Table 1-1, page 28 from your manual). All the cysteines in bovine serum albumin are involved in disulfide bonds. T V W Y 10 9 0 6 12 11 0 7 6...

  • Note: The assignment must be submitted into the designated link in the Blackboard cou page. You...

    Note: The assignment must be submitted into the designated link in the Blackboard cou page. You are to save each HTML5 document file as the following synt StudentID FirstName_LastName Question Number ► QUESTIONS In this assignment you are required to write HTML5 code which contains JavaScript in order to do the following tasks: 1) Construct the below multiplication table using JavaScript code and HTML5 1 2 3 4 5 6 7 8 9 10 11 12 111 2 3 4...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT