public
Last active

a reply to gist 438980

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
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
symm.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.