diff --git a/s24/AmFam_Ashwin/16_List_Practice/Lec_16_List_Practice_Worksheet.ipynb b/s24/AmFam_Ashwin/16_List_Practice/Lec_16_List_Practice_Worksheet.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..02f183a1671eae75102f9ce6e96f2b9fad2bacfd --- /dev/null +++ b/s24/AmFam_Ashwin/16_List_Practice/Lec_16_List_Practice_Worksheet.ipynb @@ -0,0 +1,261 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Lecture 16 Worksheet Solutions\n", + "\n", + "You should do the worksheet by hand, then check your work." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem 1:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "nums = [100, 2, 3, 40, 99]\n", + "words = [\"three\", \"two\", \"one\"]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Solutions:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "99\n", + "[2, 3]\n", + "two\n", + "w\n", + "www\n", + "\n", + "1\n", + "2\n", + "[100, 'three']\n", + "three,two,one\n", + "e,t\n" + ] + } + ], + "source": [ + "print(nums[-1])\n", + "print(nums[1:3])\n", + "print(words[1])\n", + "print(words[1][1])\n", + "print(words[1][-2] * nums[2])\n", + "print()\n", + "print(words.index(\"two\"))\n", + "print(nums[words.index(\"two\")])\n", + "print(nums[:1] + words[:1])\n", + "print(\",\".join(words))\n", + "print((\",\".join(words))[4:7])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem 2:" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "rows = [[\"x\", \"y\",\"name\"], [3,4,\"Alice\"], [9,1,\"Bob\"], [-3,4,\"Cindy\"]]\n", + "header = rows[0]\n", + "data = rows[1:]\n", + "X = 0\n", + "Y = 1\n", + "NAME = 2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Solutions:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n", + "3\n", + "3\n", + "Alice\n", + "Bob\n", + "\n", + "2\n", + "Cindy\n", + "3.0\n", + "5.0\n", + "Alice\n" + ] + } + ], + "source": [ + "print(len(rows))\n", + "print(len(data))\n", + "print(len(header))\n", + "print(rows[1][-1])\n", + "print(data[1][-1])\n", + "print()\n", + "print(header.index(\"name\"))\n", + "print(data[-1][header.index(\"name\")])\n", + "print((data[0][X] + data[1][X] + data[2][X]) / 3)\n", + "print((data[-1][X] ** 2 + data[-1][Y] ** 2) ** 0.5)\n", + "print(min(data[0][NAME], data[1][NAME], data[2][NAME]))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem 3:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "rows = [ [\"Food Science\", \"24000\", \"0.049188446\", \"62000\"],\n", + " [\"CS\", \"783000\", \"0.049518657\", \"78000\"],\n", + " [\"Microbiology\", \"70000\", \"0.050880749\", \"60000\"],\n", + " [\"Math\", \"433000\", \"0.05293608\", \"66000\"] ]\n", + "hd = [\"major\", \"students\", \"unemployed\", \"salary\"]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Solutions:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CS\n", + "433000\n", + "True\n", + "2400070000\n" + ] + } + ], + "source": [ + "print(rows[1][0])\n", + "print(rows[3][hd.index(\"students\")])\n", + "print(len(hd) == len(rows[1]))\n", + "print(rows[0][1] + rows[2][1])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem 4:" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "rows = [ [\"city\", \"state\", \"y14\", \"y15\"],\n", + " [\"Chicago\", \"Illinois\", \"411\", \"478\"],\n", + " [\"Milwaukee\", \"Wisconsin\", \"90\", \"145\"],\n", + " [\"Detroit\", \"Michigan\", \"298\", \"295\"] ]\n", + "hd = rows[0]\n", + "rows = rows[1:] # this removes the header and stores the result in rows" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Solutions:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Chicago\n", + "411\n", + "False\n", + "Detroit, Michigan\n" + ] + } + ], + "source": [ + "print(rows[0][hd.index(\"city\")])\n", + "print(rows[0][hd.index(\"y14\")])\n", + "print(rows[2][hd.index(\"y14\")] < rows[2][hd.index(\"y15\")])\n", + "print(\", \".join(rows[-1][:2]))" + ] + } + ], + "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 +}