Last active
April 20, 2016 07:32
-
-
Save fuzzy-focus/7424138df5055f0ee3703db4c927b27d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import itertools | |
import collections | |
#utility functions | |
def calc_dist(dice_list,fun): | |
cntr = {i:0 for i in range(2,13)} | |
cntr.update(collections.Counter(map(fun, dice_list))) | |
if len(cntr) != 11: # discard if sums outside of range(2,13) are generated | |
return ['== WRONG == ']*11 | |
return ['{: >3} : {: >5.2f}%'.format(k,100*v/len(dice_list)) for k,v in cntr.items()] | |
def print_hcat(*text): | |
print('\n'.join(' '.join(l) for l in zip(*text))) | |
#list of all possible results of throwing 2 or 3 D6 | |
l3d6 = list(itertools.product(range(1,7),repeat=3)) | |
l2d6 = list(itertools.product(range(1,7),repeat=2)) | |
#------- create functions for summation here -------- | |
def tereka(dice): | |
a,b,c = sorted(dice) | |
s = (a+b+c) % 6 | |
if s in (0,1): return a+b | |
elif s in (2,3): return a+c | |
else: return b+c | |
def trdi(dice): | |
s = sum(dice) % 6 | |
p = len([d for d in dice if d in (2,3,5)]) | |
if s == 0: return 3 if p == 1 else 5 | |
elif s == 1: return 2 if p == 3 else 6 | |
elif s == 2: return 4 if p == 2 else 10 | |
elif s == 3: return 11 if p == 2 else 9 | |
elif s == 4: return 12 if p == 3 else 8 | |
elif s == 5: return 7 | |
#------- add all test functions here -------- | |
funs = [tereka , trdi] | |
#------- automatic evaluation below -------- | |
t = [['2D6 dist ']+calc_dist(l2d6,sum)] | |
for f in funs: | |
t.append(['{: <12}'.format(f.__name__[:13])]+ calc_dist(l3d6,f)) | |
print_hcat(*t) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment