I've written a simple program in Python which does the following:
Input: an integer n
Output: a 2^n x 2^n matrix containing small Ls which fill the entire matrix except one single square. Each L has it's unique number, and consists of three squares.
It's a fairly simple program, and you can see clearly what it does by typing in a few different ns.
I think this problem has an "official" name, but I'm not aware of it. If anyone knows what it's called, please let me know so I can add a link here. :)
What I'd like to do: Keep the actual algorithm as it is now (that is the rec function), but improve my C-like code to make full use of Python's features.
Code:
#!/usr/bin/env python
import sys, math
ur = [[1,0],
[1,1]]
ul = [[0,1],
[1,1]]
lr = [[1,1],
[1,0]]
ll = [[1,1],
[0,1]]
shapeCounter = 1
matrix = []
def printMatrix():
for row in matrix:
for element in row:
if(element >= 10):
print("%d" % element),
else:
print(" %d" % element),
print
print
def square(n):
global matrix
matrix = [[0]*(2**n) for x in xrange(2**n)]
matrix[0][len(matrix) - 1] = -1
rec(0, 0, len(matrix) - 1, len(matrix) - 1)
matrix[0][len(matrix) - 1] = 0
printMatrix()
def writeRect(rect, x, y):
global shapeCounter
matrix[y][x] += rect[0][0] * shapeCounter
matrix[y+1][x] += rect[1][0] * shapeCounter
matrix[y][x+1] += rect[0][1] * shapeCounter
matrix[y+1][x+1] += rect[1][1] * shapeCounter
shapeCounter += 1
def rec(sx, sy,
ex, ey):
if(ex - sx < 1 or ey - sy < 1):
return
#print("(%d,%d) (%d,%d)" %(sx,sy,ex,ey))
if(matrix[sy][sx] != 0):
rect = ul
elif(matrix[ey][sx] != 0):
rect = ll
elif(matrix[sy][ex] != 0):
rect = ur
elif(matrix[ey][ex] != 0):
rect = lr
hx = sx + (ex - sx) / 2
hy = sy + (ey - sy) / 2
writeRect(rect, hx, hy)
rec(sx, sy, hx, hy)
rec(hx + 1, sy, ex, hy)
rec(sx, hy + 1, hx, ey)
rec(hx + 1, hy + 1, ex, ey)
if __name__=='__main__':
square(int(sys.argv[1]))