In [29]:
from itertools import product
from collections import namedtuple
In [35]:
Die = namedtuple('Die', 'name, faces')
dice = [Die('A', [3,3,3,3,3,3]),
Die('B', [4,4,4,4,0,0]),
Die('C', [5,5,5,1,1,1]),
Die('D', [2,2,2,2,6,6])]
In [44]:
def cmp_die(die1, die2):
'compares two die returning <, > or ='
# Numbers of times one die wins against the other for all combinations
win1 = sum(d1 > d2 for d1, d2 in product(die1, die2))
win2 = sum(d2 > d1 for d1, d2 in product(die1, die2))
return '>' if win1 > win2 else ('<' if win1 < win2 else '=')
In [45]:
dice
Out[45]:
In [46]:
cmp_die(dice[0].faces, dice[1].faces)
Out[46]:
In [47]:
shortform = []
for (n1, f1), (n2, f2) in zip(dice, dice[1:] + dice[:1]):
comparison = cmp_die(f1, f2)
print('%s: %r %s %s: %r' % (n1, f1, comparison, n2, f2))
shortform.append('%s %s %s' % (n1, comparison, n2))
print('\nIn short:', ', '.join(shortform))