You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
448 lines
18 KiB
448 lines
18 KiB
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Day 2 In-Class Homework"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"We'll be building on older material like lists, loops, and functions, as well as adding in some practice from the new materials.\n",
|
|
"\n",
|
|
"You will practice these concepts:\n",
|
|
"\n",
|
|
"* Using functions with parameters.\n",
|
|
"* Using dictionaries and sets.\n",
|
|
"* Declaring and using classes to make objects.\n",
|
|
"* Demonstrating inheritance and method overriding."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Problem 1: Call Me - Maybe!\n",
|
|
"\n",
|
|
"#### Skills you're practicing: Accessing dictionaries, writing functions, and passing parameters.\n",
|
|
"\n",
|
|
"- Write a function called `get_contact()` that has two parameters. \n",
|
|
"- The first will be a dictionary called `contacts` and the second will be a string called `name` for which you would like to look up the phone number.\n",
|
|
"- If the name is not found in the dictionary, print that the requested name was not found in the contacts dictionary.\n",
|
|
"\n",
|
|
"#### Example Test Code\n",
|
|
"```python\n",
|
|
"contacts = {\n",
|
|
" \"Carly\": \"333-3333\",\n",
|
|
" \"Blondie\": \"444-4444\",\n",
|
|
" \"Jenny\": \"867-5309\"\n",
|
|
"}\n",
|
|
"name = \"Jenny\"\n",
|
|
"\n",
|
|
"phone_number = get_contact(contacts, name)\n",
|
|
"```\n",
|
|
"\n",
|
|
"#### Example test output:\n",
|
|
"```\n",
|
|
"The phone number of Jenny is 867-5309\n",
|
|
"```\n",
|
|
"```\n",
|
|
"The phone number of Greg is not in the contacts dictionary\n",
|
|
"```\n",
|
|
"\n",
|
|
"**Note:**\n",
|
|
"\n",
|
|
"Careful! Python requires that you insert a key into a dictionary before you try to modify its value. If you try to access a dictionary at a key that hasn't been added, you'll get an error and the program will crash. Remember to use an `if` statement to see if a key is _in_ a dictionary before you try to read it!\n",
|
|
"\n",
|
|
"```python\n",
|
|
"d2 = {}\n",
|
|
"d2[\"foo\"]\n",
|
|
"# KeyError: 'foo'\n",
|
|
"```\n",
|
|
"\n",
|
|
"Checking for `\"foo\"` key in d2:\n",
|
|
"```\n",
|
|
"if \"foo\" in d2:\n",
|
|
" pass\n",
|
|
"```"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# A:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Problem 2: Hey Jude, Don't Make Me Count!\n",
|
|
"\n",
|
|
"#### Skills you're practicing: Using dictionaries and writing a function with a parameter.\n",
|
|
"\n",
|
|
"Write a function called `letter_counter()` that receives a string and returns a dictionary called `counts`, which has a character as the key and a count as a value. The goal is to count up the number of occurrences of each letter in a string.\n",
|
|
"\n",
|
|
"Here is how your function will be called, and what is expected as the output.\n",
|
|
"\n",
|
|
"#### Example test code:\n",
|
|
"```python\n",
|
|
"word_to_count = \"banana\"\n",
|
|
"\n",
|
|
"result = letter_counter(word_to_count)\n",
|
|
"\n",
|
|
"print(result)\n",
|
|
"```\n",
|
|
"\n",
|
|
"#### Example test output:\n",
|
|
"```python\n",
|
|
"{'b': 1, 'a': 3, 'n': 2}\n",
|
|
"```\n",
|
|
"\n",
|
|
"**Hint 1:**\n",
|
|
"\n",
|
|
"Remember that you can iterate over a string one letter at a time using a `for` loop.\n",
|
|
"```python\n",
|
|
"for letter in \"alpha\":\n",
|
|
" print(letter)\n",
|
|
"```\n",
|
|
"\n",
|
|
"**Hint 2:**\n",
|
|
"\n",
|
|
"Remember to write a return statement at the end of your function. No code will execute after the return statement because the return statement also exits the function. What you return from the function will be assigned to the `result` variable in the example above.\n",
|
|
"\n",
|
|
"**Hint 3:**\n",
|
|
"\n",
|
|
"Think you have the answer? To really test your function, try putting in a whole song as your string! Here are the lyrics to [\"Hey Jude\"](https://repl.it/@GAcoding/Python-02-HW-1) already written into a Python string. Here's the expected output:\n",
|
|
"\n",
|
|
"```python\n",
|
|
"{'H': 4, 'e': 143, 'y': 45, ' ': 368, 'J': 23, 'u': 53, 'd': 52, ',': 44, 'o': 69, 'n': 191, \"'\": 11, 't': 84, 'm': 23, 'a': 205, 'k': 15, 'i': 33, 'b': 19, '.': 20, 'T': 7, 's': 18, 'g': 11, 'r': 49, 'R': 3, 'l': 24, 'h': 208, 'c': 5, 'f': 7, 'Y': 3, 'w': 11, 'A': 2, 'p': 4, 'D': 1, 'F': 1, 'B': 2, 'N': 18, 'v': 2, 'S': 1, 'j': 1}\n",
|
|
"```"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# A:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Problem 5: Ready, **SET**, Go!\n",
|
|
"\n",
|
|
"### Skill you're practicing: Using sets.\n",
|
|
"\n",
|
|
"Let's say you own a restaurant. You ask the manager to take stock of the spices you have on hand so you know what you need to buy. Unfortunately, you get a list with a bunch of duplicates!\n",
|
|
"\n",
|
|
"- Convert the `spices_onhand` list to a set. \n",
|
|
"- Given the `spices_needed` set, print out what you need to buy (what is in the `spices_needed` set that is _not_ in the `spices_onhand` set\n",
|
|
"\n",
|
|
"```python\n",
|
|
"spices_needed = set({\"salt\", \"pepper\", \"ginger\", \"oregano\", \"paprika\", \"basil\", \"curry powder\", \"cumin\", \"cayenne\", \"lemon pepper\", \"chili powder\", \"nutmeg\", \"cinnamon\", \"star anise\", \"garlic salt\", \"coriander\", \"cardamom\", \"thyme\"})\n",
|
|
"```\n",
|
|
"\n",
|
|
"#### Example test input:\n",
|
|
"```python\n",
|
|
"spices_onhand = ['cumin', 'nutmeg', 'salt', 'cumin', 'star anise', 'salt', 'basil', 'nutmeg', 'cumin', 'paprika', 'curry powder', 'pepper', 'curry powder', 'curry powder', 'cayenne', 'cumin', 'star anise', 'star anise', 'curry powder', 'salt', 'salt', 'cardamom', 'cayenne', 'star anise', 'chili powder', 'curry powder', 'thyme', 'thyme', 'cayenne', 'nutmeg', 'basil', 'star anise', 'chili powder', 'oregano', 'coriander', 'nutmeg', 'chili powder', 'coriander', 'paprika', 'pepper', 'thyme', 'nutmeg', 'paprika', 'cayenne', 'basil', 'cinnamon', 'curry powder', 'cardamom', 'star anise', 'pepper', 'salt', 'curry powder', 'thyme', 'cardamom', 'salt', 'pepper', 'paprika', 'salt', 'cinnamon', 'cumin', 'curry powder', 'cardamom', 'cumin', 'cardamom', 'oregano', 'cardamom', 'pepper', 'star anise', 'pepper', 'cayenne', 'chili powder', 'cardamom', 'nutmeg', 'pepper', 'cardamom', 'curry powder', 'thyme', 'basil', 'nutmeg', 'coriander', 'paprika', 'curry powder', 'cayenne', 'cumin', 'nutmeg', 'paprika', 'star anise', 'thyme', 'curry powder', 'cardamom', 'oregano', 'basil', 'cinnamon', 'oregano', 'coriander', 'curry powder', 'cumin', 'thyme', 'pepper', 'thyme', 'cardamom', 'cayenne', 'chili powder', 'basil', 'pepper', 'cumin', 'thyme', 'cardamom', 'star anise', 'cayenne', 'cinnamon', 'cinnamon', 'cinnamon', 'cardamom', 'curry powder', 'curry powder', 'pepper', 'chili powder', 'pepper', 'cinnamon', 'cardamom', 'basil', 'thyme', 'cinnamon', 'cumin', 'nutmeg', 'cinnamon', 'cayenne', 'cardamom', 'nutmeg', 'cardamom', 'paprika', 'cumin', 'cayenne', 'chili powder', 'cinnamon', 'cumin', 'star anise', 'cardamom', 'thyme', 'basil', 'paprika', 'basil', 'oregano', 'cardamom', 'pepper', 'oregano', 'nutmeg', 'nutmeg', 'salt', 'basil', 'cayenne', 'oregano', 'star anise', 'star anise', 'oregano', 'salt', 'pepper', 'cinnamon', 'basil', 'salt', 'cardamom', 'cayenne', 'oregano', 'cinnamon', 'pepper', 'cumin', 'thyme', 'thyme', 'oregano', 'oregano', 'star anise', 'paprika', 'thyme', 'cinnamon', 'cinnamon', 'oregano', 'star anise', 'oregano', 'chili powder', 'cayenne', 'oregano', 'cumin', 'paprika', 'nutmeg', 'star anise', 'coriander', 'star anise', 'nutmeg', 'chili powder', 'star anise', 'paprika', 'salt', 'salt', 'cayenne', 'curry powder', 'thyme', 'oregano', 'curry powder', 'curry powder']\n",
|
|
"```\n",
|
|
"\n",
|
|
"#### Example test output:\n",
|
|
"\n",
|
|
"```\n",
|
|
"Go shopping for: {'garlic salt', 'ginger', 'lemon pepper'}\n",
|
|
"```\n",
|
|
"\n",
|
|
"**Hint:**\n",
|
|
"\n",
|
|
"Look up [set difference](https://www.geeksforgeeks.org/python-set-difference/). This is basically subtracting the items of one set from another. If you had two sets — one of ingredients you need and one of ingredients you have — you could subtract the items you have from the ones you need in order to know what you will have to purchase."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# A:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Problem 3: Kingdom, Phylum, **CLASS**!\n",
|
|
"\n",
|
|
"#### Skills you're practicing: Making classes and creating instances.\n",
|
|
"\n",
|
|
"Let's create a class. Remember a class is a pattern for an object. In this case, we will create an `Animal` class that will be able to generate any number of animal instances with the same properties.\n",
|
|
"\n",
|
|
"* Create an `Animal` class.\n",
|
|
" * When an animal is created, print `I am coming to life!`.\n",
|
|
" * Animals keep track of their current `energy` as an integer (this is assigned as a class instance variable).\n",
|
|
" * Animals have an `eat()` method, which takes one numerical argument, `amount`, and increases the `energy` by `amount`.\n",
|
|
" * Animals have a `say_hi()` method that prints `Meep!`.\n",
|
|
" * Animals have a `move()` method, which depletes the `energy` by 10 and prints the statement `I am running!`.\n",
|
|
" * Animals have a `get_status()` method, which prints the current `energy` level as well as a message about their energy state. If the `energy` level is:\n",
|
|
" * Below `0`: Print `I'm starving!`.\n",
|
|
" * Between `0` and `50`: Print `I'm getting hungry!`.\n",
|
|
" * Between `50` and `100`: Print `I'm happily full.`.\n",
|
|
" * Above `100`: Print `I'm feeling stuffed!`.\n",
|
|
" * Animals are created with a default `energy` of `50`.\n",
|
|
"\n",
|
|
"#### Example test code:\n",
|
|
"```python\n",
|
|
"print(\"Making my first animal\")\n",
|
|
"first_animal = Animal()\n",
|
|
"first_animal.get_status()\n",
|
|
"first_animal.eat(55)\n",
|
|
"first_animal.get_status()\n",
|
|
"first_animal.move()\n",
|
|
"first_animal.get_status()\n",
|
|
"print(\"first animal saying hi\")\n",
|
|
"first_animal.say_hi()\n",
|
|
"print()\n",
|
|
"```\n",
|
|
"#### Example test output:\n",
|
|
"```\n",
|
|
"Making my first animal\n",
|
|
"I'm coming to life!\n",
|
|
"My energy level is 50\n",
|
|
"I'm happily full.\n",
|
|
"My energy level is 105\n",
|
|
"I'm feeling stuffed!\n",
|
|
"I am running!\n",
|
|
"My energy level is 95\n",
|
|
"I'm happily full.\n",
|
|
"first animal saying hi\n",
|
|
"Meep!\n",
|
|
"```\n",
|
|
"\n",
|
|
"\n",
|
|
"#### Example test code:\n",
|
|
"```python\n",
|
|
"print(\"Making a second animal now\")\n",
|
|
"second_animal = Animal()\n",
|
|
"second_animal.get_status()\n",
|
|
"second_animal.eat(-45)\n",
|
|
"second_animal.get_status()\n",
|
|
"second_animal.move()\n",
|
|
"second_animal.get_status()\n",
|
|
"print(\"second animal saying hi\")\n",
|
|
"second_animal.say_hi()\n",
|
|
"```\n",
|
|
"\n",
|
|
"#### Example test output:\n",
|
|
"```\n",
|
|
"Making a second animal now\n",
|
|
"I'm coming to life!\n",
|
|
"My energy level is 50\n",
|
|
"I'm happily full.\n",
|
|
"My energy level is 5\n",
|
|
"I'm getting hungry!\n",
|
|
"I am running!\n",
|
|
"My energy level is -5\n",
|
|
"I'm starving!\n",
|
|
"second animal saying hi\n",
|
|
"Meep!\n",
|
|
"```\n",
|
|
"\n",
|
|
"**Hint:**\n",
|
|
"\n",
|
|
"Don't forget the constructor! This is the code that runs when we are creating an instance. The constructor in Python is `__init__`.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# A:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Problem 4: Fly Like an Eagle\n",
|
|
"\n",
|
|
"### Skill you're practicing: Using inheritance.\n",
|
|
"\n",
|
|
"Let's practice writing classes and using inheritance by modeling different types of animals. Use your `Animal` class as the parent class from which your new classes, `Penguin` and `Eagle`, will inherit.\n",
|
|
"\n",
|
|
"* Create a `Penguin` class that **inherits** from `Animal`.\n",
|
|
" * `Penguin`s are created with an `energy` level of `100`.\n",
|
|
" * `Penguin`s shout, `I am a penguin!` when they are are created.\n",
|
|
" * A `Penguin`'s `move()` method depletes the `energy` by `5` and prints `I am sliding!`.\n",
|
|
"* Create an `Eagle` class that **inherits** from `Animal`.\n",
|
|
" * An `Eagle` begins its life with an `energy` level of `20`.\n",
|
|
" * `Eagle`s shout, `I am an eagle!` when they are are created.\n",
|
|
" * An `Eagle`'s `move()` method depletes the `energy` by `20` and prints `I am flying to the sea!`.\n",
|
|
" * An `Eagle`s `say_hi()` method prints `shrieeeeeek!`.\n",
|
|
" * `Eagle`s cannot `move` if their `energy` is less than `0`. Instead print `I'm too tired to fly…`.\n",
|
|
"\n",
|
|
"#### Example Animal test code:\n",
|
|
"```python\n",
|
|
"animal = Animal()\n",
|
|
"animal.get_status()\n",
|
|
"animal.eat(60)\n",
|
|
"animal.get_status()\n",
|
|
"animal.move()\n",
|
|
"animal.get_status()\n",
|
|
"animal.say_hi()\n",
|
|
"print()\n",
|
|
"```\n",
|
|
"#### Example Animal test output:\n",
|
|
"```\n",
|
|
"I'm coming to life!\n",
|
|
"My energy level is 50\n",
|
|
"I'm happily full.\n",
|
|
"My energy level is 110\n",
|
|
"I'm feeling stuffed!\n",
|
|
"I am running!\n",
|
|
"My energy level is 100\n",
|
|
"I'm happily full.\n",
|
|
"Meep!\n",
|
|
"```\n",
|
|
"\n",
|
|
"#### Example Penguin test code:\n",
|
|
"```python\n",
|
|
"penguin = Penguin()\n",
|
|
"penguin.eat(5)\n",
|
|
"penguin.get_status()\n",
|
|
"penguin.move()\n",
|
|
"penguin.get_status()\n",
|
|
"penguin.say_hi()\n",
|
|
"print()\n",
|
|
"```\n",
|
|
"#### Example Penguin test output:\n",
|
|
"```\n",
|
|
"I'm coming to life!\n",
|
|
"I am a penguin!\n",
|
|
"My energy level is 105\n",
|
|
"I'm feeling stuffed!\n",
|
|
"I am sliding!\n",
|
|
"My energy level is 100\n",
|
|
"I'm happily full.\n",
|
|
"Meep!\n",
|
|
"```\n",
|
|
"\n",
|
|
"#### Example Eagle test code:\n",
|
|
"```python\n",
|
|
"eagle = Eagle()\n",
|
|
"eagle.say_hi()\n",
|
|
"eagle.get_status()\n",
|
|
"eagle.move()\n",
|
|
"eagle.get_status()\n",
|
|
"eagle.move()\n",
|
|
"eagle.get_status()\n",
|
|
"eagle.move()\n",
|
|
"print()\n",
|
|
"```\n",
|
|
"#### Example Eagle test output:\n",
|
|
"```\n",
|
|
"I'm coming to life!\n",
|
|
"I am an eagle!\n",
|
|
"Shrieeeeek!\n",
|
|
"My energy level is 20\n",
|
|
"I'm getting hungry!\n",
|
|
"I am flying to the sea!\n",
|
|
"My energy level is 0\n",
|
|
"I'm getting hungry!\n",
|
|
"I am flying to the sea!\n",
|
|
"My energy level is -20\n",
|
|
"I'm starving!\n",
|
|
"I'm too tired to fly…\n",
|
|
"```\n",
|
|
"\n",
|
|
"**Hint 1:**\n",
|
|
"\n",
|
|
"Don't forget to call the parent's constructor! You can do this by calling `super().__init__()` in the child class' constructor.\n",
|
|
"\n",
|
|
"**Hint 2:**\n",
|
|
"\n",
|
|
"Remember, you only need to write a method in the child class if it is **different** than the one in the parent class. For example, the `Penguin` class does not need its own `say_hi()` method because we know our `Animal` class has a `say_hi()` method already that does exactly what we want it to do. On the contrary, with `Eagle`, we will have to override the `say_hi()` method in order to make it do what we want instead of the default behavior."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# A:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Problem 6: Kwargosaurus\n",
|
|
"\n",
|
|
"### Skills you're practicing: Using variable number of keyword arguments (`**kwargs**`) and writing functions.\n",
|
|
"\n",
|
|
"The mighty Kwargosaurus stomps through the prehistoric Pythonic jungle, fighting with any dinosaur smaller than him. However, if a dinosaur his own age comes along, he cries and runs away! Write a function called `kwargosaurus()` that takes in a variable number of other dinosaurs for Kwargosaurus to encounter, with the key being the dinosaur name and the value being either `bigger` or `smaller`.\n",
|
|
"\n",
|
|
"#### Example function call:\n",
|
|
"\n",
|
|
"```python\n",
|
|
"kwargosaurus(Velociraptor=\"smaller\", Stegosaurus=\"smaller\", Triceratops=\"smaller\", Trex=\"bigger\")\n",
|
|
"```\n",
|
|
"\n",
|
|
"#### Example output:\n",
|
|
"\n",
|
|
"```\n",
|
|
"Velociraptor is small! Mighty Kwargosaurus will fight you!\n",
|
|
"Stegosaurus is small! Mighty Kwargosaurus will fight you!\n",
|
|
"Triceratops is small! Mighty Kwargosaurus will fight you!\n",
|
|
"Trex is big! Whimpering Kwargosaurus cries and runs away!\n",
|
|
"```\n",
|
|
"\n",
|
|
"**Hint:** Remember that, for a variable number of arguments, you need a loop! You can access both the key and value of each parameter by using `kwargs.items()`. If you need syntax help with kwargs, check out [this article](https://hackernoon.com/python-functions-args-kwargs-5d2d00f09c74).\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# A:"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"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.7.6"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|