pythonmagic-square

Magic square python


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

The first three numbers in each line represent the top row of the square, the next three are the middle row, and the last three are the bottom row. The problem im having is that the first three squares ARE valid, and the last four are supposed to be invalid. But what my code keeps printing out for me is

Valid
Valid
Valid
Valid
Valid
Invalid
Valid

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.


Solution

  • My version without spliting items into rows

    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 data.split("\n"):
            # create list with all numbers
            items = list(map(int, line.split()))
            print(is_magic(items))
    
    def is_magic(items):
    
        # --- dups ---
    
        #print('dups')
        #print(len(set(items)) == 9)
        #for x in range(1, 10):
        #    print(x, x in items)
        if len(set(items)) != 9:
            return 'Invalid'
    
        # --- rows ---
    
        #print('rows')
        for x in range(0, 9, 3):
            l = items[x:x+3]
            #print(l, sum(l) == 15)
            if sum(l) != 15:
                return 'Invalid'
    
        # --- cols ---
    
        #print('cols')
        for x in range(3):
            l = [items[x], items[x+3], items[x+6]]
            #print(l, sum(l) == 15)
            if sum(l) != 15:
                return 'Invalid'
    
        # --- diags ---
    
        #print('diags')
        l = [items[0], items[4], items[8]]
        #print(l, sum(l) == 15)
        if sum(l) != 15:
            return 'Invalid'
    
        l = [items[2], items[4], items[6]]
        #print(l, sum(l) == 15)
        if sum(l) != 15:
            return 'Invalid'
    
        # --- OK ---
    
        return 'Valid'
    
    main()