def dieTable(numDice, numSides): if numDice == 0: return {0: 1} outcomes = dict([(roll, 0) for roll in range(1, numDice * numSides + 1)]) outcomesFromOtherDice = dieTable(numDice - 1, numSides) for roll, count in outcomesFromOtherDice.items(): for side in range(1, numSides + 1): outcomes[side + roll] += count return outcomes def oddsToBeat(target, table): total = 0 above = 0 for roll, count in table.items(): if roll > target: above += count total += count return float(above) / float(count) def printDieTable(table): for roll in range(1, len(table) + 1): if roll in table: count = table[roll] else: count = 0 print "%2d: %d" % (roll, count) # printDieTable(dieTable(3,4)) print oddsToBeat(5, dieTable(3,4))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question