diff --git a/f23/Gurmail_Lecture_Notes/11_Iteration-2/lec_11_Iteration_2.ipynb b/f23/Gurmail_Lecture_Notes/11_Iteration-2/lec_11_Iteration_2.ipynb index aae3f2e979f1ec7b9cdd6e744472127acf55d814..9ff24398035997531d36d58040643a6ecb07d619 100644 --- a/f23/Gurmail_Lecture_Notes/11_Iteration-2/lec_11_Iteration_2.ipynb +++ b/f23/Gurmail_Lecture_Notes/11_Iteration-2/lec_11_Iteration_2.ipynb @@ -1,7 +1,33 @@ { "cells": [ { - "attachments": {}, + "cell_type": "markdown", + "id": "5117857a", + "metadata": {}, + "source": [ + "# Announcements - Friday, September 29\n", + " \n", + "* Download files for today's lecture\n", + "* Quiz 3 due today\n", + "* [Thank You](https://docs.google.com/forms/d/e/1FAIpQLSfeBW4gVkm8y-Z3JHvmz1OmvW8Db_12f2xaPhR5Np2ECNEWFw/viewform)\n", + "* Exam 1\n", + " * General: Wednesday October 4: 5:45 pm\n", + " * McBurney: Wednesday October 4: 5:30 pm - If you are approved for anything other than extra time and small group testing please fill out the form\n", + " * Alternate: Thursday October 5: 5:45 pm \n", + " * Room assignment via Canvas Message (before Tuesday, October 3)\n", + " * one page note sheet\n", + " * put anything you want on this sheet (both sides, written or printed)\n", + " * we will keep this sheet - make a copy if you want one\n", + " * Bring ID Card and #2 pencil\n", + "* [The best way to study for the exam!!!](https://git.doit.wisc.edu/cdis/cs/courses/cs220/cs220-lecture-material/-/tree/main/f23/old_exams/exam1)\n", + "\n", + " ### Conflict Form\n", + " * [Exam 1 - October 4, 5:45 pm](https://cs220.cs.wisc.edu/f23/surveys.html)\n", + " * [Exam 2 - November 8, 5:45 pm](https://cs220.cs.wisc.edu/f23/surveys.html)\n", + " * [Final - December 19, 7:45 am](https://cs220.cs.wisc.edu/f23/surveys.html)" + ] + }, + { "cell_type": "markdown", "id": "1fd5544f", "metadata": {}, @@ -396,7 +422,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.12" + "version": "3.11.4" } }, "nbformat": 4, diff --git a/f23/Gurmail_Lecture_Notes/11_Iteration-2/lec_11_Iteration_2_template.ipynb b/f23/Gurmail_Lecture_Notes/11_Iteration-2/lec_11_Iteration_2_template_Gurmail_lec1.ipynb similarity index 85% rename from f23/Gurmail_Lecture_Notes/11_Iteration-2/lec_11_Iteration_2_template.ipynb rename to f23/Gurmail_Lecture_Notes/11_Iteration-2/lec_11_Iteration_2_template_Gurmail_lec1.ipynb index a6dcd8758427161af860f9a8bc63f5bab1525e6d..b447a6ed2be542f0810ae496caae49c5b2ab2c4b 100644 --- a/f23/Gurmail_Lecture_Notes/11_Iteration-2/lec_11_Iteration_2_template.ipynb +++ b/f23/Gurmail_Lecture_Notes/11_Iteration-2/lec_11_Iteration_2_template_Gurmail_lec1.ipynb @@ -1,5 +1,32 @@ { "cells": [ + { + "cell_type": "markdown", + "id": "e6f081e5", + "metadata": {}, + "source": [ + "# Announcements - Friday, September 29\n", + " \n", + "* Download files for today's lecture\n", + "* Quiz 3 due today\n", + "* [Thank You](https://docs.google.com/forms/d/e/1FAIpQLSfeBW4gVkm8y-Z3JHvmz1OmvW8Db_12f2xaPhR5Np2ECNEWFw/viewform)\n", + "* Exam 1\n", + " * General: Wednesday October 4: 5:45 pm\n", + " * McBurney: Wednesday October 4: 5:30 pm - If you are approved for anything other than extra time and small group testing please fill out the form\n", + " * Alternate: Thursday October 5: 5:45 pm \n", + " * Room assignment via Canvas Message (before Tuesday, October 3)\n", + " * one page note sheet\n", + " * put anything you want on this sheet (both sides, written or printed)\n", + " * we will keep this sheet - make a copy if you want one\n", + " * Bring ID Card and #2 pencil\n", + "* [The best way to study for the exam!!!](https://git.doit.wisc.edu/cdis/cs/courses/cs220/cs220-lecture-material/-/tree/main/f23/old_exams/exam1)\n", + "\n", + " ### Conflict Form\n", + " * [Exam 1 - October 4, 5:45 pm](https://cs220.cs.wisc.edu/f23/surveys.html)\n", + " * [Exam 2 - November 8, 5:45 pm](https://cs220.cs.wisc.edu/f23/surveys.html)\n", + " * [Final - December 19, 7:45 am](https://cs220.cs.wisc.edu/f23/surveys.html)" + ] + }, { "cell_type": "markdown", "id": "cce4a7bf", @@ -315,7 +342,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.12" + "version": "3.11.4" } }, "nbformat": 4, diff --git a/f23/Gurmail_Lecture_Notes/11_Iteration-2/lec_11_Iteration_2_template_Gurmail_lec2.ipynb b/f23/Gurmail_Lecture_Notes/11_Iteration-2/lec_11_Iteration_2_template_Gurmail_lec2.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..b447a6ed2be542f0810ae496caae49c5b2ab2c4b --- /dev/null +++ b/f23/Gurmail_Lecture_Notes/11_Iteration-2/lec_11_Iteration_2_template_Gurmail_lec2.ipynb @@ -0,0 +1,350 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "e6f081e5", + "metadata": {}, + "source": [ + "# Announcements - Friday, September 29\n", + " \n", + "* Download files for today's lecture\n", + "* Quiz 3 due today\n", + "* [Thank You](https://docs.google.com/forms/d/e/1FAIpQLSfeBW4gVkm8y-Z3JHvmz1OmvW8Db_12f2xaPhR5Np2ECNEWFw/viewform)\n", + "* Exam 1\n", + " * General: Wednesday October 4: 5:45 pm\n", + " * McBurney: Wednesday October 4: 5:30 pm - If you are approved for anything other than extra time and small group testing please fill out the form\n", + " * Alternate: Thursday October 5: 5:45 pm \n", + " * Room assignment via Canvas Message (before Tuesday, October 3)\n", + " * one page note sheet\n", + " * put anything you want on this sheet (both sides, written or printed)\n", + " * we will keep this sheet - make a copy if you want one\n", + " * Bring ID Card and #2 pencil\n", + "* [The best way to study for the exam!!!](https://git.doit.wisc.edu/cdis/cs/courses/cs220/cs220-lecture-material/-/tree/main/f23/old_exams/exam1)\n", + "\n", + " ### Conflict Form\n", + " * [Exam 1 - October 4, 5:45 pm](https://cs220.cs.wisc.edu/f23/surveys.html)\n", + " * [Exam 2 - November 8, 5:45 pm](https://cs220.cs.wisc.edu/f23/surveys.html)\n", + " * [Final - December 19, 7:45 am](https://cs220.cs.wisc.edu/f23/surveys.html)" + ] + }, + { + "cell_type": "markdown", + "id": "cce4a7bf", + "metadata": {}, + "source": [ + "# Iteration 2\n", + "\n", + "## Readings: \n", + "- Chapter 2 of Sweigart book\n", + "- Chapter 6.4 of Python for Everybody" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7693263d", + "metadata": {}, + "outputs": [], + "source": [ + "import math" + ] + }, + { + "cell_type": "markdown", + "id": "0b7e0914", + "metadata": {}, + "source": [ + "## Learning Objectives\n", + "\n", + "- Read and trace through Python code containing nested loops.\n", + "- Read and trace through Python code using `break` or `continue` in a `while` loop\n", + "- Determine the effect of break and continue in nested loops" + ] + }, + { + "cell_type": "markdown", + "id": "06b520a0", + "metadata": {}, + "source": [ + "## `break` example\n", + "- `break` enables to terminate execution of a while loop\n", + "- typically used with a conditional; that is you break when condition evaluates to `True`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8067fa3e", + "metadata": {}, + "outputs": [], + "source": [ + "def is_prime(num):\n", + " \"\"\" returns True if x is prime, false otherwise. \n", + " Assumes x is positive\"\"\"\n", + " \n", + " # try all divisors from 2 to sqrt(num) to check if num is prime\n", + " divisor = ???\n", + " while ???:\n", + " # check if num is divisible by divisor\n", + " if num % divisor == ???:\n", + " return ???\n", + " divisor ???\n", + " \n", + " return ???" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2e3d3f29", + "metadata": {}, + "outputs": [], + "source": [ + "print(is_prime(1))\n", + "print(is_prime(2))\n", + "print(is_prime(3))\n", + "print(is_prime(7))\n", + "print(is_prime(16))\n", + "print(is_prime(23))\n", + "print(is_prime(1000000))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9a28c726", + "metadata": {}, + "outputs": [], + "source": [ + "def has_prime(start, end):\n", + " # TODO: write a for loop using range, to:\n", + " # 1. iterate over every number from start to end\n", + " # 2. call is_prime function, to determine if it is prime\n", + " # 3. if you find at least one prime, has_prime should\n", + " # return True, False otherwise\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5cde0776", + "metadata": {}, + "outputs": [], + "source": [ + "has_prime(14, 16)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3a5be934", + "metadata": {}, + "outputs": [], + "source": [ + "has_prime(1000000, 1001000)" + ] + }, + { + "cell_type": "markdown", + "id": "12c14f3b", + "metadata": {}, + "source": [ + "## `continue` example\n", + "- `continue` enables to move on to the next iteration of the while loop\n", + "- typically used with a conditional; that is you continue when condition evaluates to `True`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ef8d6e0b", + "metadata": {}, + "outputs": [], + "source": [ + "# TODO: write an infinite loop using while\n", + "\n", + " # TODO: get user input for age\n", + " \n", + " # Goal: to compute running average\n", + " # It is easy to keep track of total and number of user\n", + " # inputs to compute running average\n", + " \n", + " # TODO: discuss what is acceptable range for age\n", + " # What is the guinness world record for oldest person?\n", + " \n", + " # TODO: discuss where you will initialize variables to keep track\n", + " # of total and number of user inputs so far and then type the\n", + " # computation lines to compute updated total and running average\n", + " \n", + " # Now, try entering input as a large number outside of your\n", + " # acceptable age range. What happens to your average?\n", + " # TODO: handle this by writing a conditional and use continue,\n", + " # when user enters invalid age\n", + " \n", + " # Finally, how do we terminate the infinite while loop\n", + " # Let's accept \"q\" as user input for termination\n", + " # TODO: handle that using another conditional\n", + " # Think carefully about where this conditional needs to be in \n", + " # terms of control flow" + ] + }, + { + "cell_type": "markdown", + "id": "944cc596", + "metadata": {}, + "source": [ + "### After lecture" + ] + }, + { + "cell_type": "markdown", + "id": "0820ba66", + "metadata": {}, + "source": [ + "How many times is the *while loop condition line* executed?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c3c14b7b", + "metadata": {}, + "outputs": [], + "source": [ + "n = 7\n", + "while n >= 5:\n", + " print(n)\n", + " n -= 1\n", + " \n", + "# Answer is 4. \n", + "# Loop condition line always gets executed number of \n", + "# iterations + 1 times." + ] + }, + { + "cell_type": "markdown", + "id": "7cf19523", + "metadata": {}, + "source": [ + "Refactor the below function." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a313a32f", + "metadata": {}, + "outputs": [], + "source": [ + "def is_between(a, b, c):\n", + " \"\"\"Return True if b is between a and c (exclusive), \n", + " False otherwise\"\"\"\n", + " if a < c:\n", + " if a < b and b < c:\n", + " return True\n", + " else:\n", + " return False\n", + " elif c <= a:\n", + " if c < b and b < a:\n", + " return True\n", + " else:\n", + " return False\n", + " else:\n", + " return False\n", + " \n", + "print(is_between(1, 3, 2)) # False\n", + "print(is_between(5, 11, 20)) # True\n", + "print(is_between(20, 3, 5)) # False\n", + "print(is_between(50, 11, 9)) # True\n", + "print(is_between(4, 4, 4)) # False" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7fd82ae9", + "metadata": {}, + "outputs": [], + "source": [ + "def is_between_v2(a, b, c):\n", + " return ???\n", + "\n", + "print(is_between_v2(1, 3, 2)) # False\n", + "print(is_between_v2(5, 11, 20)) # True\n", + "print(is_between_v2(20, 3, 5)) # False\n", + "print(is_between_v2(50, 11, 9)) # True\n", + "print(is_between_v2(4, 4, 4)) # False" + ] + }, + { + "cell_type": "markdown", + "id": "ca9037f7", + "metadata": {}, + "source": [ + "Trace the output without using Python interpreter" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ec8b1b32", + "metadata": {}, + "outputs": [], + "source": [ + "x = 1\n", + "while x < 5:\n", + " y = 1\n", + " while y < 10:\n", + " print (x * y, \"\\t\", end=\"\")\n", + " y += 1\n", + " print()\n", + " x += 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "74060bc4", + "metadata": {}, + "outputs": [], + "source": [ + "width = 9\n", + "height = 4\n", + "symbol = '#'\n", + "row = 0\n", + "while row < height:\n", + " col = 0\n", + " if row % 2 == 1:\n", + " print(\" \", end=\"\")\n", + " while col < width:\n", + " print(symbol + ' ', end=\"\")\n", + " col += 1\n", + " # displays just a newline\n", + " print() # recall default value for end parameter is \"\\n\"\n", + " row += 1" + ] + } + ], + "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.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}