diff --git a/Lab03/Lab/Lab03-2.2.ipynb b/Lab03/Lab/Lab03-2.2.ipynb new file mode 100644 index 0000000..a56638a --- /dev/null +++ b/Lab03/Lab/Lab03-2.2.ipynb @@ -0,0 +1,113 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "3503da6f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "For convergence criterion 0.01:\n", + "Average number of draws: 228.51\n", + "Standard deviation of draws: 874.1452567508447\n", + "\n", + "For convergence criterion 0.001:\n", + "Average number of draws: 1204.11\n", + "Standard deviation of draws: 8195.205337140736\n", + "\n", + "For convergence criterion 0.0001:\n", + "Average number of draws: 6670.93\n", + "Standard deviation of draws: 29216.754593641985\n", + "\n", + "For convergence criterion 1e-05:\n", + "Average number of draws: 41313.39\n", + "Standard deviation of draws: 218969.87842234815\n", + "\n" + ] + } + ], + "source": [ + "import random\n", + "import math\n", + "import numpy as np\n", + "\n", + "# Define the convergence criteria\n", + "convergence_criteria = [0.01, 0.001, 0.0001, 0.00001]\n", + "\n", + "# Function to estimate Pi given a convergence criterion\n", + "def estimate_pi(convergence_criterion):\n", + " pi = math.pi\n", + " n_draws = []\n", + "\n", + " for _ in range(10): # 10 runs\n", + " n = 0 # number of points falling in the unit circle\n", + " d = 0 # number of points falling in the unit square\n", + " \n", + " while True:\n", + " x = random.random()\n", + " y = random.random()\n", + " if x**2 + y**2 <= 1.0:\n", + " n += 1\n", + " d += 1\n", + " ratio = 4 * n / d\n", + " \n", + " # Check convergence criterion\n", + " if abs(ratio - pi) / pi <= convergence_criterion:\n", + " n_draws.append(d)\n", + " break\n", + "\n", + " return n_draws\n", + "\n", + "# Calculate statistics for each convergence criterion\n", + "for criterion in convergence_criteria:\n", + " all_draws = []\n", + "\n", + " # Perform 10 runs for the current criterion\n", + " for _ in range(10):\n", + " draws = estimate_pi(criterion)\n", + " all_draws.extend(draws)\n", + "\n", + " # Calculate and print statistics\n", + " average_draws = np.mean(all_draws)\n", + " std_draws = np.std(all_draws)\n", + " \n", + " print(f\"For convergence criterion {criterion}:\")\n", + " print(f\"Average number of draws: {average_draws}\")\n", + " print(f\"Standard deviation of draws: {std_draws}\")\n", + " print()\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "de54eb99", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.11.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Lab03/Lab/simulate_pi.ipynb b/Lab03/Lab/simulate_pi.ipynb index dc7e3a1..5491c41 100755 --- a/Lab03/Lab/simulate_pi.ipynb +++ b/Lab03/Lab/simulate_pi.ipynb @@ -2,20 +2,14 @@ "cells": [ { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ "# Simulating $\\pi$" ] }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ "The area of a circle $C$ is given as $|C| = \\pi r^2$ with $r$ as the radius.\n", "\n", @@ -34,10 +28,7 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ "We can use this relationship to simulate the ratio of the areas to generate an estimate of $\\pi$.\n", "\n", @@ -50,9 +41,6 @@ "cell_type": "code", "execution_count": 1, "metadata": { - "collapsed": false, - "deletable": true, - "editable": true, "jupyter": { "outputs_hidden": false } @@ -68,9 +56,6 @@ "cell_type": "code", "execution_count": 2, "metadata": { - "collapsed": false, - "deletable": true, - "editable": true, "jupyter": { "outputs_hidden": false } @@ -84,9 +69,6 @@ "cell_type": "code", "execution_count": 3, "metadata": { - "collapsed": false, - "deletable": true, - "editable": true, "jupyter": { "outputs_hidden": false } @@ -121,9 +103,6 @@ "cell_type": "code", "execution_count": 9, "metadata": { - "collapsed": false, - "deletable": true, - "editable": true, "jupyter": { "outputs_hidden": false } @@ -158,9 +137,6 @@ "cell_type": "code", "execution_count": 10, "metadata": { - "collapsed": false, - "deletable": true, - "editable": true, "jupyter": { "outputs_hidden": false } @@ -5824,9 +5800,6 @@ "cell_type": "code", "execution_count": 11, "metadata": { - "collapsed": false, - "deletable": true, - "editable": true, "jupyter": { "outputs_hidden": false } @@ -5863,9 +5836,6 @@ "cell_type": "code", "execution_count": 12, "metadata": { - "collapsed": false, - "deletable": true, - "editable": true, "jupyter": { "outputs_hidden": false } @@ -5897,9 +5867,6 @@ "cell_type": "code", "execution_count": 13, "metadata": { - "collapsed": false, - "deletable": true, - "editable": true, "jupyter": { "outputs_hidden": false } @@ -5937,9 +5904,6 @@ "cell_type": "code", "execution_count": 14, "metadata": { - "collapsed": false, - "deletable": true, - "editable": true, "jupyter": { "outputs_hidden": false } @@ -5980,9 +5944,6 @@ "cell_type": "code", "execution_count": 15, "metadata": { - "collapsed": false, - "deletable": true, - "editable": true, "jupyter": { "outputs_hidden": false } @@ -6018,8 +5979,6 @@ { "cell_type": "markdown", "metadata": { - "deletable": true, - "editable": true, "jupyter": { "outputs_hidden": false } @@ -6221,6 +6180,62 @@ "xf" ] }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the convergence criterion (e.g., 0.0001): 0.00001 \n", + "Enter the sentinel value for the number of draws: 100\n", + "Draw limit was exceeded.\n", + "Pi estimate: 3.28\n", + "Percentage difference: 4.405642668283338\n" + ] + } + ], + "source": [ + "import random\n", + "import math\n", + "\n", + "# Prompt user for convergence criterion\n", + "convergence_criterion = float(input(\"Enter the convergence criterion (e.g., 0.0001): \"))\n", + "\n", + "# Prompt user for sentinel value\n", + "sentinel_value = int(input(\"Enter the sentinel value for the number of draws: \"))\n", + "\n", + "# True value of Pi\n", + "true_pi = math.pi\n", + "\n", + "n = 0 # number of points falling in the unit circle\n", + "d = 0 # number of points falling in the unit square\n", + "simulating = True # use as a sentinel\n", + "\n", + "while simulating:\n", + " x = random.random()\n", + " y = random.random()\n", + " if x**2 + y**2 <= 1.0:\n", + " n += 1\n", + " d += 1\n", + " ratio = 4 * n * 1.0 / d\n", + "\n", + " # Check convergence criterion\n", + " if abs(ratio - true_pi) / true_pi <= convergence_criterion:\n", + " print(\"Convergence criterion met.\")\n", + " print(\"Pi estimate:\", ratio)\n", + " break\n", + "\n", + " # Check sentinel value\n", + " if d >= sentinel_value:\n", + " print(\"Draw limit was exceeded.\")\n", + " print(\"Pi estimate:\", ratio)\n", + " print(\"Percentage difference:\", abs(ratio - true_pi) / true_pi * 100)\n", + " break\n" + ] + }, { "cell_type": "code", "execution_count": null, @@ -6231,7 +6246,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -6245,7 +6260,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.8" + "version": "3.11.5" } }, "nbformat": 4, diff --git a/Lab03/notebook/Lab03-Operators.ipynb b/Lab03/notebook/Lab03-Operators.ipynb new file mode 100644 index 0000000..007a577 --- /dev/null +++ b/Lab03/notebook/Lab03-Operators.ipynb @@ -0,0 +1,252 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "15ed7669", + "metadata": {}, + "outputs": [], + "source": [ + "#Chapter 2 Exercises 1" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "a60e5b27", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "cannot assign to literal here. Maybe you meant '==' instead of '='? (1247378958.py, line 3)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m Cell \u001b[1;32mIn[4], line 3\u001b[1;36m\u001b[0m\n\u001b[1;33m 42=n\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m cannot assign to literal here. Maybe you meant '==' instead of '='?\n" + ] + } + ], + "source": [ + "#1 n=42 vs. 42=n\n", + "n=42\n", + "42=n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "f7e8218d", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (2068491520.py, line 2)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m Cell \u001b[1;32mIn[6], line 2\u001b[1;36m\u001b[0m\n\u001b[1;33m print(x=y=1)\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "source": [ + "#2 x=y=1\n", + "print(x=y=1)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "bc1a61ba", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid character ';' (U+FF1B) (370115192.py, line 2)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m Cell \u001b[1;32mIn[4], line 2\u001b[1;36m\u001b[0m\n\u001b[1;33m print(\"Hello, World!\");\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid character ';' (U+FF1B)\n" + ] + } + ], + "source": [ + "#3 semi-colon at the end\n", + "print(\"Hello, World!\");" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "156a03d5", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (2227484473.py, line 2)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m Cell \u001b[1;32mIn[5], line 2\u001b[1;36m\u001b[0m\n\u001b[1;33m print(\"Hello, World!\").\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "source": [ + "#4 period at the end\n", + "print(\"Hello, World!\")." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "4f1be016", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (2610192198.py, line 2)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m Cell \u001b[1;32mIn[6], line 2\u001b[1;36m\u001b[0m\n\u001b[1;33m z= x y\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "source": [ + "#5 Multiplying x and y like 'x y' in Python\n", + "z= x y\n", + "z=x*y" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "be7f577a", + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'n' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[7], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(n)\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(x,y)\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(z)\n", + "\u001b[1;31mNameError\u001b[0m: name 'n' is not defined" + ] + } + ], + "source": [ + "print(n)\n", + "print(x,y)\n", + "print(z)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "37ef7b13", + "metadata": {}, + "outputs": [], + "source": [ + "# Chapter 2 Excercise 2\n", + "import datetime" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "e72aeeed", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Volume of the sphere with radius 5: 523.5987666666666\n" + ] + } + ], + "source": [ + "#1 Volume of a Sphere\n", + "r=5\n", + "Volume = 4/3*3.1415926*r**3\n", + "print(\"Volume of the sphere with radius 5:\",Volume)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "4d0fd8fb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total wholesale cost for 60 copies: 945.4499999999999\n" + ] + } + ], + "source": [ + "#2 Total Wholesale Cost for 60 Copies\n", + "cover_price = 24.95\n", + "discount_rate = 0.40\n", + "shipping_first_copy = 3\n", + "shipping_additional_copy = 0.75\n", + "number_of_copies = 60\n", + "\n", + "discounted_price = cover_price * (1 - discount_rate)\n", + "total_wholesale_cost = (discounted_price * number_of_copies) + shipping_first_copy + (number_of_copies - 1) * shipping_additional_copy\n", + "print(\"Total wholesale cost for 60 copies:\", total_wholesale_cost)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "027b224a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Time to get home for breakfast: 08:58 PM\n" + ] + } + ], + "source": [ + "#3 time to get breakfast\n", + "start_time = datetime.datetime.strptime('6:52', '%I:%M')\n", + "easy_pace_time = (8 * 60 + 15) * 2 # 2 miles\n", + "tempo_pace_time = (7 * 60 + 12) * 3 # 3 miles\n", + "total_time = easy_pace_time + tempo_pace_time\n", + "end_time = start_time + datetime.timedelta(minutes=total_time)\n", + "print(\"Time to get home for breakfast:\", end_time.strftime('%I:%M %p'))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a0f4b1e6", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.11.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Lab03/notebook/Lab03-String.ipynb b/Lab03/notebook/Lab03-String.ipynb new file mode 100644 index 0000000..7bfae1f --- /dev/null +++ b/Lab03/notebook/Lab03-String.ipynb @@ -0,0 +1,184 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "dbe04a28", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello, world!\n", + " Hi, world! \n" + ] + } + ], + "source": [ + "#1 Example using strip and replace\n", + "text = \" Hello, world! \"\n", + "stripped_text = text.strip() \n", + "replaced_text = text.replace(\"Hello\", \"Hi\") \n", + "print(stripped_text)\n", + "print(replaced_text)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "e7cc6fea", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Count of 'a' in 'banana': 3\n" + ] + } + ], + "source": [ + "#2 Example using count\n", + "word = \"banana\"\n", + "count_of_a = word.count('a')\n", + "print(\"Count of 'a' in 'banana':\", count_of_a)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "12ebcc5f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "bnn\n", + "True\n", + "False\n" + ] + } + ], + "source": [ + "#3 \n", + "#Example using string slice with step size\n", + "fruit = 'banana'\n", + "sliced_fruit = fruit[0:5:2]\n", + "print(sliced_fruit)\n", + "\n", + "# One-line version of is_palindrome using string slice with step size\n", + "def is_palindrome(word):\n", + " return word == word[::-1]\n", + "\n", + "# Test the function\n", + "print(is_palindrome(\"radar\")) \n", + "print(is_palindrome(\"python\")) \n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "b5462687", + "metadata": {}, + "outputs": [], + "source": [ + "#4 Description of each function\n", + "def any_lowercase1(s):\n", + " for c in s:\n", + " if c.islower():\n", + " return True\n", + " else:\n", + " return False\n", + "\n", + "def any_lowercase2(s):\n", + " for c in s:\n", + " if 'c'.islower():\n", + " return 'True'\n", + " else:\n", + " return 'False'\n", + "\n", + "def any_lowercase3(s):\n", + " for c in s:\n", + " flag = c.islower()\n", + " return flag\n", + "\n", + "def any_lowercase4(s):\n", + " flag = False\n", + " for c in s:\n", + " flag = flag or c.islower()\n", + " return flag\n", + "\n", + "\n", + "def any_lowercase5(s):\n", + " for c in s:\n", + " if not c.islower():\n", + " return False\n", + " return True\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "1c6f6039", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "jolly\n", + "cubed\n" + ] + } + ], + "source": [ + "#5 Function to rotate a word\n", + "def rotate_word(word, n):\n", + " rotated_word = ''\n", + " for char in word:\n", + " if char.isalpha(): # Check if the character is a letter\n", + " base = ord('a') if char.islower() else ord('A')\n", + " rotated_word += chr((ord(char) - base + n) % 26 + base)\n", + " else:\n", + " rotated_word += char \n", + " return rotated_word\n", + "\n", + "# Test the function\n", + "print(rotate_word(\"cheer\", 7)) \n", + "print(rotate_word(\"melon\", -10)) \n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "86a400dd", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.11.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}