Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. def dupe_check(true_array):
  2.     number_list = []
  3.     for current_list in true_array:
  4.         for j in current_list:
  5.             number_list.append(j)
  6.     number_list.sort()
  7.     return number_list == range(1,10)
  8. def magic_square(true_array):
  9.     if not dupe_check(true_array):
  10.             return False
  11.     iSize = len(true_array[0])
  12.     sum_list = []
  13.        
  14.     #Vertical:
  15.     for col in range(iSize):
  16.         sum_list.append(sum(row[col] for row in true_array))
  17.    
  18.     #Horizontal
  19.     sum_list.extend([sum (lines) for lines in true_array])
  20.    
  21.     #Diagonals
  22.     dlResult = 0
  23.     for i in range(0,iSize):
  24.         dlResult +=true_array[i][i]
  25.     sum_list.append(dlResult)  
  26.    
  27.     drResult = 0
  28.     for i in range(iSize-1,-1,-1):
  29.         drResult +=true_array[i][i]
  30.     sum_list.append(drResult)
  31.  
  32.     if len(set(sum_list))>1:
  33.         return False
  34.     return True
  35.  
  36. a = [[2, 7, 6], [9, 5,1 ], [4, 3, 8]]
  37.  
  38. b = [[4, 9, 2], [3, 5, 7], [8, 1, 6]]
  39.  
  40. print dupe_check(a) # True
  41.  
  42. print magic_square(a) # True
  43.  
  44. print "and now b"
  45.  
  46. print dupe_check(b) # True
  47.  
  48. print magic_square(b) # True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement