From f08c46647c72bc1bde388ffd0efa56150c20618a Mon Sep 17 00:00:00 2001
From: Andy Kuemmel <kuemmel@wisc.edu>
Date: Mon, 3 Oct 2022 11:20:50 -0500
Subject: [PATCH] Update
 f22/andy_lec_notes/lec12_Oct03_IterationAlgorithms/lec12_Oct03_IterationAlgorithms_completed.ipynb

---
 ..._Oct03_IterationAlgorithms_completed.ipynb | 634 ++++++++++++++++++
 1 file changed, 634 insertions(+)
 create mode 100644 f22/andy_lec_notes/lec12_Oct03_IterationAlgorithms/lec12_Oct03_IterationAlgorithms_completed.ipynb

diff --git a/f22/andy_lec_notes/lec12_Oct03_IterationAlgorithms/lec12_Oct03_IterationAlgorithms_completed.ipynb b/f22/andy_lec_notes/lec12_Oct03_IterationAlgorithms/lec12_Oct03_IterationAlgorithms_completed.ipynb
new file mode 100644
index 0000000..d66f14f
--- /dev/null
+++ b/f22/andy_lec_notes/lec12_Oct03_IterationAlgorithms/lec12_Oct03_IterationAlgorithms_completed.ipynb
@@ -0,0 +1,634 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "id": "6a76ef95",
+   "metadata": {},
+   "source": [
+    "# Iteration Algorithms\n",
+    "\n",
+    "Thanks to CS 220 campus students for their data and to Meena for putting together the project.py file"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "103da70b",
+   "metadata": {},
+   "source": [
+    "## Learning Objectives\n",
+    "\n",
+    "- Iterate through a dataset using for idx in range(project.count())\n",
+    "- Compute the frequency of data that meets a certain criteria\n",
+    "- Find the maximum or minimum value of a numeric column in a dataset\n",
+    "    - Handle missing numeric values when computing a maximum / minimum\n",
+    "    - Use the index of a maximum or minimum to access other information about that data item\n",
+    "- Use break and continue in for loops when processing a dataset"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "id": "28961628",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import project"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "id": "d1dca7ae",
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Help on module project:\n",
+      "\n",
+      "NAME\n",
+      "    project\n",
+      "\n",
+      "FUNCTIONS\n",
+      "    __init__()\n",
+      "    \n",
+      "    count()\n",
+      "        This function will return the number of records in the dataset\n",
+      "    \n",
+      "    get_age(idx)\n",
+      "        get_age(idx) returns the age of the student in row idx\n",
+      "    \n",
+      "    get_latitude(idx)\n",
+      "        get_latitude(idx) returns the latitude of the student's favourite place in row idx\n",
+      "    \n",
+      "    get_lecture(idx)\n",
+      "        get_lecture(idx) returns the lecture of the student in row idx\n",
+      "    \n",
+      "    get_longitude(idx)\n",
+      "        get_longitude(idx) returns the longitude of the student's favourite place in row idx\n",
+      "    \n",
+      "    get_major(idx)\n",
+      "        get_major(idx) returns the major of the student in row idx\n",
+      "    \n",
+      "    get_pet_owner(idx)\n",
+      "        get_pet_owner(idx) returns the pet preference of student in row idx\n",
+      "    \n",
+      "    get_piazza_topping(idx)\n",
+      "        get_piazza_topping(idx) returns the preferred pizza toppings of the student in row idx\n",
+      "    \n",
+      "    get_procrastinator(idx)\n",
+      "        get_procrastinator(idx) returns whether student in row idx is a procrastinator\n",
+      "    \n",
+      "    get_runner(idx)\n",
+      "        get_runner(idx) returns whether student in row idx is a runner\n",
+      "    \n",
+      "    get_sleep_habit(idx)\n",
+      "        get_sleep_habit(idx) returns the sleep habit of the student in row idx\n",
+      "    \n",
+      "    get_zip_code(idx)\n",
+      "        get_zip_code(idx) returns the residential zip code of the student in row idx\n",
+      "\n",
+      "DATA\n",
+      "    __student__ = [{'Age': '22', 'Latitude': '43.073051', 'Lecture': 'LEC0...\n",
+      "\n",
+      "FILE\n",
+      "    /Users/andrewkuemmel/cs220-f22/lectures/lec12_Oct03_IterationAlgorithms/project.py\n",
+      "\n",
+      "\n"
+     ]
+    }
+   ],
+   "source": [
+    "# TODO: inspect the project module's documentation by using help\n",
+    "help(project)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "7fb78f6b",
+   "metadata": {},
+   "source": [
+    "### How many students does the dataset have?"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "id": "d67a080f",
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "992"
+      ]
+     },
+     "execution_count": 4,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "project.count()"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "3c97d494",
+   "metadata": {},
+   "source": [
+    "### What is the age of the student at index 10?"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "id": "bde8dc35",
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'21'"
+      ]
+     },
+     "execution_count": 5,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "id_10_age = project.get_age(10)\n",
+    "id_10_age"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 6,
+   "id": "b0f87a2c",
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "str"
+      ]
+     },
+     "execution_count": 6,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "# TODO: what is the return value type of the get_age function? \n",
+    "type(id_10_age)\n",
+    "\n",
+    "# in a CSV file, all data stored as a string"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "37898141",
+   "metadata": {},
+   "source": [
+    "### What is the lecture number of the student at index 20?"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 7,
+   "id": "ba993090",
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'LEC001'"
+      ]
+     },
+     "execution_count": 7,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "project.get_lecture(20)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "6e3e3c0a",
+   "metadata": {},
+   "source": [
+    "### What are the age and pizza choices of the first 10 students? \n",
+    "\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 10,
+   "id": "46ec7632",
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "22 none (just cheese)\n",
+      " none (just cheese)\n",
+      "18 none (just cheese)\n",
+      " none (just cheese)\n",
+      " none (just cheese)\n",
+      "18 none (just cheese)\n",
+      " none (just cheese)\n",
+      "18 pineapple\n",
+      "18 none (just cheese)\n",
+      "18 pepperoni\n"
+     ]
+    }
+   ],
+   "source": [
+    "for i in range(10):\n",
+    "    print(project.get_age(i), project.get_piazza_topping(i))"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "af3d9d8c",
+   "metadata": {},
+   "source": [
+    "### How many current lecture (example: LEC001) students are in the dataset? \n",
+    "\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 13,
+   "id": "e024c488",
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "195\n"
+     ]
+    }
+   ],
+   "source": [
+    "frequency = 0\n",
+    "for i in range(project.count()):\n",
+    "    if project.get_lecture(i) == \"LEC001\":\n",
+    "        frequency += 1\n",
+    "print(frequency)\n",
+    "    "
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "b9ff6434",
+   "metadata": {},
+   "source": [
+    "### What is the age of the oldest student in the course? \n",
+    "\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 19,
+   "id": "38bd778a",
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "53\n"
+     ]
+    }
+   ],
+   "source": [
+    "# find the maximum\n",
+    "# check for missing data\n",
+    "# check for data out of range \n",
+    "\n",
+    "max_age = 0\n",
+    "for i in range(project.count()):\n",
+    "    if project.get_age(i) == '': \n",
+    "        continue\n",
+    "    age = int(project.get_age(i))\n",
+    "    if age < 0 or age > 100:\n",
+    "        continue\n",
+    "    if age > max_age:\n",
+    "        max_age = age\n",
+    "print(max_age)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "b40a32fb",
+   "metadata": {},
+   "source": [
+    "### What is the age of the youngest student in current lecture (example: LEC001)?\n",
+    "- use similar algorithm as above question"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 23,
+   "id": "ea77e0cd",
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "22\n"
+     ]
+    }
+   ],
+   "source": [
+    "min_age = int(project.get_age(0) )  #assume first student is youngest,and has age\n",
+    "for i in range(project.count()):\n",
+    "    if project.get_lecture != \"LEC001\":\n",
+    "        continue\n",
+    "    if project.get_age(i) == '': \n",
+    "        continue\n",
+    "    age = int(project.get_age(i))\n",
+    "    if age < 0 or age > 100:\n",
+    "        continue\n",
+    "    if age < min_age:\n",
+    "        min_age = age\n",
+    "print(min_age)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "296c2a49",
+   "metadata": {},
+   "source": [
+    "### What is the average age of students enrolled in CS220 / CS319?\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 38,
+   "id": "8b7c8367",
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "19.608180839612487"
+      ]
+     },
+     "execution_count": 38,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "total_age = 0\n",
+    "num_students_with_age = 0\n",
+    "for i in range(project.count()):\n",
+    "    if project.get_age(i) == '': \n",
+    "        continue\n",
+    "    age = int(project.get_age(i))\n",
+    "    if age < 0 or age > 100:\n",
+    "        continue\n",
+    "    total_age += age\n",
+    "    num_students_with_age += 1\n",
+    "average_age = total_age / num_students_with_age\n",
+    "average_age"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "48f1c791",
+   "metadata": {},
+   "source": [
+    "### What major is the youngest student in current lecture (example: LEC001) planning to declare?\n",
+    "- now, we need to find some other detail about the youngest student\n",
+    "- often, you'll have to keep track of ID of the max or min, so that you can retrive other details about that data entry"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 36,
+   "id": "a524873b",
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "17"
+      ]
+     },
+     "execution_count": 36,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "# we will do this in lecture on Wednesday\n",
+    "    "
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "5294702a",
+   "metadata": {},
+   "source": [
+    "### Considering current lecture students (example: LEC001), what is the age of the first student residing at zip code 53715?"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 33,
+   "id": "fada2a40",
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "19 <class 'str'>\n"
+     ]
+    }
+   ],
+   "source": [
+    "for i in range(project.count()):\n",
+    "    # put a print statement here\n",
+    "    if project.get_lecture(i) == \"LEC002\" and project.get_zip_code(i) == \"53715\":\n",
+    "        print(project.get_age(i), type(project.get_age(i)))\n",
+    "        break"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 25,
+   "id": "772f7a58",
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Help on module project:\n",
+      "\n",
+      "NAME\n",
+      "    project\n",
+      "\n",
+      "FUNCTIONS\n",
+      "    __init__()\n",
+      "    \n",
+      "    count()\n",
+      "        This function will return the number of records in the dataset\n",
+      "    \n",
+      "    get_age(idx)\n",
+      "        get_age(idx) returns the age of the student in row idx\n",
+      "    \n",
+      "    get_latitude(idx)\n",
+      "        get_latitude(idx) returns the latitude of the student's favourite place in row idx\n",
+      "    \n",
+      "    get_lecture(idx)\n",
+      "        get_lecture(idx) returns the lecture of the student in row idx\n",
+      "    \n",
+      "    get_longitude(idx)\n",
+      "        get_longitude(idx) returns the longitude of the student's favourite place in row idx\n",
+      "    \n",
+      "    get_major(idx)\n",
+      "        get_major(idx) returns the major of the student in row idx\n",
+      "    \n",
+      "    get_pet_owner(idx)\n",
+      "        get_pet_owner(idx) returns the pet preference of student in row idx\n",
+      "    \n",
+      "    get_piazza_topping(idx)\n",
+      "        get_piazza_topping(idx) returns the preferred pizza toppings of the student in row idx\n",
+      "    \n",
+      "    get_procrastinator(idx)\n",
+      "        get_procrastinator(idx) returns whether student in row idx is a procrastinator\n",
+      "    \n",
+      "    get_runner(idx)\n",
+      "        get_runner(idx) returns whether student in row idx is a runner\n",
+      "    \n",
+      "    get_sleep_habit(idx)\n",
+      "        get_sleep_habit(idx) returns the sleep habit of the student in row idx\n",
+      "    \n",
+      "    get_zip_code(idx)\n",
+      "        get_zip_code(idx) returns the residential zip code of the student in row idx\n",
+      "\n",
+      "DATA\n",
+      "    __student__ = [{'Age': '22', 'Latitude': '43.073051', 'Lecture': 'LEC0...\n",
+      "\n",
+      "FILE\n",
+      "    /Users/andrewkuemmel/cs220-f22/lectures/lec12_Oct03_IterationAlgorithms/project.py\n",
+      "\n",
+      "\n"
+     ]
+    }
+   ],
+   "source": [
+    "help(project)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "68793d99",
+   "metadata": {},
+   "source": [
+    "## Self-practice"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "2eeed867",
+   "metadata": {},
+   "source": [
+    "### How many current lecture (example: LEC001) students are runners? "
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "1ea57e12",
+   "metadata": {},
+   "source": [
+    "### How many current lecture (example: LEC001) students are procrastinators? "
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "cf0ac7c8",
+   "metadata": {},
+   "source": [
+    "### How many current lecture (example: LEC001) students own or have owned a pet?"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "ffd5e10f",
+   "metadata": {},
+   "source": [
+    "### What sleep habit does the youngest student in current lecture (example: LEC001) have?\n",
+    "- try to solve this from scratch, instead of copy-pasting code to find mimimum age"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "f255b95a",
+   "metadata": {},
+   "source": [
+    "### What sleep habit does the oldest student in current lecture (example: LEC001) have?\n",
+    "- try to solve this from scratch, instead of copy-pasting code to find mimimum age"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "70a8ac57",
+   "metadata": {},
+   "source": [
+    "### What is the minimum latitude (& corresponding longitude) of a student's place of interest? \n",
+    "- What place is this -> try to enter the lat, long on Google maps?"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "581ea197",
+   "metadata": {},
+   "source": [
+    "### What is the maximum latitude (& corresponding longitude) of a student's place of interest? \n",
+    "- What place is this -> try to enter the lat, long on Google maps?"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "fcee0994",
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "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.12"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
-- 
GitLab