Skip to content

Instantly share code, notes, and snippets.

@jholman
Created June 17, 2010 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jholman/441533 to your computer and use it in GitHub Desktop.
Save jholman/441533 to your computer and use it in GitHub Desktop.
a reply to gist 438980
Inspired by
http://gist.github.com/438980
and, in turn
http://www.reddit.com/r/programming/comments/cehbe/awesome_lego_robot_monster_chessand_shit_gets/c0s0afu
jholman $ python symm.py 3 | wc -l
18
jholman $ python symm.py 4 | wc -l
60
jholman $ python symm.py 5 | wc -l
150
Now, to ensure that I'm not counting any ineligible candidates,
I print out all rotations, remove duplicates, and then manually
verify that it's four times as many as with simple output above.
jholman $ python symm.py 4 True | sort | uniq | wc -l
240
jholman $ python symm.py 5 True | sort | uniq | wc -l
600
And now for some raw output:
jholman $ python symm.py 2
AAAB
AABB
ABBB
jholman $ python symm.py 3
AAAB
AAAC
AABB
AABC
AACB
AACC
ABAC
ABBB
ABBC
ABCB
ABCC
ACBB
ACBC
ACCB
ACCC
BBBC
BBCC
BCCC
rot = lambda a,n: a[n%len(a):] + a[:n%len(a)]
rotset = lambda a: set([rot(a,i) for i in range(len(a))])
def all_patterns(num,show_all = False): # I need a Design Pattern for exponential iteration
already_saw = set([])
for i in range(num):
for j in range(num):
for k in range(num):
for m in range(num):
pattern = [i,j,k,m]
word = ''.join(map(lambda x:chr(x+65), pattern))
rots = rotset(word)
if len(rots) == 4 and len(rots.intersection(already_saw)) == 0:
already_saw.update(rots)
if show_all:
for w in rots:
print w
else:
print word
if __name__ == '__main__':
from sys import argv
show_all = False
n = 3
if len(argv) > 2 and argv[2] == 'True':
show_all = True
if len(argv) > 1:
try:
n = int(argv[1])
except ValueError:
pass
all_patterns(n,show_all)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment