{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Day 1 In-Class Homework"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this homework, you're going to write code for a few problems.\n",
"\n",
"You will practice these programming concepts we've covered in class:\n",
"* Declaring and using variables.\n",
"* Using mathematical operators.\n",
"* Using string concatenation.\n",
"* Storing data in lists.\n",
"* Using built-in list functions (e.g., `max`, `min`, `sum`).\n",
"* Using loops to go through data inside lists.\n",
"\n",
"Work through as many of these as possible with the time you have, inside and outside class. \n",
"\n",
"Although not graded, the more practice you get - the more comfortable you'll be!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Problem 1: Can You Repeat Yourself Repeat Yourself?\n",
"\n",
"#### Skill you're practicing: Writing loops.\n",
"\n",
"- Create a string variable named `statement` and numerical variable named `num`\n",
"- Write a loop that prints the `statement` some `num` of times to the console\n",
"\n",
"#### Example:\n",
"```python\n",
"statement = \"Hello World\"\n",
"num = 3\n",
"\n",
"# Your loop here\n",
"```\n",
"\n",
"#### Example, expected output:\n",
"\n",
"```\n",
"Hello World\n",
"Hello World\n",
"Hello World\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# A:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Problem 2: I Got Chills, They're Multiplyin'\n",
"\n",
"#### Skill you're practicing: Using mathematical operators and writing loops.\n",
"\n",
"- Declare a number named `multiplier` and a list of numbers named `numbers`. \n",
"- Put two or more numbers in `numbers` (our example below has a list length of `4`). \n",
"- Go through each element in `numbers` and multiply the number in the list by `multipliers`, printing a new resulting new list.\n",
"\n",
"#### Example code:\n",
"```\n",
"num = 5\n",
"my_list = [1, 2, 3, 4]\n",
"\n",
"# Your solution here\n",
"```\n",
"\n",
"#### Expected output:\n",
"```\n",
"[5, 10, 15, 20]\n",
"```\n",
"\n",
"This should work, no matter the values of `num` and `my_list`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# A:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Problem 3: REVERSE! — !ESREVER\n",
"\n",
"#### Skill you're practicing: Using I/O, manipulating strings, and writing loops.\n",
"\n",
"- Prompt the user for an input string\n",
"- Using a looping iterator, reverse the string and print the result (as a string)\n",
"\n",
"#### Example:\n",
"```\n",
"# Enter a word or sentence, please.\n",
"# reverse me\n",
"\n",
"# Your expected program output:\n",
"em esrever\n",
"```\n",
"\n",
"> **Hint:** You can receive direct user input by using the `input` function. For example:\n",
"\n",
"```python\n",
"user_entry = input('Please enter your favorite number')\n",
"# user_entry now holds whatever the user typed in!\n",
"```\n",
"\n",
"> **Note:** While there is an awesome shortcut to reverse strings, `s[::-1]`, don't use it — practice writing out the code instead.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# A:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem 4: Calc U Later\n",
"\n",
"### Skill you're Practicing: Using I/O and control flow.\n",
"\n",
"Create a simple calculator that first asks the user what method they would like to use (`add` for addition, `sub` for subtraction, `mult` for multiplication, or `div` for division), then asks the user for two numbers. Your program will print the result of the method with the two numbers. Here is a sample prompt:\n",
"\n",
"```\n",
"# What calculation would you like to do? (add, sub, mult, div)\n",
"# add\n",
"# What is number 1?\n",
"# 3\n",
"# What is number 2?\n",
"# 6\n",
"\n",
"# Your expected program output:\n",
"Your result is 9. Calc U later!\n",
"```\n",
"\n",
"> **Hint:** By default, Python sets user input to a string. If the user types `2` into the prompt, the value your variable holds will be the string `\"2\"`! To avoid that, you can use `int()`, as shown in the second example below.\n",
"\n",
"```python\n",
"# Wrong for numbers — Python saves a STRING!\n",
"# This will work for the user entering `\"add\"`, `\"sub\"`, `\"mult\"`, or `\"div\"`, but not for numbers.\n",
"my_num = input(\"Please enter a number.\")\n",
"\n",
"# Correct for numbers — Python saves a NUMBER!\n",
"# This is how you should request the number from the user:\n",
"my_num = int(input(\"Please enter a number.\"))\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# A:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Problem 6: Loops and Froot Loops\n",
"\n",
"#### Skill you're practicing: Using lists, functions, and loops.\n",
"\n",
"Declare a list called `cereals` and fill it with at least three examples of breakfast cereals (e.g., `\"Wheaties\"`, `\"Froot Loops\"`, etc.). Then, write a loop that iterates through your `cereals` list. If the length of the name is _less than_ 10 characters (not counting spaces), print \n",
"\n",
">`cereal name` `is icky` and has `number of characters`\n",
"\n",
"If the length of the name is _greater than or equal to_ 10 characters, replace `is icky!` with `is delicious!`\n",
"\n",
"#### Example starter code:\n",
"```python\n",
"cereals = [\"Froot Loops\", \"Wheaties\", \"Cap'n Crunch\"]\n",
"```\n",
"\n",
"#### Example Output:\n",
"```\n",
"Froot Loops is delicious and has 10 characters\n",
"Wheaties is icky and has 8 characters\n",
"Cap'n Crunch is delicious and has 11 characters\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# A:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Problem 7: Pluralizer\n",
"\n",
"#### Skill you're practicing: String manipulation.\n",
"\n",
"If you take a look at the last problem's sample output, you'll notice that the first two sentences, `Froot Loops is delicious`, and `Wheaties is icky` doesn't quite make sense. What would make more sense is the phrase `Froot Loops are delicious` and `Wheaties are icky`.\n",
"\n",
"It would be better to look at whether or not the cereal name ends in an `s` and determine from there whether the rest of the sentence should be pluralized (\"are\") or singular (\"is\").\n",
"\n",
"Your task is to alter the answer to the previous problem such that, if the last letter of the cereal string is `s`, it prints `are icky/delicious`, and if it ends in any other character, it prints `is icky/delicious`.\n",
"\n",
"#### Altered example output:\n",
"\n",
"```\n",
"Froot Loops are delicious and has 10 characters\n",
"Wheaties are icky and has 8 characters\n",
"Cap'n Crunch is delicious and has 11 characters\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# A:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Problem 8: I Got Chills, They're Multiplyin' pt. 2\n",
"\n",
"Repeat problem 2 above, except instead of returning a _new_ list, _mutate the existing list in-place_ and return that list as a result.\n",
"\n",
"#### Example code:\n",
"```\n",
"num = 5\n",
"my_list = [1, 2, 3, 4]\n",
"\n",
"# Your solution here\n",
"```\n",
"\n",
"#### Expected output:\n",
"```\n",
"[5, 10, 15, 20]\n",
"```\n",
"This should work, no matter the values of `num` and `my_list`."
]
},
{
"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
}