Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Basic brute-force solution of solution to Siecherman Dice problem
{
"metadata": {
"name": "Siecherman Dice"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": "import itertools\n\n# Generate distribution of real dice\nrealdice1 = [1, 2, 3, 4, 5, 6]\nrealdice2 = [1, 2, 3, 4, 5, 6]\n\nrealdist = {}\nfor x in realdice1:\n for y in realdice2:\n realdist[x+y]= realdist.get(x+y, 0)+1\n \n \n# testgrid(D1, D2)\n# Test two dice to see if the probability distribution of their \n# sum is the same as two standard dice\ndef testgrid(D1, D2): \n results = {}\n for x in D1:\n for y in D2:\n results[x+y]= results.get(x+y, 0)+1\n if results[x+y] > realdist.get(x+y, 99):\n return False\n\n for k in results:\n if not (results[k] == realdist.get(k, -1)):\n return False\n\n return True\n\n\n# Test all combinations of dice.\n# (this was slow when I tried it on the train because I used wasn't thinking\n# and used itertools.product() instead of combinations_with_replacement();\n# itertools.product() returns every combination *and* permutation, so it takes forever.\nfor D1 in itertools.combinations_with_replacement([1, 2, 3, 4, 5, 6, 7, 8], 6):\n for D2 in itertools.combinations_with_replacement([1, 2, 3, 4, 5, 6, 7, 8], 6):\n if max(D1) + max(D2) > 12: continue\n if testgrid(D1, D2):\n print D1, D2",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "(1, 2, 2, 3, 3, 4) (1, 3, 4, 5, 6, 8)\n(1, 2, 3, 4, 5, 6)"
},
{
"output_type": "stream",
"stream": "stdout",
"text": " (1, 2, 3, 4, 5, 6)\n(1, 3, 4, 5, 6, 8)"
},
{
"output_type": "stream",
"stream": "stdout",
"text": " (1, 2, 2, 3, 3, 4)\n"
}
],
"prompt_number": 63
},
{
"cell_type": "code",
"collapsed": false,
"input": "",
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.