From d04906744bdfc8c417c4d1ab2017d67a12b0d3b6 Mon Sep 17 00:00:00 2001 From: Anna Meyer <annapmeyer95@gmail.com> Date: Mon, 3 Jul 2023 11:57:28 -0500 Subject: [PATCH] lec 7 notes --- .../lec_06_Iteration_Practice_notes.ipynb | 692 ++++++++++++++++++ .../lec_06_Iteration_notes.ipynb | 125 +++- 2 files changed, 797 insertions(+), 20 deletions(-) create mode 100644 sum23/lecture_materials/06_Iteration1/lec_06_Iteration_Practice_notes.ipynb diff --git a/sum23/lecture_materials/06_Iteration1/lec_06_Iteration_Practice_notes.ipynb b/sum23/lecture_materials/06_Iteration1/lec_06_Iteration_Practice_notes.ipynb new file mode 100644 index 0000000..30b7d4b --- /dev/null +++ b/sum23/lecture_materials/06_Iteration1/lec_06_Iteration_Practice_notes.ipynb @@ -0,0 +1,692 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "6a76ef95", + "metadata": {}, + "source": [ + "# Iteration Practice" + ] + }, + { + "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\n", + "- Trace the output of a nested loop algorithm that prints out a game grid" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "28961628", + "metadata": {}, + "outputs": [], + "source": [ + "import project" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "c9341253", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__builtins__',\n", + " '__cached__',\n", + " '__doc__',\n", + " '__file__',\n", + " '__init__',\n", + " '__loader__',\n", + " '__name__',\n", + " '__package__',\n", + " '__spec__',\n", + " '__student__',\n", + " 'count',\n", + " 'get_age',\n", + " 'get_latitude',\n", + " 'get_lecture',\n", + " 'get_longitude',\n", + " 'get_major',\n", + " 'get_pet_owner',\n", + " 'get_piazza_topping',\n", + " 'get_procrastinator',\n", + " 'get_runner',\n", + " 'get_sleep_habit',\n", + " 'get_zip_code']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# TODO: inspect the functions inside project module\n", + "dir(project)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "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/annameyer/DocumentsLocal/teaching/cs220-lecture-material/sum23/lecture_materials/06_Iteration1/project.py\n", + "\n", + "\n" + ] + } + ], + "source": [ + "# TODO: inspect the project module's documentation\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": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<class 'str'>\n" + ] + } + ], + "source": [ + "# TODO: inspect return value type of get_age function\n", + "print(type(id_10_age))" + ] + }, + { + "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": "60730da8", + "metadata": {}, + "source": [ + "### What is the sleep habit of the student at the last index?" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "1d92e499", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'night owl'" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "project.get_sleep_habit(project.count() - 1)" + ] + }, + { + "cell_type": "markdown", + "id": "af3d9d8c", + "metadata": {}, + "source": [ + "### How many current lecture (example: LEC001) students are in the dataset? \n", + "\n", + "- use `for` loop to iterate over the dataset:\n", + " - `count` function gives you total number of students\n", + " - use `range` built-in function to generate sequence of integers from `0` to `count - 1`\n", + "- use `get_lecture` to retrieve lecture column value\n", + "- use `if` condition, to determine whether current student is part of `LEC001`\n", + " - `True` evaluation: increment count\n", + " - `False` evaluation: nothing to do" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "e024c488", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "195\n" + ] + } + ], + "source": [ + "count = 0\n", + "for idx in range(project.count()):\n", + " lecture_num = project.get_lecture(idx)\n", + " if lecture_num == 'LEC001':\n", + " count += 1\n", + "print(count)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c6a2ddf7", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "b9ff6434", + "metadata": {}, + "source": [ + "### What is the age of the oldest student in current lecture (example: LEC001)?\n", + "\n", + "- use `for` loop to iterate over the dataset just like last problem\n", + "- use `get_age` to retrieve lecture column value\n", + " - if: age is '' (empty), move on to next student using `continue`\n", + " - make sure to typecast return value to an integer\n", + "- use `get_lecture` to retrieve lecture column value\n", + "- use `if` condition, to determine whether current student is part of `LEC001`\n", + " - use `if` condition to determine whether current student's age is greater than previously known max age\n", + " - `True` evaluation: replace previously known max age with current age\n", + " - `False` evaluation: nothing to do" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "38bd778a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "37\n" + ] + } + ], + "source": [ + "max_age = 0\n", + "for idx in range(project.count()):\n", + " age = project.get_age(idx)\n", + " \n", + " if age == '':\n", + " continue\n", + " \n", + " age = int(age)\n", + "\n", + " lecture_num = project.get_lecture(idx)\n", + " if lecture_num == 'LEC001':\n", + " if age > max_age:\n", + " max_age = age\n", + "\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": null, + "id": "ea77e0cd", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "296c2a49", + "metadata": {}, + "source": [ + "### What is the average age of students enrolled in CS220 / CS319?\n", + "- you will get an interesting answer :)\n", + "- how can we ensure that data doesn't skew statistics?" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "8b7c8367", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average age is: 64.8225806451613\n" + ] + } + ], + "source": [ + "total = 0\n", + "number_of_students = 0\n", + "\n", + "for idx in range(project.count()):\n", + " age = project.get_age(idx)\n", + " \n", + " if age == '':\n", + " continue\n", + " age = int(age)\n", + " \n", + " #TODO: make sure age is real\n", + " \n", + " total += age\n", + " number_of_students += 1\n", + "print(\"Average age is:\", total / number_of_students)" + ] + }, + { + "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": 18, + "id": "a524873b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Economics\n" + ] + } + ], + "source": [ + "min_age = 99\n", + "min_age_idx = -1 \n", + "\n", + "for idx in range(project.count()):\n", + " age = project.get_age(idx)\n", + " if age == '':\n", + " continue\n", + " \n", + " age = int(age)\n", + " \n", + " lecture_number = project.get_lecture(idx)\n", + " if lecture_number != \"LEC001\":\n", + " continue\n", + " \n", + " if age < min_age:\n", + " min_age = age\n", + " min_age_idx = idx\n", + "\n", + "print(project.get_major(idx))\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": null, + "id": "fada2a40", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "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": "db60812c", + "metadata": {}, + "source": [ + "### Considering current lecture students (example: LEC001), is the first student with age 18 a runner?" + ] + }, + { + "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": 23, + "id": "333cd894", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "maximum latitude is 78.225\n", + "corresponding longitude is 15.626\n" + ] + } + ], + "source": [ + "maximum_latitude = -1000\n", + "max_idx = -1 \n", + "\n", + "for idx in range(project.count()):\n", + " latitude = float(project.get_latitude(idx))\n", + " if abs(latitude) > 90:\n", + " continue\n", + " \n", + " if latitude > maximum_latitude:\n", + " maximum_latitude = latitude\n", + " max_idx = idx\n", + " \n", + "print(\"maximum latitude is \",maximum_latitude)\n", + "print(\"corresponding longitude is \",project.get_longitude(max_idx))" + ] + }, + { + "cell_type": "markdown", + "id": "5f943c98", + "metadata": {}, + "source": [ + "### What is the minimum longitude (& corresponding latitude) 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": 24, + "id": "d3e7fad2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "minimum longitude is -90.0\n", + "corresponding latitude is 40\n" + ] + } + ], + "source": [ + "minimum_longitude = 1000\n", + "min_idx = -1 \n", + "\n", + "for idx in range(project.count()):\n", + " longitude = float(project.get_longitude(idx))\n", + " if abs(longitude) > 90:\n", + " continue\n", + " \n", + " if longitude < minimum_longitude:\n", + " minimum_longitude = longitude\n", + " min_idx = idx\n", + " \n", + "print(\"minimum longitude is \",minimum_longitude)\n", + "print(\"corresponding latitude is \",project.get_latitude(min_idx))" + ] + }, + { + "cell_type": "markdown", + "id": "9b104a50", + "metadata": {}, + "source": [ + "### What is the maximum longitude (& corresponding latitude) 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.10.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/sum23/lecture_materials/06_Iteration1/lec_06_Iteration_notes.ipynb b/sum23/lecture_materials/06_Iteration1/lec_06_Iteration_notes.ipynb index 745cbc2..bcd912c 100644 --- a/sum23/lecture_materials/06_Iteration1/lec_06_Iteration_notes.ipynb +++ b/sum23/lecture_materials/06_Iteration1/lec_06_Iteration_notes.ipynb @@ -640,31 +640,47 @@ ] }, { - "cell_type": "markdown", - "id": "81abdb38", + "cell_type": "code", + "execution_count": 3, + "id": "de2adb5a", "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", + " divisor = 2\n", + " while divisor <= math.sqrt(num):\n", " # check if num is divisible by divisor\n", - " if num % divisor == ???:\n", - " return ???\n", - " divisor ???\n", + " if num % divisor == 0:\n", + " return False\n", + " divisor += 1\n", " \n", - " return ???" + " return True" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "e432722e", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "True\n", + "True\n", + "True\n", + "False\n", + "True\n", + "False\n" + ] + } + ], "source": [ "print(is_prime(1))\n", "print(is_prime(2))\n", @@ -677,7 +693,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "c69150ed", "metadata": {}, "outputs": [], @@ -688,25 +704,65 @@ " # 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" + " success = False \n", + " first_prime = -1 # optional (use if we want to return the value of the prime number)\n", + " for x in range(start, end + 1):\n", + " x_prime = is_prime(x)\n", + " if x_prime:\n", + " success = True\n", + " first_prime = x # optional \n", + " print(str(x) + \" is prime\") # optional \n", + " break\n", + " return success\n", + "# we could return first_prime instead if we wanted to return the prime value itself rather \n", + "# than just a boolean True/False" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "bd3e2027", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "has_prime(14, 16)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "0429022b", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1000003 is prime\n" + ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "has_prime(1000000, 1001000)" ] @@ -723,14 +779,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "id": "cb569b21", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "What is your age?20\n", + "What is your age?21\n", + "What is your age?30\n", + "What is your age?17\n", + "What is your age?q\n", + "Average age is 22.0\n" + ] + } + ], "source": [ "# TODO: write an infinite loop using while\n", + "total = 0\n", + "number_of_users = 0\n", "\n", + "while True:\n", " # TODO: get user input for age\n", + " age = input(\"What is your age?\")\n", + " # special case: quit\n", + " if age == 'q':\n", + " break\n", + " \n", + " age = int(age)\n", " \n", " # Goal: to compute running average\n", " # It is easy to keep track of total and number of user\n", @@ -738,10 +816,15 @@ " \n", " # TODO: discuss what is acceptable range for age\n", " # What is the guinness world record for oldest person?\n", - " \n", + " valid_age = 0 <= age <= 120\n", + " if not valid_age:\n", + " continue\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", + " number_of_users += 1\n", + " total += age\n", " \n", " # Now, try entering input as a large number outside of your\n", " # acceptable age range. What happens to your average?\n", @@ -752,7 +835,9 @@ " # 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" + " # terms of control flow\n", + " \n", + "print(\"Average age is \", str(total/number_of_users))" ] }, { -- GitLab