From e90102d069079c91a7f5f21ca0045ea045005ba9 Mon Sep 17 00:00:00 2001 From: Ashwin Maran <amaran@wisc.edu> Date: Sun, 18 Feb 2024 16:39:17 -0600 Subject: [PATCH] Upload New File --- .../Lec_12_Iteration_Practice_Solution.ipynb | 566 ++++++++++++++++++ 1 file changed, 566 insertions(+) create mode 100644 s24/AmFam_Ashwin/12_Iterations_Practice/Lecture Code/Lec_12_Iteration_Practice_Solution.ipynb diff --git a/s24/AmFam_Ashwin/12_Iterations_Practice/Lecture Code/Lec_12_Iteration_Practice_Solution.ipynb b/s24/AmFam_Ashwin/12_Iterations_Practice/Lecture Code/Lec_12_Iteration_Practice_Solution.ipynb new file mode 100644 index 0000000..5b66540 --- /dev/null +++ b/s24/AmFam_Ashwin/12_Iterations_Practice/Lecture Code/Lec_12_Iteration_Practice_Solution.ipynb @@ -0,0 +1,566 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Warmup 1: Jupyter Notebook stuck in `[*]`?\n", + "\n", + "Press the Stop button! It's probably waiting for input. Otherwise, **restart** the kernel! (and run the cells)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Type in a number: 220\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The number is 220 then 221 then 222 then 223 then 224!\n" + ] + } + ], + "source": [ + "x = int(input(\"Type in a number: \"))\n", + "print(\"The number is \" + str(x), x + 1, x + 2, x + 3, x + 4, sep=\" then \", end=\"!\\n\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Warmup 2: Write a function that prints the factorial of parameter num" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "def do_factorial(num):\n", + " factorial = 1\n", + " while num != 1:\n", + " factorial = factorial * num\n", + " num -= 1\n", + " return factorial" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "6\n", + "24\n" + ] + } + ], + "source": [ + "print(do_factorial(1))\n", + "print(do_factorial(2))\n", + "print(do_factorial(3))\n", + "print(do_factorial(4))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Warmup 3: Complete the code to print a treasure map\n", + "The map should be a grid of size `width` and `height` with `symbol` everywhere, expect at the location: `treasure_row`, `treasure_col`, where an `'X'` is placed." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "----\n", + "----\n", + "--X-\n", + "----\n" + ] + } + ], + "source": [ + "def print_treasure_map(symbol='-', height=4, width=4, treasure_row=2, treasure_col=2):\n", + " i = 0\n", + " while i < height: # TODO: Complete the loop condition for printing out each row.\n", + " j = 0\n", + " while j < width:\n", + " if i == treasure_row and j == treasure_col: # TODO: Complete the if condition for checking if we print an X or the symbol.\n", + " print('X', end=\"\")\n", + " else:\n", + " print(symbol, end=\"\")\n", + " j += 1 # TODO: Complete the statement so we do not run into an infinite loop.\n", + " print()\n", + " i += 1\n", + " \n", + "print_treasure_map()\n", + "# print_treasure_map(width=10, height=10)\n", + "# print_treasure_map('#', 7, 4, treasure_row=0, treasure_col=1)\n", + "# print_treasure_map('.', 5, 8, 3, 6)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# CS220: Lecture 12\n", + "\n", + "\n", + "## Learning Objectives\n", + "After this lecture you will be able to...\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", + "- Use break and continue in for loops when processing 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" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "import project" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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_cats_or_dogs(idx)\n", + " get_cats_or_dogs(idx) returns whether student in row idx likes cats or dogs\n", + " \n", + " get_lecture(idx)\n", + " get_lecture(idx) returns the lecture of the student in row idx\n", + " \n", + " get_other_majors(idx)\n", + " get_other_majors(idx) returns the secondary major of the student in row idx\n", + " \n", + " get_pizza_topping(idx)\n", + " get_pizza_topping(idx) returns the preferred pizza toppings of the student in row idx\n", + " \n", + " get_primary_major(idx)\n", + " get_primary_major(idx) returns the primary major 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_section(idx)\n", + " get_lecture(idx) returns the section of the student in row idx\n", + " \n", + " get_sleep_habit(idx)\n", + " get_sleep_habit(idx) returns the sleep habit of the student in row idx\n", + " \n", + " get_song(idx)\n", + " get_procrastinator(idx) returns the student in row idx favorite song\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': '19', 'Cats or dogs': 'cat', 'Latitude': '44.25...\n", + "\n", + "FILE\n", + " c:\\college\\uw-madison\\spring 2024\\cs220\\lecture code\\12_iteration_practice\\project.py\n", + "\n", + "\n" + ] + } + ], + "source": [ + "help(project)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1019" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Get the total # of responses\n", + "project.count()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Other (please provide details below).'" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Get the first student's primary major.\n", + "# With indices, we always start from 0!\n", + "project.get_primary_major(0)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1: Print and Break" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 Other (please provide details below).\n", + "1 Engineering: Biomedical\n", + "2 Computer Science\n", + "3 Engineering: Other\n", + "4 Data Science\n", + "5 Engineering: Biomedical\n", + "6 Mathematics/AMEP\n", + "7 Engineering: Mechanical\n", + "8 Other (please provide details below).\n", + "9 Other (please provide details below).\n", + "10 Business: Information Systems\n" + ] + } + ], + "source": [ + "for i in range(project.count()):\n", + " print (i, project.get_primary_major(i))\n", + " if i == 10:\n", + " break" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 Other (please provide details below).\n", + "1 Engineering: Biomedical\n", + "2 Computer Science\n", + "3 Engineering: Other\n", + "4 Data Science\n", + "5 Engineering: Biomedical\n", + "6 Mathematics/AMEP\n", + "7 Engineering: Mechanical\n", + "8 Other (please provide details below).\n", + "9 Other (please provide details below).\n", + "10 Business: Information Systems\n" + ] + } + ], + "source": [ + "# TODO: Write the same code as above using a while loop!\n", + "i = 0\n", + "while i <= 10:\n", + " print(i, project.get_primary_major(i))\n", + " i += 1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2: How many students are not in Computer Science?" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "942 out of 1019 are non-cs!\n" + ] + } + ], + "source": [ + "non_cs = 0\n", + "for i in range(project.count()):\n", + " major = project.get_primary_major(i)\n", + " if major == \"Computer Science\":\n", + " continue\n", + " non_cs += 1\n", + "print(non_cs, \"out of\", project.count(), \"are non-cs!\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3: How many students are in Data Science or Statistics?" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "200 out of 1019 are Data Science or Statistics majors!\n", + "19.63% of students are Data Science or Statistics majors!\n" + ] + } + ], + "source": [ + "ds_or_stats = 0\n", + "for i in range(project.count()):\n", + " major = project.get_primary_major(i)\n", + " if major == \"Data Science\" or major == \"Statistics\":\n", + " ds_or_stats += 1\n", + "print(ds_or_stats, \"out of\", project.count(), \"are Data Science or Statistics majors!\")\n", + "# BONUS: Can you express this as a percentage?\n", + "print(str(round(ds_or_stats/project.count()*100, 2)) + \"% of students are Data Science or Statistics majors!\")\n", + "# BONUS+: ...rounded to 2 decimal places?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 4: How many early birds are there below the age of 21?" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "There are 190 early birds below the age of 21.\n", + "There are 791 total students below the age of 21.\n" + ] + } + ], + "source": [ + "early_birds = 0\n", + "total_students = 0\n", + "for i in range(project.count()):\n", + " sleep_habit = project.get_sleep_habit(i)\n", + " age = project.get_age(i)\n", + " if age != \"\":\n", + " age = int(age)\n", + " if age < 21: # TODO Complete this condition!\n", + " total_students += 1\n", + " if sleep_habit == \"early bird\":\n", + " early_birds += 1\n", + " \n", + "print(\"There are\", early_birds, \"early birds below the age of 21.\")\n", + "print(\"There are\", total_students, \"total students below the age of 21.\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5: What percentage of 20-year-olds are early birds?\n", + "\n", + "**Bonus:** How can we generalize our code to 'What percentage of x-year-olds are early birds?'" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "There are 47 early birds that are 20.\n", + "There are 165 total students that are 20.\n", + "28.48% of 20 year olds are early birds\n" + ] + } + ], + "source": [ + "def print_early_birds(target_age):\n", + " early_birds = 0\n", + " total_students = 0\n", + " for i in range(project.count()):\n", + " sleep_habit = project.get_sleep_habit(i)\n", + " age = project.get_age(i)\n", + " if age != \"\":\n", + " age = int(age)\n", + " if age == target_age:\n", + " total_students += 1\n", + " if sleep_habit == \"early bird\":\n", + " early_birds += 1\n", + "\n", + "\n", + " print(\"There are\", early_birds, \"early birds that are 20.\")\n", + " print(\"There are\", total_students, \"total students that are 20.\")\n", + " per_early = round((early_birds / total_students) * 100, 2)\n", + " print(str(per_early) + \"% of \" + str(target_age) + \" year olds are early birds\")\n", + " \n", + " \n", + "print_early_birds(20)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 6: What is the age of the oldest student?" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The oldest student is 40\n" + ] + } + ], + "source": [ + "# Python reserved keyword `max` should not be used as a variable name\n", + "max_age = 0\n", + "for i in range(project.count()):\n", + " if project.get_age(i) == \"\":\n", + " continue\n", + " student_age = int(project.get_age(i))\n", + " if student_age != \"\" and student_age > max_age:\n", + " max_age = student_age \n", + "print(\"The oldest student is\", max_age)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 7: What is the age of the youngest student?" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The youngest student is 17\n" + ] + } + ], + "source": [ + "min_age = 100\n", + "for i in range(project.count()):\n", + " if project.get_age(i) == \"\":\n", + " continue\n", + " student_age = int(project.get_age(i))\n", + " if student_age != \"\" and student_age < min_age:\n", + " min_age = student_age \n", + "print(\"The youngest student is\", min_age)" + ] + } + ], + "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.7" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} -- GitLab