Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (4k points)
edited by

I'm coding a program that reads a line in a file and determines whether or not the line makes a Lo Shu Magic square. In this magic square, the sum of the rows, sum of the columns, and sum of the diagonals have to equal 15, and each number 1-9 can only occur once in the square. This is what I have so far:

def main():
    for line in open("Magic Square Input.txt"):
        items = line.split(" ")
        items = [int(x) for x in items]
        result = [items[0:3], items[3:6], items[6:9]]
        isMagic(result)

def isMagic(result):
    checks1 = ''
    for x in result:
        for y in range(3):
            if sum (result[y][y] for y in range(3)) == 15:
                if sum(x[y] for x in result) == 15:
                    checks1 = checkDupe(result)
                else:
                    checks1 = 'Invalid'
            else:
                checks1 = 'Invalid'

    print(checks1)

def checkDupe(result):
    checks1 = ''
    for i in range(0,8):
        counter = 0
        for j in result:
            if (j == i):
                counter += 1
        if counter > 0:
            checks1 = 'Invalid'
        else:
            checks1 = 'Valid'
    return checks1
main()

the contents of my text file are as follows:

4 3 8 9 5 1 2 7 6
8 3 4 1 5 9 6 7 2
6 1 8 7 5 3 2 9 4
6 9 8 7 5 3 2 1 4
6 1 8 7 5 3 2 1 4
6 1 3 2 9 4 8 7 5
5 5 5 5 5 5 5 5 5

Could somebody show me where I'm screwing up here? I'm fairly new to python and I've been staring at this for hours trying to make sense of it.

1 Answer

0 votes
by (8.7k points)
edited by

It basically revolves around the term Lo Shu Magic square and this  Python program determine whether it follows a certain condition or not.

Condition: It is that the sum of the columns,sum of the rows and sum of the diagonals have to equal 15, and each number 1 to 9 can only occur once in the square

sq_data = '''
4 3 8 9 5 1 2 7 6
8 3 4 1 5 9 6 7 2
6 1 8 7 5 3 2 9 4
6 9 8 7 5 3 2 1 4
6 1 8 7 5 3 2 1 4
6 1 3 2 9 4 8 7 5
5 5 5 5 5 5 5 5 5'''

def main():

for line in sq_data.split("\n"):                    ## it will create list of all number

    elements = list(map(int, line.split()))

    print(wonder (elements))

def wonder(elements):

##dups

#print('dups')

#print(len(set(elements)) == 9)

#for x in range(1, 10):

# print(x, x in elements)

if len(set(elements)) != 9:

          return 'Not valid'                                                                             ##rows

#print('rows')

for x in range(0, 9, 3):

    m = items[x:x+3]

    #print(m, sum(m) == 15)

    if sum(m) != 15:

        return 'Not valid'

                                                                                                                   ##cols

#print('cols')

for x in range(3):

    m = [elements [x], elements [x+3], elements [x+6]]

    #print(m, sum(m) == 15)

    if sum(m) != 15:

      return 'Not valid'

##  diags

#print('diags')

m = [elements [0], items[4], items[8]]

#print(m, sum(m) == 15)

if sum(m) != 15:

  return 'Not valid'

m = [elements [2], elements [4], elements [6]]

#print(m, sum(m) == 15)

if sum(m) != 15:

    return 'Not valid'

# --- OK ---

return 'Valid'

main()

Related questions

0 votes
1 answer
asked Dec 10, 2020 in Python by laddulakshana (16.4k points)
0 votes
1 answer
0 votes
1 answer
asked Feb 12, 2021 in Python by adhiraj (4k points)
0 votes
1 answer
asked Jan 12, 2021 in Python by ashely (50.2k points)
0 votes
1 answer
asked Sep 27, 2019 in Python by Sammy (47.6k points)

Browse Categories

...