diff --git a/quickstarts/Code_Execution.ipynb b/quickstarts/Code_Execution.ipynb index 0382fb90a..c538a5ae9 100644 --- a/quickstarts/Code_Execution.ipynb +++ b/quickstarts/Code_Execution.ipynb @@ -57,11 +57,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "id": "6rshwediCXto" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[33mWARNING: There was an error checking the latest version of pip.\u001b[0m\u001b[33m\n", + "\u001b[0m" + ] + } + ], "source": [ "!pip install -Uq google-generativeai" ] @@ -77,7 +86,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": { "id": "OsxWu_BI50Ex" }, @@ -88,7 +97,7 @@ "'0.7.1'" ] }, - "execution_count": 3, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -121,7 +130,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "metadata": { "id": "kS3JQzazOI48" }, @@ -166,7 +175,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "metadata": { "id": "BFxIcGkxbq3_" }, @@ -219,7 +228,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 25, "metadata": { "id": "u2DRvZZde2C6" }, @@ -258,7 +267,7 @@ } ], "source": [ - "result = model.generate_content(\"What's the sum of the sum of first 200 prime numbers?\")" + "result = model.generate_content(\"What's the sum of the sum of first 200 prime numbers? Make sure you get all 200.\")" ] }, { @@ -272,7 +281,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 26, "metadata": { "id": "E1_KEd4P-_YB" }, @@ -315,7 +324,7 @@ "[['text'], ['executable_code'], ['code_execution_result'], ['text']]" ] }, - "execution_count": 9, + "execution_count": 26, "metadata": {}, "output_type": "execute_result" } @@ -329,7 +338,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 27, "metadata": { "id": "JtL3wwKJA0Jf" }, @@ -370,22 +379,22 @@ "name": "stdout", "output_type": "stream", "text": [ - "text: \"I will use Python to find the first 200 prime numbers and then calculate their sum.\\n\\n\"\n", + "text: \"I will use Python to find the first 200 prime numbers and then sum them up.\\n\\n\"\n", "\n", "\n", "executable_code {\n", " language: PYTHON\n", - " code: \"\\nimport math\\n\\ndef is_prime(n):\\n if n <= 1:\\n return False\\n for i in range(2, int(math.sqrt(n)) + 1):\\n if n % i == 0:\\n return False\\n return True\\n\\nprimes = []\\ni = 2\\nwhile len(primes) < 200:\\n if is_prime(i):\\n primes.append(i)\\n i += 1\\n\\nprint(f\\'The first 200 prime numbers are: {primes}\\')\\nprint(f\\'The sum of the first 200 prime numbers is: {sum(primes)}\\')\\n\"\n", + " code: \"\\ndef is_prime(num):\\n \\\"\\\"\\\"Checks if a number is prime.\\\"\\\"\\\"\\n if num <= 1:\\n return False\\n for i in range(2, int(num**0.5) + 1):\\n if num % i == 0:\\n return False\\n return True\\n\\nprime_numbers = []\\nnum = 2\\nwhile len(prime_numbers) < 200:\\n if is_prime(num):\\n prime_numbers.append(num)\\n num += 1\\n\\nsum_of_primes = sum(prime_numbers)\\nprint(f\\'The sum of the first 200 prime numbers is: {sum_of_primes}\\')\\n\"\n", "}\n", "\n", "\n", "code_execution_result {\n", " outcome: OUTCOME_OK\n", - " output: \"The first 200 prime numbers are: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223]\\nThe sum of the first 200 prime numbers is: 111587\\n\"\n", + " output: \"The sum of the first 200 prime numbers is: 111587\\n\"\n", "}\n", "\n", "\n", - "text: \"The sum of the first 200 prime numbers is **111587**. \\n\"\n", + "text: \"The code I used first defines a function `is_prime` that checks if a number is prime. It then initializes an empty list called `prime_numbers` and a variable `num` to 2. It then enters a `while` loop that continues until `prime_numbers` contains 200 elements. Inside the loop, it calls the `is_prime` function to check if the current `num` is prime. If it is, it adds `num` to the `prime_numbers` list. After the loop completes, the code sums all the elements in `prime_numbers` and prints the result.\\n\\nTherefore, the sum of the first 200 prime numbers is 111587. \\n\"\n", "\n", "\n" ] @@ -408,7 +417,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 28, "metadata": { "id": "Qe_aoIGoMlMc" }, @@ -449,37 +458,37 @@ "name": "stdout", "output_type": "stream", "text": [ - "I will use Python to find the first 200 prime numbers and then calculate their sum.\n", + "I will use Python to find the first 200 prime numbers and then sum them up.\n", "\n", "\n", "``` python\n", - "import math\n", - "\n", - "def is_prime(n):\n", - " if n <= 1:\n", + "def is_prime(num):\n", + " \"\"\"Checks if a number is prime.\"\"\"\n", + " if num <= 1:\n", " return False\n", - " for i in range(2, int(math.sqrt(n)) + 1):\n", - " if n % i == 0:\n", + " for i in range(2, int(num**0.5) + 1):\n", + " if num % i == 0:\n", " return False\n", " return True\n", "\n", - "primes = []\n", - "i = 2\n", - "while len(primes) < 200:\n", - " if is_prime(i):\n", - " primes.append(i)\n", - " i += 1\n", + "prime_numbers = []\n", + "num = 2\n", + "while len(prime_numbers) < 200:\n", + " if is_prime(num):\n", + " prime_numbers.append(num)\n", + " num += 1\n", "\n", - "print(f'The first 200 prime numbers are: {primes}')\n", - "print(f'The sum of the first 200 prime numbers is: {sum(primes)}')\n", + "sum_of_primes = sum(prime_numbers)\n", + "print(f'The sum of the first 200 prime numbers is: {sum_of_primes}')\n", "\n", "```\n", "```\n", - "The first 200 prime numbers are: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223]\n", "The sum of the first 200 prime numbers is: 111587\n", "\n", "```\n", - "The sum of the first 200 prime numbers is **111587**. \n", + "The code I used first defines a function `is_prime` that checks if a number is prime. It then initializes an empty list called `prime_numbers` and a variable `num` to 2. It then enters a `while` loop that continues until `prime_numbers` contains 200 elements. Inside the loop, it calls the `is_prime` function to check if the current `num` is prime. If it is, it adds `num` to the `prime_numbers` list. After the loop completes, the code sums all the elements in `prime_numbers` and prints the result.\n", + "\n", + "Therefore, the sum of the first 200 prime numbers is 111587. \n", "\n" ] } @@ -499,7 +508,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 29, "metadata": { "id": "N-c7VXWoEFQB" }, @@ -539,43 +548,43 @@ { "data": { "text/markdown": [ - "I will use Python to find the first 200 prime numbers and then calculate their sum.\n", + "I will use Python to find the first 200 prime numbers and then sum them up.\n", "\n", "\n", "``` python\n", - "import math\n", - "\n", - "def is_prime(n):\n", - " if n <= 1:\n", + "def is_prime(num):\n", + " \"\"\"Checks if a number is prime.\"\"\"\n", + " if num <= 1:\n", " return False\n", - " for i in range(2, int(math.sqrt(n)) + 1):\n", - " if n % i == 0:\n", + " for i in range(2, int(num**0.5) + 1):\n", + " if num % i == 0:\n", " return False\n", " return True\n", "\n", - "primes = []\n", - "i = 2\n", - "while len(primes) < 200:\n", - " if is_prime(i):\n", - " primes.append(i)\n", - " i += 1\n", + "prime_numbers = []\n", + "num = 2\n", + "while len(prime_numbers) < 200:\n", + " if is_prime(num):\n", + " prime_numbers.append(num)\n", + " num += 1\n", "\n", - "print(f'The first 200 prime numbers are: {primes}')\n", - "print(f'The sum of the first 200 prime numbers is: {sum(primes)}')\n", + "sum_of_primes = sum(prime_numbers)\n", + "print(f'The sum of the first 200 prime numbers is: {sum_of_primes}')\n", "\n", "```\n", "```\n", - "The first 200 prime numbers are: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223]\n", "The sum of the first 200 prime numbers is: 111587\n", "\n", "```\n", - "The sum of the first 200 prime numbers is **111587**. \n" + "The code I used first defines a function `is_prime` that checks if a number is prime. It then initializes an empty list called `prime_numbers` and a variable `num` to 2. It then enters a `while` loop that continues until `prime_numbers` contains 200 elements. Inside the loop, it calls the `is_prime` function to check if the current `num` is prime. If it is, it adds `num` to the `prime_numbers` list. After the loop completes, the code sums all the elements in `prime_numbers` and prints the result.\n", + "\n", + "Therefore, the sum of the first 200 prime numbers is 111587. \n" ], "text/plain": [ "" ] }, - "execution_count": 12, + "execution_count": 29, "metadata": {}, "output_type": "execute_result" } @@ -596,7 +605,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 30, "metadata": { "id": "DL2mRlNTF5JN" }, @@ -640,7 +649,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 31, "metadata": { "id": "fY062-nsGLBu" }, @@ -686,7 +695,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 32, "metadata": { "id": "fQY1_501GfP-" }, @@ -728,20 +737,24 @@ "text/markdown": [ "\n", "``` python\n", - "print(list('strawberry').count('r'))\n", + "print(list(str(\"strawberry\")).count(\"r\"))\n", "\n", "```\n", "```\n", "3\n", "\n", "```\n", - "There are 3 letter r's in the word strawberry. \n" + "The code counts the occurrences of the letter 'r' in the word 'strawberry' and prints the result. \n", + "\n", + "The code first converts the word 'strawberry' into a list of individual characters using `list(str(\"strawberry\"))`. Then, it uses the `count()` method to count the number of times 'r' appears in the list. \n", + "\n", + "The output of the code is `3`, indicating that there are three 'r's in the word 'strawberry'. \n" ], "text/plain": [ "" ] }, - "execution_count": 19, + "execution_count": 32, "metadata": {}, "output_type": "execute_result" } @@ -763,7 +776,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 33, "metadata": { "id": "_19QkCnQEZSu" }, @@ -807,7 +820,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 34, "metadata": { "id": "VFMAiEH_Dx6E" }, @@ -857,34 +870,31 @@ " return arr\n", "\n", "def is_sorted(arr):\n", - " for i in range(len(arr) - 1):\n", - " if arr[i] > arr[i + 1]:\n", + " for i in range(len(arr)-1):\n", + " if arr[i] > arr[i+1]:\n", " return False\n", " return True\n", "\n", "arr = [2, 34, 1, 65, 4]\n", "sorted_arr = bogo_sort(arr)\n", - "print(f'Sorted array: {sorted_arr}')\n", + "print(f'Sorted Array: {sorted_arr}')\n", "\n", "```\n", "```\n", - "Sorted array: [1, 2, 4, 34, 65]\n", + "Sorted Array: [1, 2, 4, 34, 65]\n", "\n", "```\n", - "The code defines two functions:\n", + "Bogo sort is a horribly inefficient sorting algorithm that relies on random shuffling of the list until it is sorted. It works by repeatedly shuffling the list and checking if it is sorted. If it is not, it shuffles again. This process continues until the list is sorted.\n", "\n", - "1. `bogo_sort`: This function takes a list as input and shuffles it randomly until it is sorted. The `is_sorted` function is called to check if the list is sorted.\n", - "2. `is_sorted`: This function checks if a list is sorted in ascending order by iterating through the list and comparing adjacent elements.\n", + "I ran the code you provided to implement bogo sort on the list [2, 34, 1, 65, 4]. The tool outputs show that the sorted list is [1, 2, 4, 34, 65].\n", "\n", - "The provided list `[2, 34, 1, 65, 4]` is passed to the `bogo_sort` function, which randomly shuffles the list until it is sorted in ascending order. The final sorted list is then printed as `[1, 2, 4, 34, 65]`. \n", - "\n", - "It's important to note that Bogo sort is a notoriously inefficient sorting algorithm. It has an average time complexity of O(n!), which makes it impractical for sorting even moderately sized lists. \n" + "While this code successfully sorts the list, it's important to note that bogo sort is not a practical sorting algorithm due to its extremely poor average time complexity. It is often used as a pedagogical example to illustrate the concept of randomness and the inefficiency of some sorting algorithms. \n" ], "text/plain": [ "" ] }, - "execution_count": 21, + "execution_count": 34, "metadata": {}, "output_type": "execute_result" } @@ -896,7 +906,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 35, "metadata": { "id": "ANxrYfl0Bk6T" }, @@ -948,31 +958,33 @@ " return arr, iterations\n", "\n", "def is_sorted(arr):\n", - " for i in range(len(arr) - 1):\n", - " if arr[i] > arr[i + 1]:\n", + " for i in range(len(arr)-1):\n", + " if arr[i] > arr[i+1]:\n", " return False\n", " return True\n", "\n", "arr = [2, 34, 1, 65, 4]\n", "sorted_arr, iterations = bogo_sort(arr)\n", - "print(f'Sorted array: {sorted_arr}')\n", + "print(f'Sorted Array: {sorted_arr}')\n", "print(f'Iterations: {iterations}')\n", "\n", "```\n", "```\n", - "Sorted array: [1, 2, 4, 34, 65]\n", - "Iterations: 90\n", + "Sorted Array: [1, 2, 4, 34, 65]\n", + "Iterations: 75\n", "\n", "```\n", - "The code now includes a counter `iterations` in the `bogo_sort` function. The counter is incremented every time the list is shuffled. When the list is sorted, the number of iterations is printed along with the sorted list.\n", + "I modified the `bogo_sort` function to count the number of iterations. The code now prints both the sorted array and the number of iterations it took to sort it.\n", + "\n", + "In this particular run, it took **75 iterations** to sort the list. It's important to note that the number of iterations will vary greatly for each run due to the random nature of the algorithm. \n", "\n", - "In this particular run, it took `90` iterations to sort the list. However, since Bogo Sort relies on random shuffling, the number of iterations needed to sort the list can vary significantly. It's important to note that, as we discussed previously, this is a very inefficient algorithm, and for larger lists, the number of iterations could be extremely high. \n" + "Bogo sort is notorious for its extremely high average time complexity, making it impractical for any real-world sorting tasks.\n" ], "text/plain": [ "" ] }, - "execution_count": 22, + "execution_count": 35, "metadata": {}, "output_type": "execute_result" } @@ -1002,7 +1014,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 36, "metadata": { "id": "bDg1bDRpAnFR" }, @@ -1045,7 +1057,7 @@ "text": [ " % Total % Received % Xferd Average Speed Time Time Time Current\n", " Dload Upload Total Spent Left Speed\n", - "100 24719 100 24719 0 0 394k 0 --:--:-- --:--:-- --:--:-- 395k\n" + "100 24719 100 24719 0 0 500k 0 --:--:-- --:--:-- --:--:-- 502k\n" ] } ], @@ -1055,7 +1067,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 37, "metadata": { "id": "1Uhq7nZPEsvO" }, @@ -1099,7 +1111,7 @@ "" ] }, - "execution_count": 24, + "execution_count": 37, "metadata": {}, "output_type": "execute_result" } @@ -1112,7 +1124,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 38, "metadata": { "id": "2S0WIddTEzUm" }, @@ -1152,76 +1164,54 @@ { "data": { "text/markdown": [ - "I can help you with that! \n", - "\n", - "It turns out that switching doors does significantly increase your chances of winning! Here's why, and a simulation of the Monty Hall problem in Python that will demonstrate.\n", - "\n", - "The Monty Hall problem is a bit counterintuitive, but here's why switching doors works:\n", - "\n", - "1. **Initial Choice:** When you first choose a door, you have a 1/3 chance of picking the door with the car and a 2/3 chance of picking a door with a goat.\n", - "\n", - "2. **Host's Action:** The host *always* reveals a goat behind one of the doors you didn't choose. This doesn't change the initial probabilities. Your chosen door still has a 1/3 chance of having the car.\n", - "\n", - "3. **The Switch:** The key is that the remaining unopened door *now* concentrates the remaining 2/3 probability. The host's action has effectively transferred the probability from the revealed goat door to the other unopened door.\n", - "\n", - "Now, let's run a Python simulation to see this in action:\n", + "I can help you with that. I will use Python to simulate the Monty Hall problem 1,000 times. I will simulate the game by randomly assigning the car and goats behind the doors, then allowing the player to choose a door. The host will then reveal a goat behind one of the unchosen doors, and the player will have the option to switch doors. I will record whether the player wins or loses for each trial. I will then calculate the percentage of wins for each strategy (switching and not switching) to show which strategy is better.\n", "\n", "\n", "``` python\n", "import random\n", "\n", - "def play_monty_hall(switch_doors):\n", - " # 1. Choose a door (0, 1, or 2)\n", - " chosen_door = random.randint(0, 2)\n", + "def monty_hall_simulation(num_trials):\n", + " wins_switch = 0\n", + " wins_stay = 0\n", + " for _ in range(num_trials):\n", + " # Randomly assign the car and goats\n", + " doors = ['goat', 'goat', 'car']\n", + " random.shuffle(doors)\n", "\n", - " # 2. Determine where the car is\n", - " car_door = random.randint(0, 2)\n", + " # Player chooses a door\n", + " player_choice = random.randint(0, 2)\n", "\n", - " # 3. Host reveals a goat\n", - " if chosen_door == car_door:\n", - " # Host reveals a random goat\n", - " revealed_goat = random.choice([door for door in range(3) if door != chosen_door])\n", - " else:\n", - " # Host reveals the goat behind the other unchosen door\n", - " revealed_goat = [door for door in range(3) if door != chosen_door and door != car_door][0]\n", + " # Host reveals a goat\n", + " host_reveal = [i for i in range(3) if i != player_choice and doors[i] == 'goat'][0]\n", "\n", - " # 4. Switch doors?\n", - " if switch_doors:\n", - " remaining_door = [door for door in range(3) if door != chosen_door and door != revealed_goat][0]\n", - " chosen_door = remaining_door\n", + " # Player switches doors\n", + " switch_choice = [i for i in range(3) if i != player_choice and i != host_reveal][0]\n", "\n", - " # 5. Check if you won\n", - " return chosen_door == car_door\n", + " # Determine if the player wins\n", + " if doors[player_choice] == 'car':\n", + " wins_stay += 1\n", + " if doors[switch_choice] == 'car':\n", + " wins_switch += 1\n", "\n", - "# Run the simulation\n", - "num_trials = 1000\n", - "switch_wins = 0\n", - "stay_wins = 0\n", + " return wins_stay / num_trials, wins_switch / num_trials\n", "\n", - "for _ in range(num_trials):\n", - " if play_monty_hall(True):\n", - " switch_wins += 1\n", - " if play_monty_hall(False):\n", - " stay_wins += 1\n", - "\n", - "print(f\"Switching doors: {switch_wins/num_trials:.2%}\")\n", - "print(f\"Staying with initial choice: {stay_wins/num_trials:.2%}\")\n", + "wins_stay, wins_switch = monty_hall_simulation(1000)\n", + "print(f'Percentage of wins staying: {wins_stay * 100:.2f}%')\n", + "print(f'Percentage of wins switching: {wins_switch * 100:.2f}%')\n", "\n", "```\n", "```\n", - "Switching doors: 68.50%\n", - "Staying with initial choice: 34.80%\n", + "Percentage of wins staying: 33.90%\n", + "Percentage of wins switching: 66.10%\n", "\n", "```\n", - "The simulation shows that switching doors wins about 68.5% of the time, while staying with your initial choice wins only about 34.8% of the time. This is consistent with the theoretical probability of switching doors winning (which is 2/3 or about 66.7%).\n", - "\n", - "This simulation confirms that switching doors in the Monty Hall problem is the more advantageous strategy. \n" + "The simulation shows that switching doors results in a win about 66.1% of the time, while staying with the original choice results in a win about 33.9% of the time. This confirms the counterintuitive result of the Monty Hall Problem: switching doors is the better strategy. \n" ], "text/plain": [ "" ] }, - "execution_count": 25, + "execution_count": 38, "metadata": {}, "output_type": "execute_result" } @@ -1268,7 +1258,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 39, "metadata": { "id": "J9s8DPy7GuKN" }, @@ -1309,49 +1299,28 @@ "name": "stdout", "output_type": "stream", "text": [ - "text: \"I\"\n", - "\n", - "----------------------------------------\n", - "text: \" can help you run a simulation of the Monty Hall problem to illustrate the best strategy\"\n", - "\n", - "----------------------------------------\n", - "text: \".\\n\\nHere\\'s how the simulation works:\\n\\n1. **Initialize Variables\"\n", - "\n", - "----------------------------------------\n", - "text: \":** We\\'ll track the number of wins for switching doors and staying with the original choice.\\n2. **Simulate Game:** For each trial:\\n\"\n", - "\n", - "----------------------------------------\n", - "text: \" - Randomly assign the car and goats behind the doors.\\n - The player chooses a door at random.\\n - The host reveals a goat\"\n", - "\n", - "----------------------------------------\n", - "text: \" behind a different door.\\n - The player either stays with their original choice or switches.\\n - The results are recorded based on the player\\'s decision and whether they won or lost.\\n3. **Calculate Results:** After\"\n", - "\n", - "----------------------------------------\n", - "text: \" a large number of trials (in this case, 1,000), we\\'ll calculate the winning percentages for both strategies.\\n\\n\"\n", - "\n", - "----------------------------------------\n", "executable_code {\n", " language: PYTHON\n", - " code: \"\\nimport random\\n\\ndef monty_hall_simulation(num_trials):\\n wins_switch = 0\\n wins_stay = 0\\n\\n for _ in range(num_trials):\\n # Randomly assign car and goats\\n doors = [\\'goat\\', \\'goat\\', \\'car\\']\\n random.shuffle(doors)\\n\\n # Player chooses a door\\n player_choice = random.randint(0, 2)\\n\\n # Host reveals a goat\\n host_choice = 0\\n while host_choice == player_choice or doors[host_choice] == \\'car\\':\\n host_choice = random.randint(0, 2)\\n\\n # Player switches or stays\\n stay_choice = player_choice\\n switch_choice = 0\\n while switch_choice == stay_choice or switch_choice == host_choice:\\n switch_choice = random.randint(0, 2)\\n\\n # Determine if the player wins or loses\\n if doors[stay_choice] == \\'car\\':\\n wins_stay += 1\\n if doors[switch_choice] == \\'car\\':\\n wins_switch += 1\\n\\n return wins_stay, wins_switch\\n\\n# Run the simulation\\nnum_trials = 1000\\nwins_stay, wins_switch = monty_hall_simulation(num_trials)\\n\\n# Print the results\\nprint(f\\\"Staying with original choice: Won {wins_stay} out of {num_trials} trials ({wins_stay / num_trials * 100:.2f}%)\\\")\\nprint(f\\\"Switching doors: Won {wins_switch} out of {num_trials} trials ({wins_switch / num_trials * 100:.2f}%)\\\")\\n\"\n", + " code: \"\\nimport random\\n\\ndef monty_hall_simulation(trials):\\n wins_staying = 0\\n wins_switching = 0\\n\\n for _ in range(trials):\\n # Initialize doors\\n doors = [0, 1, 2]\\n car_door = random.choice(doors)\\n\\n # Player picks a door\\n player_choice = random.choice(doors)\\n\\n # Host reveals a goat\\n remaining_doors = [door for door in doors if door != car_door and door != player_choice]\\n host_revealed_door = random.choice(remaining_doors)\\n\\n # Player switches or stays\\n if player_choice == car_door:\\n wins_staying += 1\\n else:\\n wins_switching += 1\\n\\n return wins_staying, wins_switching\\n\\nwins_staying, wins_switching = monty_hall_simulation(1000)\\n\\nprint(f\\'Wins staying: {wins_staying}\\')\\nprint(f\\'Wins switching: {wins_switching}\\')\\n\"\n", "}\n", "\n", "----------------------------------------\n", "code_execution_result {\n", " outcome: OUTCOME_OK\n", - " output: \"Staying with original choice: Won 351 out of 1000 trials (35.10%)\\nSwitching doors: Won 649 out of 1000 trials (64.90%)\\n\"\n", + " output: \"Wins staying: 326\\nWins switching: 674\\n\"\n", "}\n", "\n", "----------------------------------------\n", - "text: \"The\"\n", + "text: \"I\"\n", "\n", "----------------------------------------\n", - "text: \" simulation shows that you\\'re much more likely to win if you switch doors (\"\n", + "text: \" ran the simulation 1,000 times. The results show that the\"\n", "\n", "----------------------------------------\n", - "text: \"64.90% win rate) compared to staying with your original choice\"\n", + "text: \" player won 326 times when they stayed with their initial choice and \"\n", "\n", "----------------------------------------\n", - "text: \" (35.10% win rate). This demonstrates that switching doors is indeed the better strategy in the Monty Hall Problem. \\n\"\n", + "text: \"674 times when they switched. This clearly shows that switching doors is the better strategy in the Monty Hall problem. \\n\"\n", "\n", "----------------------------------------\n" ] @@ -1375,7 +1344,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 40, "metadata": { "id": "yjUSubFXiaEP" }, @@ -1416,22 +1385,19 @@ "name": "stdout", "output_type": "stream", "text": [ - "text: \"I can help you run a simulation of the Monty Hall problem to illustrate the best strategy.\\n\\nHere\\'s how the simulation works:\\n\\n1. **Initialize Variables:** We\\'ll track the number of wins for switching doors and staying with the original choice.\\n2. **Simulate Game:** For each trial:\\n - Randomly assign the car and goats behind the doors.\\n - The player chooses a door at random.\\n - The host reveals a goat behind a different door.\\n - The player either stays with their original choice or switches.\\n - The results are recorded based on the player\\'s decision and whether they won or lost.\\n3. **Calculate Results:** After a large number of trials (in this case, 1,000), we\\'ll calculate the winning percentages for both strategies.\\n\\n\"\n", - "\n", - "----------------------------------------\n", "executable_code {\n", " language: PYTHON\n", - " code: \"\\nimport random\\n\\ndef monty_hall_simulation(num_trials):\\n wins_switch = 0\\n wins_stay = 0\\n\\n for _ in range(num_trials):\\n # Randomly assign car and goats\\n doors = [\\'goat\\', \\'goat\\', \\'car\\']\\n random.shuffle(doors)\\n\\n # Player chooses a door\\n player_choice = random.randint(0, 2)\\n\\n # Host reveals a goat\\n host_choice = 0\\n while host_choice == player_choice or doors[host_choice] == \\'car\\':\\n host_choice = random.randint(0, 2)\\n\\n # Player switches or stays\\n stay_choice = player_choice\\n switch_choice = 0\\n while switch_choice == stay_choice or switch_choice == host_choice:\\n switch_choice = random.randint(0, 2)\\n\\n # Determine if the player wins or loses\\n if doors[stay_choice] == \\'car\\':\\n wins_stay += 1\\n if doors[switch_choice] == \\'car\\':\\n wins_switch += 1\\n\\n return wins_stay, wins_switch\\n\\n# Run the simulation\\nnum_trials = 1000\\nwins_stay, wins_switch = monty_hall_simulation(num_trials)\\n\\n# Print the results\\nprint(f\\\"Staying with original choice: Won {wins_stay} out of {num_trials} trials ({wins_stay / num_trials * 100:.2f}%)\\\")\\nprint(f\\\"Switching doors: Won {wins_switch} out of {num_trials} trials ({wins_switch / num_trials * 100:.2f}%)\\\")\\n\"\n", + " code: \"\\nimport random\\n\\ndef monty_hall_simulation(trials):\\n wins_staying = 0\\n wins_switching = 0\\n\\n for _ in range(trials):\\n # Initialize doors\\n doors = [0, 1, 2]\\n car_door = random.choice(doors)\\n\\n # Player picks a door\\n player_choice = random.choice(doors)\\n\\n # Host reveals a goat\\n remaining_doors = [door for door in doors if door != car_door and door != player_choice]\\n host_revealed_door = random.choice(remaining_doors)\\n\\n # Player switches or stays\\n if player_choice == car_door:\\n wins_staying += 1\\n else:\\n wins_switching += 1\\n\\n return wins_staying, wins_switching\\n\\nwins_staying, wins_switching = monty_hall_simulation(1000)\\n\\nprint(f\\'Wins staying: {wins_staying}\\')\\nprint(f\\'Wins switching: {wins_switching}\\')\\n\"\n", "}\n", "\n", "----------------------------------------\n", "code_execution_result {\n", " outcome: OUTCOME_OK\n", - " output: \"Staying with original choice: Won 351 out of 1000 trials (35.10%)\\nSwitching doors: Won 649 out of 1000 trials (64.90%)\\n\"\n", + " output: \"Wins staying: 326\\nWins switching: 674\\n\"\n", "}\n", "\n", "----------------------------------------\n", - "text: \"The simulation shows that you\\'re much more likely to win if you switch doors (64.90% win rate) compared to staying with your original choice (35.10% win rate). This demonstrates that switching doors is indeed the better strategy in the Monty Hall Problem. \\n\"\n", + "text: \"I ran the simulation 1,000 times. The results show that the player won 326 times when they stayed with their initial choice and 674 times when they switched. This clearly shows that switching doors is the better strategy in the Monty Hall problem. \\n\"\n", "\n", "----------------------------------------\n" ] @@ -1445,7 +1411,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 41, "metadata": { "id": "3sJ7LTpVFsqM" }, @@ -1486,69 +1452,45 @@ "name": "stdout", "output_type": "stream", "text": [ - "I can help you run a simulation of the Monty Hall problem to illustrate the best strategy.\n", - "\n", - "Here's how the simulation works:\n", - "\n", - "1. **Initialize Variables:** We'll track the number of wins for switching doors and staying with the original choice.\n", - "2. **Simulate Game:** For each trial:\n", - " - Randomly assign the car and goats behind the doors.\n", - " - The player chooses a door at random.\n", - " - The host reveals a goat behind a different door.\n", - " - The player either stays with their original choice or switches.\n", - " - The results are recorded based on the player's decision and whether they won or lost.\n", - "3. **Calculate Results:** After a large number of trials (in this case, 1,000), we'll calculate the winning percentages for both strategies.\n", - "\n", - "\n", "``` python\n", "import random\n", "\n", - "def monty_hall_simulation(num_trials):\n", - " wins_switch = 0\n", - " wins_stay = 0\n", - "\n", - " for _ in range(num_trials):\n", - " # Randomly assign car and goats\n", - " doors = ['goat', 'goat', 'car']\n", - " random.shuffle(doors)\n", + "def monty_hall_simulation(trials):\n", + " wins_staying = 0\n", + " wins_switching = 0\n", "\n", - " # Player chooses a door\n", - " player_choice = random.randint(0, 2)\n", + " for _ in range(trials):\n", + " # Initialize doors\n", + " doors = [0, 1, 2]\n", + " car_door = random.choice(doors)\n", "\n", - " # Host reveals a goat\n", - " host_choice = 0\n", - " while host_choice == player_choice or doors[host_choice] == 'car':\n", - " host_choice = random.randint(0, 2)\n", + " # Player picks a door\n", + " player_choice = random.choice(doors)\n", "\n", - " # Player switches or stays\n", - " stay_choice = player_choice\n", - " switch_choice = 0\n", - " while switch_choice == stay_choice or switch_choice == host_choice:\n", - " switch_choice = random.randint(0, 2)\n", + " # Host reveals a goat\n", + " remaining_doors = [door for door in doors if door != car_door and door != player_choice]\n", + " host_revealed_door = random.choice(remaining_doors)\n", "\n", - " # Determine if the player wins or loses\n", - " if doors[stay_choice] == 'car':\n", - " wins_stay += 1\n", - " if doors[switch_choice] == 'car':\n", - " wins_switch += 1\n", + " # Player switches or stays\n", + " if player_choice == car_door:\n", + " wins_staying += 1\n", + " else:\n", + " wins_switching += 1\n", "\n", - " return wins_stay, wins_switch\n", + " return wins_staying, wins_switching\n", "\n", - "# Run the simulation\n", - "num_trials = 1000\n", - "wins_stay, wins_switch = monty_hall_simulation(num_trials)\n", + "wins_staying, wins_switching = monty_hall_simulation(1000)\n", "\n", - "# Print the results\n", - "print(f\"Staying with original choice: Won {wins_stay} out of {num_trials} trials ({wins_stay / num_trials * 100:.2f}%)\")\n", - "print(f\"Switching doors: Won {wins_switch} out of {num_trials} trials ({wins_switch / num_trials * 100:.2f}%)\")\n", + "print(f'Wins staying: {wins_staying}')\n", + "print(f'Wins switching: {wins_switching}')\n", "\n", "```\n", "```\n", - "Staying with original choice: Won 351 out of 1000 trials (35.10%)\n", - "Switching doors: Won 649 out of 1000 trials (64.90%)\n", + "Wins staying: 326\n", + "Wins switching: 674\n", "\n", "```\n", - "The simulation shows that you're much more likely to win if you switch doors (64.90% win rate) compared to staying with your original choice (35.10% win rate). This demonstrates that switching doors is indeed the better strategy in the Monty Hall Problem. \n", + "I ran the simulation 1,000 times. The results show that the player won 326 times when they stayed with their initial choice and 674 times when they switched. This clearly shows that switching doors is the better strategy in the Monty Hall problem. \n", "\n" ] }