{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "source": [ "# CSCI 127 Final Exam Preparation\n", "Name:" ], "metadata": { "id": "37TdF0oQ53gr" } }, { "cell_type": "markdown", "source": [ "______\n", "Imports required. Do not modify the following code chunk." ], "metadata": { "id": "ECtKVwEJDz5L" } }, { "cell_type": "code", "source": [ "import numpy as np" ], "metadata": { "id": "rDubwpZuDycT" }, "execution_count": 1, "outputs": [] }, { "cell_type": "markdown", "source": [ "## Question 1\n", "This question will test your ability to work with dictionaries and arrays.\n", "\n", "**Dictionaries:** You will need to be able to work with a dictionary containing multiple dictionaries within it. In other words, you will have multiple dictionaries nested within a dictionary. I am going to ask you to use the built in methods to add, remove, and/or modify entries. Practice working with nested dictionaries before the exam.\n", "\n", "**NumPy Arrays:** You will need to be able to work with 1- and 2-dimensional arrays to successfully answer this question. Make sure that you are familiar with creating and modifying arrays. Be sure to review slicing and extracting elements." ], "metadata": { "id": "2ZK3SWmO5ore" } }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "cbrSLsw43ssQ" }, "outputs": [], "source": [ "# Question 1 starter code\n", "\n", "parkingPassType = {1:{\"name\":\"Day Pass\", \"vehicles\":[\"mopeds\", \"motorcycles\", \"cars\", \"trucks\", \"trailers\"], \"cost\":6}, \\\n", " 2: {\"name\":\"Weekly Pass\", \"vehicles\":[\"motorcycles\", \"cars\", \"trucks\"], \"cost\":90}, \\\n", " 3: {\"name\":\"Annual\", \"vehicles\":[\"cars\", \"trucks\"], \"cost\":1200} }\n" ] }, { "cell_type": "markdown", "source": [ "In the code block below,\n", "\n", "\n", "1. Delete the nested dictionary with the ```key``` value of ```1``` from ```parkingPassType```. Print the ```parkingPassType``` dictionary.\n", "2. Create a new dictionary with a key value of ```4``` within the parkingPassType dictionary with a ```name``` of ```Monthly```, a list of ```vehicles``` that includes ```[\"motorcycles\", \"cars\", \"trucks\"]``` and a ```cost``` of ```250```.\n", "3. Create an array that contains the cost of each of the passes in the ```parkingPassType``` dictionary. Set the datatype to be a 32-bit integer. Order the passess from most expensive to least expensive.\n", "\n" ], "metadata": { "id": "BXOAI7tn6OgG" } }, { "cell_type": "code", "source": [ "# Answer to part 1 here:\n", "\n", "\n", "# Answer to part 2 here:\n", "\n", "\n", "# Answer to part 3 here:\n", "\n" ], "metadata": { "id": "1LDQzKgE6OC6" }, "execution_count": 3, "outputs": [] }, { "cell_type": "markdown", "source": [ "## Question 2\n", "This question will test your proficiency with object oriented programming in Python. You will need to be able to work with the common \"dunder methods\". In particular, you must be proficient with ```___init___``` and ```___print___``` methods to successfully answer this question. You will be asked to build up a print method that returns a complex print string from multiple elements in an array, list, tuple, or dictionary.\n", "\n", "You will need to be able to work with ```if``` and ```else``` statements within a method to produce divergent outcomes dependening on the parameters supplied to it.\n", "\n", "---\n", "\n", "1. Write the missing dunder methods. When you select ```2```, the print statement should return the following ```This pass is called the Weekly Pass. Allowable vehicles are motorcycles, cars, and trucks, and costs $90.```\n", "\n", "2. Correct the input statement for ```pass_idx``` according to the instructions in the comment below." ], "metadata": { "id": "FLWIbzeL6fAi" } }, { "cell_type": "code", "source": [ "class ParkingPassInfo():\n", "\n", " # Provide the missing \"dunder methods\" here\n", "\n", " def park_pass(self):\n", " if self.vehicles != [] and self.cost > 0:\n", " self.result = self.vehicles_string_builder()\n", " else:\n", " self.result = \"ERROR: problem with the pass...\"\n", "\n", " def vehicles_string_builder(self):\n", " self.vehicles_string = \"\"\n", " for i in range(len(self.vehicles)-1):\n", " self.vehicles_string += self.vehicles[i] + \", \"\n", " self.vehicles_string += \"and \" + self.vehicles[len(self.vehicles)-1]\n", "\n", "def main():\n", "\n", " # Debug this next line of code so that when you select 1, it returns the Monthly pass\n", " pass_idx = int(input(\"Select 1 for \" + parkingPassType.get(1).get(\"name\") + \". \\n\" +\\\n", " \"Select 2 for \" + parkingPassType.get(2).get(\"name\") + \". \\n\"+\\\n", " \"Select 3 for \"+ parkingPassType.get(3).get(\"name\")+ \". \\n\",\\\n", " ))\n", "\n", " park = ParkingPassInfo(parkingPassType.get(pass_idx).get(\"vehicles\"), \\\n", " parkingPassType.get(pass_idx).get(\"cost\"), \\\n", " parkingPassType.get(pass_idx).get(\"name\"))\n", " park.park_pass()\n", " print(park)\n", "\n", "\n", "main()\n", "\n", "\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 407 }, "id": "lDEzCemh6oPN", "outputId": "086c4d17-e721-4862-ce7b-0831fce1111a" }, "execution_count": 7, "outputs": [ { "output_type": "error", "ename": "KeyboardInterrupt", "evalue": "ignored", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 29\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 30\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 31\u001b[0;31m \u001b[0mmain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 32\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 33\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mmain\u001b[0;34m()\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0;31m# Debug this next line of code.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 19\u001b[0;31m pass_idx = int(input(\"Select 1 for \" + parkingPassType.get(1).get(\"name\") + \". \\n\" +\\\n\u001b[0m\u001b[1;32m 20\u001b[0m \u001b[0;34m\"Select 2 for \"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mparkingPassType\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"name\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m\". \\n\"\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;31m\\\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[0;34m\"Select 3 for \"\u001b[0m\u001b[0;34m+\u001b[0m \u001b[0mparkingPassType\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"name\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m+\u001b[0m \u001b[0;34m\". \\n\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;31m\\\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 849\u001b[0m \u001b[0;34m\"raw_input was called, but this frontend does not support input requests.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 850\u001b[0m )\n\u001b[0;32m--> 851\u001b[0;31m return self._input_request(str(prompt),\n\u001b[0m\u001b[1;32m 852\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 853\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 893\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 894\u001b[0m \u001b[0;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 895\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Interrupted by user\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 896\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 897\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwarning\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Invalid Message:\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexc_info\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mKeyboardInterrupt\u001b[0m: Interrupted by user" ] } ] } ] }