Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lab02+Lab03 exercises #40

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lab02.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Print all odd numbers between 0 and 100
for num in range(1, 101, 2):
print(num)
120 changes: 120 additions & 0 deletions Lab03-2.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 9,
"id": "59e7cbcd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the convergence criterion (e.g., 0.01 for 1%): 0.0001\n",
"Enter the sentinel value (maximum number of draws): 50000000\n",
"Convergence achieved with 727 draws. Estimated Pi: 3.1416781292984868\n"
]
}
],
"source": [
"import random\n",
"import math\n",
"\n",
"# User inputs for convergence criterion and sentinel value\n",
"convergence_criterion = float(input(\"Enter the convergence criterion (e.g., 0.01 for 1%): \"))\n",
"sentinel_value = int(input(\"Enter the sentinel value (maximum number of draws): \"))\n",
"\n",
"pi = math.pi # The true value of Pi for comparison\n",
"n = 0 # Number of points inside the unit circle\n",
"d = 0 # Total number of points generated\n",
"ratios = [] # To store the estimates of Pi after each iteration for plotting, if needed\n",
"\n",
"# Simulation loop\n",
"while True:\n",
" x, y = random.random(), random.random() # Generate random point\n",
" if x**2 + y**2 <= 1:\n",
" n += 1 # Increment count of points inside the unit circle\n",
" d += 1 # Increment total count of points\n",
" estimated_pi = 4 * n / d # Estimate of Pi\n",
" ratios.append(estimated_pi) # For plotting purposes\n",
" \n",
" # Check if convergence criterion is met\n",
" if abs(estimated_pi - pi) / pi <= convergence_criterion:\n",
" print(f\"Convergence achieved with {d} draws. Estimated Pi: {estimated_pi}\")\n",
" break\n",
" # Check if sentinel value is reached\n",
" if d >= sentinel_value:\n",
" print(f\"Sentinel value reached with {d} draws. Estimated Pi: {estimated_pi}\")\n",
" print(f\"Percentage difference from true Pi: {abs(estimated_pi - pi) / pi * 100}%\")\n",
" break\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "2b4546f9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Convergence Criterion: 0.01, Average Draws: 294.50, Standard Deviation: 425.72\n",
"Convergence Criterion: 0.001, Average Draws: 12031.20, Standard Deviation: 35729.05\n",
"Convergence Criterion: 0.0001, Average Draws: 3085.20, Standard Deviation: 4588.12\n",
"Convergence Criterion: 1e-05, Average Draws: 52795.00, Standard Deviation: 150229.89\n"
]
}
],
"source": [
"import random\n",
"import math\n",
"import numpy as np\n",
"\n",
"# Setting different convergence criteria\n",
"convergence_criteria = [0.01, 0.001, 0.0001, 0.00001]\n",
"\n",
"def estimate_pi(convergence_criterion):\n",
" n = 0 \n",
" d = 0 \n",
" while True:\n",
" x, y = random.random(), random.random()\n",
" if x**2 + y**2 <= 1:\n",
" n += 1\n",
" d += 1\n",
" pi_estimate = 4 * n / d\n",
" if d > 1 and abs(pi_estimate - math.pi) / math.pi < convergence_criterion:\n",
" break\n",
" return d \n",
"\n",
"# main program\n",
"for criterion in convergence_criteria:\n",
" draws = [estimate_pi(criterion) for _ in range(10)] \n",
" avg_draws = np.mean(draws) \n",
" std_draws = np.std(draws) \n",
" print(f\"Convergence Criterion: {criterion}, Average Draws: {avg_draws:.2f}, Standard Deviation: {std_draws:.2f}\")\n"
]
}
],
"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.9.16"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
180 changes: 180 additions & 0 deletions Lab03_chapter2.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "e5ead4f2",
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "cannot assign to literal (3090476294.py, line 1)",
"output_type": "error",
"traceback": [
"\u001b[1;36m File \u001b[1;32m\"C:\\Users\\haruhi\\AppData\\Local\\Temp\\ipykernel_12348\\3090476294.py\"\u001b[1;36m, line \u001b[1;32m1\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\n"
]
}
],
"source": [
"42 = n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "1a0220b3",
"metadata": {},
"outputs": [],
"source": [
"x = y = 1"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ad8b642b",
"metadata": {},
"outputs": [],
"source": [
"first = 'throat';"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "cd7062c8",
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (3550435703.py, line 1)",
"output_type": "error",
"traceback": [
"\u001b[1;36m File \u001b[1;32m\"C:\\Users\\haruhi\\AppData\\Local\\Temp\\ipykernel_12348\\3550435703.py\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m a = '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": [
"a = 'hello world'."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "43ef3a4a",
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'xy' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_12348\\3424147140.py\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mxy\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mNameError\u001b[0m: name 'xy' is not defined"
]
}
],
"source": [
"xy"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "9d572670",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"523.5987755982989\n"
]
}
],
"source": [
"import math\n",
"r = 5\n",
"V = (4/3) * math.pi * r**3\n",
"print(V)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "ea78a2d1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"945.4499999999999\n"
]
}
],
"source": [
"Discounted_Price = (24.95 * 0.6) * 60\n",
"Shipping_Costs = 3 + 0.75 * 59\n",
"print(Discounted_Price + Shipping_Costs)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "b5ca6708",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"07:30 AM\n"
]
}
],
"source": [
"from datetime import datetime, timedelta\n",
"\n",
"# Define the starting time\n",
"start_time = datetime.strptime(\"6:52\", \"%H:%M\")\n",
"\n",
"# Calculate time taken for each part of the run\n",
"easy_pace_time = timedelta(minutes=8, seconds=15)\n",
"tempo_pace_time = timedelta(minutes=7, seconds=12)\n",
"\n",
"# Calculate total time for the entire run\n",
"total_time = 2 * easy_pace_time + 3 * tempo_pace_time\n",
"\n",
"# Calculate the time for breakfast\n",
"end_time = start_time + total_time\n",
"\n",
"# Output the result\n",
"print(end_time.strftime(\"%I:%M %p\"))\n"
]
}
],
"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.9.16"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading