Skip to content

Instantly share code, notes, and snippets.

@theblacksquid
Last active August 29, 2015 14:11
Show Gist options
  • Save theblacksquid/aeff10495a92670bb93f to your computer and use it in GitHub Desktop.
Save theblacksquid/aeff10495a92670bb93f to your computer and use it in GitHub Desktop.
WIP
from random import randint
# was able to finally create a working mechanism for
# creating a grid of random numbers n thru n**2
def genSquare(num):
matrix = {}; already = []
# initialize empty grid
for count in range(num):
matrix[count] = [0]*num
# produce the grid,
for row in range(num):
i = 0
while i != num:
# one item at a time
number = randint(1, num**2)
if number in already:
pass
else:
already.append(number)
matrix[row][i] = number # matrix[x][y]
i = i + 1
return matrix
# made a row-column evaluator that checks if the sums of
# the rows and column items per row and column are
# all equal to m = (n*(n**2)+1)/2; magick square common sum
def evalSquare(squareObj):
rowSums = {}; colSums = {};
itemNum = len(squareObj[0]);
n = itemNum
m = (n*((n**2)+1))/2
for row in range(itemNum):
box = 0;
for item in range(itemNum):
box = box + squareObj[row][item]
rowSums[row] = box
if rowSums[row] != m:
return False
for col in range(itemNum):
box = 0;
for item in range(itemNum):
box = box + squareObj[item][col]
colSums[col] = box
if colSums[col] != m:
return False
return True
# with all the primary parts there, the main function can now
# be defined, but, is extremely slow. Is able to produce a correct
# result for 3*3 squares, but has yet to produce any for anything
# beyond that. If it could, then it would've taken longer than the
# 3-5 minutes i've waited for the tests to provide any output.
def genKamea(num2square):
while True:
square = genSquare(num2square)
check = evalSquare(square)
if check == False:
pass
else:
break
return square
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment