From 23a7bf386c4b88b473b0e51d67288f6d0e3ebbdb Mon Sep 17 00:00:00 2001 From: Ashwin Maran <amaran@wisc.edu> Date: Sun, 18 Feb 2024 16:39:28 -0600 Subject: [PATCH] Upload New File --- .../Lec_12_Iteration_Practice_Template.ipynb | 310 ++++++++++++++++++ 1 file changed, 310 insertions(+) create mode 100644 s24/AmFam_Ashwin/12_Iterations_Practice/Lecture Code/Lec_12_Iteration_Practice_Template.ipynb diff --git a/s24/AmFam_Ashwin/12_Iterations_Practice/Lecture Code/Lec_12_Iteration_Practice_Template.ipynb b/s24/AmFam_Ashwin/12_Iterations_Practice/Lecture Code/Lec_12_Iteration_Practice_Template.ipynb new file mode 100644 index 0000000..75cb347 --- /dev/null +++ b/s24/AmFam_Ashwin/12_Iterations_Practice/Lecture Code/Lec_12_Iteration_Practice_Template.ipynb @@ -0,0 +1,310 @@ +{ + "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": null, + "metadata": {}, + "outputs": [], + "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": null, + "metadata": {}, + "outputs": [], + "source": [ + "def do_factorial(num):\n", + " pass" + ] + }, + { + "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": null, + "metadata": {}, + "outputs": [], + "source": [ + "def print_treasure_map(symbol='-', height=4, width=4, treasure_row=2, treasure_col=2):\n", + " i = 0\n", + " while ...: # TODO: Complete the loop condition for printing out each row.\n", + " j = 0\n", + " while j < width:\n", + " if ...: # 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 += ... # 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": null, + "metadata": {}, + "outputs": [], + "source": [ + "import project" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "help(project)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Get the total # of responses\n", + "project.count()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "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": null, + "metadata": {}, + "outputs": [], + "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": null, + "metadata": {}, + "outputs": [], + "source": [ + "# TODO: Write the same code as above using a while loop!" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2: How many students are not in Computer Science?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "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?\n", + "\n", + "**BONUS:** Can you express this as a percentage?\n", + "\n", + "**BONUS+:** ...rounded to 2 decimal places?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# write your code here" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 4: How many early birds are there below the age of 21?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "early_birds = 0\n", + "for i in range(project.count()):\n", + " sleep_habit = project.get_sleep_habit(i)\n", + " age = project.get_age(i)\n", + " if ...: # TODO Complete this condition!\n", + " early_birds += 1\n", + " \n", + "print(\"There are\", early_birds, \"early birds 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": null, + "metadata": {}, + "outputs": [], + "source": [ + "# write your code here" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 6: What is the age of the oldest student?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "max = 0\n", + "for i in range(project.count()):\n", + " student_age = int(project.get_age(i))\n", + " if student_age != \"\" and student_age > max:\n", + " max = student_age \n", + "print(\"The oldest student is\", max)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 7: What is the age of the youngest student?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# write your code here" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Other tasks for you to try...\n", + "- What is the average age of the class?\n", + "- What is the class's favorite pizza topping?\n", + "- What is the most popular major?\n", + "- For all students that have another major, print out what it is.\n", + "- What zip code do the majority of pineapple-eating night owls live in?" + ] + } + ], + "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