|
133 | 133 | "id": "279bb483-4bc6-4c8a-bf11-ee2addec3bff", |
134 | 134 | "metadata": {}, |
135 | 135 | "source": [ |
136 | | - "**Solution:**" |
| 136 | + "### Solution" |
137 | 137 | ] |
138 | 138 | }, |
139 | 139 | { |
|
310 | 310 | "- If `converged = False`: loop exited because iteration limit was reached" |
311 | 311 | ] |
312 | 312 | }, |
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 | | - }, |
348 | 313 | { |
349 | 314 | "cell_type": "markdown", |
350 | 315 | "id": "9288c3e4-7c88-48c9-b5b8-5bb886db08ec", |
351 | 316 | "metadata": {}, |
352 | 317 | "source": [ |
353 | | - "**Alternative Solution: Using `break`**\n", |
| 318 | + "### Alternative Solution 1: Using `break`\n", |
354 | 319 | "\n", |
355 | 320 | "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", |
356 | 321 | "\n", |
|
386 | 351 | "id": "64a6b608-c652-4619-85c3-3324a321b248", |
387 | 352 | "metadata": {}, |
388 | 353 | "source": [ |
389 | | - "** Alternative Solution: Using a Function to Test Convergence**\n", |
| 354 | + "### Alternative Solution 2: Using a Function to Test Convergence\n", |
390 | 355 | "\n", |
391 | 356 | "In the main solution, we test the convergence condition twice:\n", |
392 | 357 | "1. Before the loop: `converged = temperature - room_temperature <= threshold`\n", |
|
477 | 442 | " return abs(temperature - room_temperature) <= threshold\n", |
478 | 443 | "```\n", |
479 | 444 | "\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", |
481 | 446 | "\n", |
482 | 447 | "**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." |
483 | 448 | ] |
| 449 | + }, |
| 450 | + { |
| 451 | + "cell_type": "code", |
| 452 | + "execution_count": null, |
| 453 | + "id": "b14658e8-8971-47b3-883c-f42fcbba4f36", |
| 454 | + "metadata": {}, |
| 455 | + "outputs": [], |
| 456 | + "source": [] |
484 | 457 | } |
485 | 458 | ], |
486 | 459 | "metadata": { |
|
0 commit comments