Created
August 26, 2015 13:14
-
-
Save dave-martinez/5e7aff7c2a40926c9cdf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Magic Square | |
# Define 3x3 square -- Initalized to 0 | |
square = [[0 for x in range(3)] for x in range(3)] | |
def get_input(): | |
for row in range(3): | |
for col in range(3): | |
square[row][col] = int(raw_input("Enter number: ")) | |
def display_square(): | |
for row in range(3): | |
for col in range(3): | |
print square[row][col], | |
print "\n" | |
def checksquare(): | |
sum_col = [] | |
sum_row = [] | |
sum_ldiagonal, sum_rdiagonal = 0, 0 | |
# Horizontal Sum | |
for row in range(3): | |
sum_x = 0 | |
for col in range(3): | |
sum_x += square[row][col] | |
sum_col.append(sum_x) | |
print "Horizontal Sum: ",sum_col | |
# Vertical Sum | |
for col in range(3): | |
sum_x = 0 | |
for row in range(3): | |
sum_x += square[row][col] | |
sum_row.append(sum_x) | |
print "Vertical Sum", sum_row | |
# Diagonals Sum | |
sum_ldiagonal = square[0][0] + square[1][1] + square[2][2] | |
sum_rdiagonal = square[0][2] + square[1][1] + square[2][0] | |
print "Diagonal Sum: ",sum_rdiagonal, sum_ldiagonal | |
# Verify | |
magicsquare = False | |
if (len(set(sum_row)) == 1) and (len(set(sum_col)) == 1): | |
if sum_rdiagonal == 15 and sum_ldiagonal == 15: | |
magicsquare = True | |
if magicsquare: | |
print "It is a magic square\t(^_^)" | |
else: | |
print "It is NOT a magic square\t(T_T)" | |
print "\n" | |
# Main Program | |
get_input() | |
display_square() | |
checksquare() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment