Created
October 22, 2024 16:35
-
-
Save heathhenley/375602b4a01634af7267688a231b8035 to your computer and use it in GitHub Desktop.
ace_game.ipynb
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
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"authorship_tag": "ABX9TyOe61qvhHP9oENBgIA3Zp0X", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/heathhenley/375602b4a01634af7267688a231b8035/ace_game.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 71, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "zP8yjjnx08Ol", | |
"outputId": "9198d1db-1663-45c4-9b06-f259df878d27" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(91.694, 91.0)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 71 | |
} | |
], | |
"source": [ | |
"import random\n", | |
"\n", | |
"# stop when you find, don't check the same card twice\n", | |
"def strategy_random(ace_location: int, max_places: int = 13) -> int:\n", | |
" \"\"\" Returns number of guesses needed to find the ace \"\"\"\n", | |
" steps = 1\n", | |
" cards = [n for n in range(max_places)]\n", | |
" #print(ace_location)\n", | |
" while True:\n", | |
" flip_card = random.choice(cards)\n", | |
" #print(\" \", flip_card, steps)\n", | |
" if flip_card == ace_location:\n", | |
" break\n", | |
" cards.remove(flip_card)\n", | |
" steps += 1\n", | |
" return steps\n", | |
"\n", | |
"\n", | |
"def strategy_left_to_right(ace_location: int) -> int:\n", | |
" \"\"\" Returns the number of guesses to find the ace \"\"\"\n", | |
" return ace_location + 1\n", | |
"\n", | |
"\n", | |
"def play_the_game(n_times: int = 1):\n", | |
" # repeat the game\n", | |
" rand_steps = 0\n", | |
" ltr_steps = 0\n", | |
" for _ in range(n_times):\n", | |
" # how does each strategy do, for each ace position, on average?\n", | |
" for ace_actual_position in range(13):\n", | |
" rand_steps += strategy_random(ace_actual_position)\n", | |
" ltr_steps += strategy_left_to_right(ace_actual_position)\n", | |
" # average steps to find the ace in each strategy\n", | |
" return rand_steps / n_times, ltr_steps / n_times\n", | |
"\n", | |
"\n", | |
"# How many repeats of the game?\n", | |
"play_the_game(1000)\n", | |
"\n", | |
"\n" | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment