Skip to content

Instantly share code, notes, and snippets.

@gajeam
Last active March 27, 2020 18:05
Show Gist options
  • Save gajeam/6040af461f793ff81c2f4f2b1025148d to your computer and use it in GitHub Desktop.
Save gajeam/6040af461f793ff81c2f4f2b1025148d to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9.6764\n"
]
}
],
"source": [
"# A simulation-based solution to this week's Riddler\n",
"# https://fivethirtyeight.com/features/can-you-get-the-gloves-out-of-the-box/\n",
"\n",
"import random\n",
"\n",
"def generate_die(die):\n",
" new_die = []\n",
" for i in die:\n",
" new_die.append(random.choice(die))\n",
" return new_die\n",
"\n",
"def count_die_generations(sides):\n",
" die = range(1, sides+1) # I don't need the actual numbers, but it makes it fun!\n",
" gen_count = 0\n",
" while len(set(die)) != 1:\n",
" die = generate_die(die)\n",
" gen_count += 1\n",
" return gen_count\n",
"\n",
"def run_simulation(sides=6, sim_count=5000):\n",
" total = 0.0\n",
" for i in range(sim_count):\n",
" total += count_die_generations(sides)\n",
" return total/float(sim_count)\n",
"\n",
"print(run_simulation(6))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment