Skip to content

Instantly share code, notes, and snippets.

@atlefren
Created May 24, 2017 13:07
Show Gist options
  • Save atlefren/aed000ce1d090d894198d5b221886ae0 to your computer and use it in GitHub Desktop.
Save atlefren/aed000ce1d090d894198d5b221886ae0 to your computer and use it in GitHub Desktop.
# -*- 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