Created
May 24, 2017 13:07
-
-
Save atlefren/aed000ce1d090d894198d5b221886ae0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from itertools import product | |
def diff(l1, l2): | |
if len(l1) != len(l2): | |
return 1000000 | |
diff = 0 | |
for a, b in zip(l1, l2): | |
if a != b: | |
diff += 1 | |
return diff | |
cards = [u'Hjerter', u'Ruter', u'Spar', u'Kløver'] | |
perms = list(product(cards, repeat=len(cards))) | |
def get_from_list(perms, index=0): | |
perm = perms.pop(index) | |
ordered = [perm] | |
if len(perms) == 0: | |
return ordered | |
min_diff = min([diff(perm, el) for el in perms]) | |
next_idx = next(i for i, perm2 in enumerate(perms) if diff(perm, perm2) == min_diff) | |
return ordered + get_from_list(perms, next_idx) | |
for perm in reversed(get_from_list(perms)): | |
print '%s - %s - %s - %s' % perm | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment