diff --git a/Lab02/Lab02.py b/Lab02/Lab02.py new file mode 100644 index 0000000..8214762 --- /dev/null +++ b/Lab02/Lab02.py @@ -0,0 +1,3 @@ +for i in range(101): + if i % 2 == 1: + print(i) \ No newline at end of file diff --git a/Lab03/Lab03-1.ipynb b/Lab03/Lab03-1.ipynb new file mode 100644 index 0000000..f3c9d2a --- /dev/null +++ b/Lab03/Lab03-1.ipynb @@ -0,0 +1,442 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "a5416fc7-6018-4015-a710-7130ad61fc37", + "metadata": {}, + "source": [ + "Lab03 – Chapter 2" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "6f5f8fc7-9d0a-4185-a807-c44c86e42358", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "cannot assign to literal here. Maybe you meant '==' instead of '='? (3090476294.py, line 1)", + "output_type": "error", + "traceback": [ + " \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[2]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[31m \u001b[39m\u001b[31m42 = n\u001b[39m\n ^\n\u001b[31mSyntaxError\u001b[39m\u001b[31m:\u001b[39m cannot assign to literal here. Maybe you meant '==' instead of '='?\n" + ] + } + ], + "source": [ + "42 = n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "25d708a6-621f-43f2-aeee-9d372691977e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "1\n" + ] + } + ], + "source": [ + "x = y = 1\n", + "print(x)\n", + "print(y)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "33fe4efd-8ea3-4046-af52-d26e029dd5a7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n" + ] + } + ], + "source": [ + "x = 5;\n", + "print(x);" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "90770035-1f05-4fe5-943c-872cdd63b7ff", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5.0\n" + ] + } + ], + "source": [ + "x = 5.\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "55ab572d-b322-4b44-a603-937de1c18ebf", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (760919749.py, line 3)", + "output_type": "error", + "traceback": [ + " \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[9]\u001b[39m\u001b[32m, line 3\u001b[39m\n\u001b[31m \u001b[39m\u001b[31mx y\u001b[39m\n ^\n\u001b[31mSyntaxError\u001b[39m\u001b[31m:\u001b[39m invalid syntax\n" + ] + } + ], + "source": [ + "x = 2\n", + "y = 3\n", + "x y" + ] + }, + { + "cell_type": "markdown", + "id": "ecb2ab26-3fc7-47ed-9d10-eb05d2cf98c6", + "metadata": {}, + "source": [ + "exercise 2" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "b6ef63e3-6ca9-4cf8-bfca-54dd4c1e7b5a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "523.5987755982989" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import math\n", + "\n", + "r = 5\n", + "volume = 4/3 * math.pi * r**3\n", + "volume" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "580f407d-6c66-4346-866e-9ba5d665e086", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "945.4499999999999" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cover_price = 24.95\n", + "discount = 0.4\n", + "shipping_first = 3\n", + "shipping_additional = 0.75\n", + "copies = 60\n", + "\n", + "book_price = cover_price * (1 - discount)\n", + "shipping = shipping_first + (copies - 1) * shipping_additional\n", + "total_cost = copies * book_price + shipping\n", + "\n", + "total_cost" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "bffaf732-4504-48b5-8939-87617ecc5480", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "38.1" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "easy_pace = 8 + 15/60\n", + "tempo_pace = 7 + 12/60\n", + "\n", + "total_minutes = (\n", + " 1 * easy_pace +\n", + " 3 * tempo_pace +\n", + " 1 * easy_pace\n", + ")\n", + "\n", + "total_minutes" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "e97c5dde-1800-4d02-9296-965190f03db2", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(7.0, 30.099999999999994)" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "start_hour = 6\n", + "start_minute = 52\n", + "\n", + "end_minutes = start_minute + total_minutes\n", + "end_hour = start_hour + end_minutes // 60\n", + "end_minute = end_minutes % 60\n", + "\n", + "end_hour, end_minute" + ] + }, + { + "cell_type": "markdown", + "id": "d368f5d5-347d-4162-8d4c-b96e287979d5", + "metadata": {}, + "source": [ + "Lab03-Chapter 8" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "6d493eab-689f-4e96-b98e-9597dac25f6d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'banana'.count('a')" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "cfdd2f77-be34-4128-8f9c-3849519db00c", + "metadata": {}, + "outputs": [], + "source": [ + "def is_palindrome(s):\n", + " return s == s[::-1]" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "acc3feb4-a7bf-4afe-8928-e88302040f3f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "is_palindrome(\"radar\")" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "b79be1ea-1acf-484f-bb18-ccb1b7e5adba", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "is_palindrome(\"banana\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ed11e28b-1476-469d-a018-226e4c4757e8", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "cd62313b-304a-4809-8ce5-33adbd5c5595", + "metadata": {}, + "outputs": [], + "source": [ + "def rotate_letter(c, n):\n", + " if not c.isalpha():\n", + " return c\n", + "\n", + " if c.islower():\n", + " start = ord('a')\n", + " else:\n", + " start = ord('A')\n", + "\n", + " return chr((ord(c) - start + n) % 26 + start)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "1fb5044b-dca0-4cd3-978d-d80bab31f75b", + "metadata": {}, + "outputs": [], + "source": [ + "def rotate_word(s, n):\n", + " result = ''\n", + " for c in s:\n", + " result += rotate_letter(c, n)\n", + " return result" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "2e825fc1-68d4-4a68-a1e2-a0180021c8ec", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'jolly'" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rotate_word(\"cheer\", 7)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "94caefe8-9cb6-4c80-b870-0c0daa66fc72", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'cubed'" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rotate_word(\"melon\", -10)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "6dfe10e0-bcc6-46a9-ae28-6255915ed33f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'HAL'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rotate_word(\"IBM\", -1)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:base] *", + "language": "python", + "name": "conda-base-py" + }, + "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.13.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Lab03/Lab03-2.1.ipynb b/Lab03/Lab03-2.1.ipynb new file mode 100644 index 0000000..9e731b9 --- /dev/null +++ b/Lab03/Lab03-2.1.ipynb @@ -0,0 +1,135 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "b3c4070d-5221-4d6c-8862-42de7221a12d", + "metadata": {}, + "source": [ + "Lab03-2" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "8d0b1593-db6f-4e5a-b311-6ce17a57ef47", + "metadata": {}, + "outputs": [], + "source": [ + "import random\n", + "import math" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "f7f85685-0140-4a01-8e8f-03291537952f", + "metadata": {}, + "outputs": [], + "source": [ + "def estimate_pi_until_stop(convergence, sentinel):\n", + " inside = 0\n", + " draws = 0\n", + " while True:\n", + " draws += 1\n", + " x = random.uniform(-1, 1)\n", + " y = random.uniform(-1, 1)\n", + " if x*x + y*y <= 1:\n", + " inside += 1\n", + " pi_estimate = 4 * inside / draws\n", + " percent_diff = abs(pi_estimate - math.pi) / math.pi\n", + " if percent_diff < convergence:\n", + " stop_reason = \"converged\"\n", + " break\n", + " if draws >= sentinel:\n", + " stop_reason = \"draw limit exceeded\"\n", + " break\n", + " return pi_estimate, draws, percent_diff, stop_reason" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "1165cb86-6d82-4afc-bec2-6ac54f30acdd", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter convergence criterion (e.g. 0.001): 0.001\n", + "Enter maximum number of draws (sentinel) (e.g. 100000): 100000\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You entered:\n", + "convergence = 0.001\n", + "sentinel = 100000\n" + ] + } + ], + "source": [ + "convergence = float(input(\"Enter convergence criterion (e.g. 0.001): \"))\n", + "sentinel = int(input(\"Enter maximum number of draws (sentinel) (e.g. 100000): \"))\n", + "\n", + "print(\"You entered:\")\n", + "print(\"convergence =\", convergence)\n", + "print(\"sentinel =\", sentinel)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "ea262146-9f5f-49a8-bdd4-637e7eabf62b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "--- Result ---\n", + "Stop reason: converged\n", + "Estimated pi: 3.142857142857143\n", + "True pi : 3.141592653589793\n", + "Draws used : 28\n", + "Percent diff: 0.0004024994347707008\n" + ] + } + ], + "source": [ + "pi_est, draws_used, pct_diff, reason = estimate_pi_until_stop(convergence, sentinel)\n", + "\n", + "print(\"\\n--- Result ---\")\n", + "print(\"Stop reason:\", reason)\n", + "print(\"Estimated pi:\", pi_est)\n", + "print(\"True pi :\", math.pi)\n", + "print(\"Draws used :\", draws_used)\n", + "print(\"Percent diff:\", pct_diff)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:base] *", + "language": "python", + "name": "conda-base-py" + }, + "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.13.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Lab03/Lab03-2.2.ipynb b/Lab03/Lab03-2.2.ipynb new file mode 100644 index 0000000..2a8f33c --- /dev/null +++ b/Lab03/Lab03-2.2.ipynb @@ -0,0 +1,135 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "92adb31b-f185-4cd0-9cf7-ded77f0401cc", + "metadata": {}, + "outputs": [], + "source": [ + "import random\n", + "import math\n", + "import statistics" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "6f0d6281-24fe-4acf-8009-f32e12e1d9c2", + "metadata": {}, + "outputs": [], + "source": [ + "def estimate_pi_until_converged(convergence):\n", + " inside = 0\n", + " draws = 0\n", + " \n", + " while True:\n", + " draws += 1\n", + " x = random.uniform(-1, 1)\n", + " y = random.uniform(-1, 1)\n", + " \n", + " if x*x + y*y <= 1:\n", + " inside += 1\n", + " \n", + " pi_estimate = 4 * inside / draws\n", + " percent_diff = abs(pi_estimate - math.pi) / math.pi\n", + " \n", + " if percent_diff < convergence:\n", + " break\n", + " \n", + " return pi_estimate, draws, percent_diff" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "b93bd054-de17-4fc0-a78b-2d106ebdbe37", + "metadata": {}, + "outputs": [], + "source": [ + "convergences = [0.01, 0.001, 0.0001, 0.00001]\n", + "runs_per_setting = 10\n", + "\n", + "results = {}\n", + "\n", + "for conv in convergences:\n", + " draws_list = []\n", + " \n", + " for _ in range(runs_per_setting):\n", + " pi_est, draws_used, pct_diff = estimate_pi_until_converged(conv)\n", + " draws_list.append(draws_used)\n", + " \n", + " results[conv] = draws_list" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "2d8d452b-d4c4-4c7d-925d-948c06329c4d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "--- Summary ---\n", + "\n", + "Convergence: 0.01\n", + "Draws (10 runs): [9, 9, 23, 43, 9, 14, 9, 28, 23, 27]\n", + "Average draws : 19.4\n", + "Std dev draws : 11.432896007185192\n", + "\n", + "Convergence: 0.001\n", + "Draws (10 runs): [28, 42, 9045, 28, 404, 6598, 14, 14, 14, 14]\n", + "Average draws : 1620.1\n", + "Std dev draws : 3321.072800232546\n", + "\n", + "Convergence: 0.0001\n", + "Draws (10 runs): [2801, 219, 559, 2437, 438, 1165, 191, 396, 438, 1053]\n", + "Average draws : 969.7\n", + "Std dev draws : 930.0448077616714\n", + "\n", + "Convergence: 1e-05\n", + "Draws (10 runs): [1123, 30577, 11244, 3327, 2027, 7218, 890, 890, 452, 1356]\n", + "Average draws : 5910.4\n", + "Std dev draws : 9334.629281456346\n" + ] + } + ], + "source": [ + "print(\"--- Summary ---\")\n", + "\n", + "for conv, draws_list in results.items():\n", + " avg_draws = statistics.mean(draws_list)\n", + " \n", + " sd_draws = statistics.stdev(draws_list)\n", + " \n", + " print(f\"\\nConvergence: {conv}\")\n", + " print(\"Draws (10 runs):\", draws_list)\n", + " print(\"Average draws :\", avg_draws)\n", + " print(\"Std dev draws :\", sd_draws)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:base] *", + "language": "python", + "name": "conda-base-py" + }, + "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.13.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}