diff --git a/s24/AmFam_Ashwin/16_List_Practice/Lecture Code/Lec_16_List_Practice_Template.ipynb b/s24/AmFam_Ashwin/16_List_Practice/Lecture Code/Lec_16_List_Practice_Template.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..d7ecd86d99ba18df1744dbd72f0f9cb2abd9dd27 --- /dev/null +++ b/s24/AmFam_Ashwin/16_List_Practice/Lecture Code/Lec_16_List_Practice_Template.ipynb @@ -0,0 +1,512 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Lists Practice" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Warmup 0: Hotkeys\n", + "\n", + "We move quickly, it's good to know some hotkeys!\n", + "\n", + "#### All-around good-to-knows...\n", + "* `Ctrl`+`A`: Select all the text in a cell.\n", + "* `Ctrl`+`C`: Copy selected text.\n", + "* `Ctrl`+`X`: Cut selected text.\n", + "* `Ctrl`+`V`: Paste text from clipboard.\n", + "* `Ctrl`+`S`: Save.\n", + "\n", + "#### Jupyter-specific good-to-knows...\n", + "* `Ctrl`+`Enter` or `Shift`+`Enter`: Run Cell\n", + "* `Ctrl`+`/`: Comment/uncomment sections of code.\n", + "* `Esc` -> `Shift`+`L`: Toggle line numbers.\n", + "* Select text -> `Tab`: Add extra level of indent.\n", + "* Select text -> `Shift`+`Tab`: Remove level of indent." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Warmup 1: Create an empty list and add elements to it" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "weekend_plans = [] # I have no weekend plans :(\n", + "print(weekend_plans)\n", + "\n", + "# TODO add three things to your weekend plans\n", + "\n", + "print(weekend_plans)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Warmup 2: Sets\n", + "\n", + "Like a **list**, a **set** is another collection. However, it is **unordered** and **unique**." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "my_set_of_weekend_plans = set()\n", + "\n", + "# TODO: add 4 weekend plans, 1 of which is a duplicate.\n", + "\n", + "print(my_set_of_weekend_plans)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### We can `pop` to remove elements, but it will remove a random item.\n", + "See: https://www.w3schools.com/python/trypython.asp?filename=demo_ref_set_pop2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "what_removed = my_set_of_weekend_plans.pop()\n", + "\n", + "print(what_removed)\n", + "print(my_set_of_weekend_plans)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### We can `discard` a specific item!\n", + "\n", + "Unlike a list's `remove`, this will **not** throw an error if the element does not exist." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "my_set_of_weekend_plans.discard(...)\n", + "print(my_set_of_weekend_plans)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Copy/paste the `cell` function from the last lecture" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import csv\n", + "\n", + "# source: Automate the Boring Stuff with Python Ch 12\n", + "def process_csv(filename):\n", + " exampleFile = open(filename, encoding=\"utf-8\") \n", + " exampleReader = csv.reader(exampleFile) \n", + " exampleData = list(exampleReader) \n", + " exampleFile.close() \n", + " return exampleData" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def cell(row_idx, col_name):\n", + " pass" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Warmup 4: Does the oldest basil/spinach-loving Business major prefer cats, dogs, or neither?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Warmup 5: Is their city in the United States?\n", + "\n", + "The United States has **latitudes** approximately spanning from `23.101` to `49.632`, and **longitudes** approximately spanning from `-129.306` to `-65.017`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Restaurants" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "restaurant_csv = process_csv('restaurants.csv')\n", + "\n", + "# TODO: Display restaurant_csv. What do we call this data structure?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Separate the data into 2 parts: a header row, and a list of data rows" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "restaurant_header = ...\n", + "restaurant_data = ..." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1: Make a list of just the names from restaurant_data" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "names = [] # names starts out empty, we will append to it\n", + "for row in restaurant_data:\n", + " pass\n", + "names" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2: Extract the list of unique restaurant names " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "names = ...\n", + "print(names)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3: Sort the list of unique restaurant names" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Sorting Option 1: Print the sorted list without changing it" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(sorted(names))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Sorting Option 2: Sort the list and then print it" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "names.sort()\n", + "print(names)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 4: Define a `cell` function to extract the data in `restaurant_data`\n", + "\n", + "Make sure it returns `None` if there is **missing** data. Make sure it **typecasts** appropriately depending on the **column name**." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def cell(row_idx, col_name):\n", + " col_idx = restaurant_header.index(col_name)\n", + " val = restaurant_data[row_idx][col_idx]\n", + " if ...:\n", + " return None\n", + " elif ...:\n", + " return int(val)\n", + " else:\n", + " return val" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5: Write a function that is sent x y coordinates and returns back the `restaurant_id` of that restaurant." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def get_restaurant_at_coordinates(search_x, search_y):\n", + " for i in range(len(restaurant_data)):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(get_restaurant_at_coordinates(1, 3)) # should be EIN_1\n", + "print(get_restaurant_at_coordinates(0, -3)) # should be GRE_1\n", + "print(get_restaurant_at_coordinates(2, -3)) # should be None" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 6: Write a function that is sent the restaurant ID of a restaurant and returns the x and y coordinates as a string.\n", + "\n", + "This should be **case-insensitive**." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def get_coordinates(restaurant_id):\n", + " for i in range(len(restaurant_data)):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(get_coordinates(\"GRE_1\")) # should be (0, -3)\n", + "print(get_coordinates(\"MCD_2\")) # should be (2, 0)\n", + "print(get_coordinates(\"mcd_2\")) # should be (2, 0)\n", + "print(get_coordinates(\"PAN_1\")) # should be None\n", + "print(get_coordinates(\"ZZZ_123\")) # should be None" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 7: Define `get_smallest_index` to get the INDEX of the smallest value in `col_name` (such as `'x_coord'`)\n", + "\n", + "If there are ties, use the last value in the dataset." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def get_smallest_index(col_name):\n", + " pass" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### What is the name of the restaurant farthest to the west?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# use `get_smallest_index` to find the answer here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### What is the restaurant ID of the restaurant farthest to the south?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# use `get_smallest_index` to find the answer here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 8: Complete this function that computes the distance between `(x1,y1)` and `(x2,y2)`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import math\n", + "def distance(x1, y1, x2, y2):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(distance(0, 0, 3, 4)) # should be 5.0\n", + "print(distance(1, 2, 2, 3)) # should be square root of 2\n", + "print(distance(-3, 3, 2, -9)) # should be 13.0" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 9: Write a function that is sent x and y coordinates and returns the name of the closest restaurant to those coordinates.\n", + "\n", + "Use the `distance` function to calculate the distance." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def closest_restaurant(source_x, source_y):\n", + " '''return the name of the closest restaurant to the parameters given'''\n", + " closest_index = None # start with no value, to be clear if no result found\n", + " min_dist = None # why does this have to be None, not just 0?\n", + " \n", + " for i in range(len(restaurant_data)):\n", + " current_x = cell(i, \"x_coord\")\n", + " current_y = cell(i, \"y_coord\")\n", + " if ...: # check for missing data\n", + " continue\n", + " current_dist = ...\n", + " \n", + " if ...:\n", + " closest_index = i\n", + " min_dist = current_dist\n", + " \n", + " return cell(closest_index, \"name\") # Bonus: fix this to make the function more useful" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(closest_restaurant(3, 3)) # should be Einsteins Bagels\n", + "print(closest_restaurant(0, 0)) # should be Starbucks\n", + "print(closest_restaurant(5, -2)) # should be McDonalds\n", + "print(closest_restaurant(1, -2)) # should be Greenbush Donuts" + ] + } + ], + "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 +}