Skip to content

Commit b73a6d3

Browse files
committed
week 2 solutions
1 parent 2de94d7 commit b73a6d3

2 files changed

Lines changed: 14 additions & 39 deletions

File tree

CH40208/_toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,5 @@ parts:
130130
title: Functions
131131
- file: worked_examples/week_2_comparisons_and_flow_control
132132
title: Comparisons and Flow Control
133+
- file: worked_examples/week_2_advanced_loop_control
134+
title: Advanced Loop Control

CH40208/worked_examples/week_2_advanced_loop_control.ipynb

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@
133133
"id": "279bb483-4bc6-4c8a-bf11-ee2addec3bff",
134134
"metadata": {},
135135
"source": [
136-
"**Solution:**"
136+
"### Solution"
137137
]
138138
},
139139
{
@@ -310,47 +310,12 @@
310310
"- If `converged = False`: loop exited because iteration limit was reached"
311311
]
312312
},
313-
{
314-
"cell_type": "code",
315-
"execution_count": null,
316-
"id": "5f3cbd1f-5e98-4d8c-911b-8f08f7130f1a",
317-
"metadata": {},
318-
"outputs": [],
319-
"source": [
320-
"temperature = 100.0 # Starting temperature in °C\n",
321-
"room_temperature = 20.0 # Ambient temperature in °C\n",
322-
"threshold = 0.5 # Stop when within 0.5°C of room temp\n",
323-
"cooling_rate = 0.02 # Cool by 2% per step\n",
324-
"iteration = 0\n",
325-
"max_iterations = 100\n",
326-
"converged = temperature - room_temperature <= threshold\n",
327-
"\n",
328-
"while not converged and iteration < max_iterations:\n",
329-
" # Cool by 2% of the temperature difference each step\n",
330-
" temperature_difference = temperature - room_temperature\n",
331-
" temperature = temperature - cooling_rate * temperature_difference\n",
332-
" iteration += 1\n",
333-
" print(f\"Step {iteration}: Temperature = {temperature:.2f}°C\")\n",
334-
"\n",
335-
" if temperature - room_temperature <= threshold:\n",
336-
" converged = True\n",
337-
"\n",
338-
"# Report results based on whether convergence was achieved\n",
339-
"if converged:\n",
340-
" print(f\"\\nThermal equilibrium reached after {iteration} steps\")\n",
341-
" print(f\"Final temperature: {temperature:.2f}°C\")\n",
342-
"else:\n",
343-
" print(f\"\\nIteration limit reached after {iteration} steps\")\n",
344-
" print(f\"Current temperature: {temperature:.2f}°C (still {temperature - room_temperature:.2f}°C above room temperature)\")\n",
345-
" print(\"More time needed for thermal equilibrium\")"
346-
]
347-
},
348313
{
349314
"cell_type": "markdown",
350315
"id": "9288c3e4-7c88-48c9-b5b8-5bb886db08ec",
351316
"metadata": {},
352317
"source": [
353-
"**Alternative Solution: Using `break`**\n",
318+
"### Alternative Solution 1: Using `break`\n",
354319
"\n",
355320
"An alternative approach uses `break` to exit the loop early when the iteration limit is reached. This separates the two exit conditions rather than combining them in the `while` statement.\n",
356321
"\n",
@@ -386,7 +351,7 @@
386351
"id": "64a6b608-c652-4619-85c3-3324a321b248",
387352
"metadata": {},
388353
"source": [
389-
"** Alternative Solution: Using a Function to Test Convergence**\n",
354+
"### Alternative Solution 2: Using a Function to Test Convergence\n",
390355
"\n",
391356
"In the main solution, we test the convergence condition twice:\n",
392357
"1. Before the loop: `converged = temperature - room_temperature <= threshold`\n",
@@ -477,10 +442,18 @@
477442
" return abs(temperature - room_temperature) <= threshold\n",
478443
"```\n",
479444
"\n",
480-
"With a function, this is a one-line change. Without it, we'd need to find and update both occurrences, risking mistakes if we miss one.\n",
445+
"With a function, this is a one-line change. Without it, we'd need to find and update both occurrences, risking bugs if we miss one.\n",
481446
"\n",
482447
"**Trade-off:** For very simple conditions used only 2-3 times in short code, a function might be overkill. But as code grows in complexity, extracting repeated logic into functions becomes increasingly valuable."
483448
]
449+
},
450+
{
451+
"cell_type": "code",
452+
"execution_count": null,
453+
"id": "b14658e8-8971-47b3-883c-f42fcbba4f36",
454+
"metadata": {},
455+
"outputs": [],
456+
"source": []
484457
}
485458
],
486459
"metadata": {

0 commit comments

Comments
 (0)