{
 "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": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello world\n",
      "Hello world\n",
      "Hello world\n"
     ]
    }
   ],
   "source": [
    "statement = \"Hello world\"\n",
    "num = 3\n",
    "\n",
    "for i in range(3):\n",
    "    print(statement)"
   ]
  },
  {
   "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": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5, 10, 15, 20]\n"
     ]
    }
   ],
   "source": [
    "multiplier = 5\n",
    "numbers = [1, 2, 3, 4]\n",
    "result = list()\n",
    "\n",
    "for number in numbers:\n",
    "    result.append(number * multiplier)\n",
    "\n",
    "print(result)"
   ]
  },
  {
   "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",
    "# <Input Prompt:> Enter a word or sentence, please.\n",
    "# <User Input:> 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": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter a word of sentence please. reverse me\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'em esrever'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = input(\"Enter a word of sentence please. \")\n",
    "s_list = list(s)\n",
    "s_list.reverse()\n",
    "result = str()\n",
    "for letter in s_list:\n",
    "    result += letter\n",
    "result"
   ]
  },
  {
   "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",
    "# <Input Prompt:> What calculation would you like to do? (add, sub, mult, div)\n",
    "# <User Input:> add\n",
    "# <Input Prompt:> What is number 1?\n",
    "# <User Input:> 3\n",
    "# <Input Prompt:> What is number 2?\n",
    "# <User Input:> 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": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "What calculation would you like to do? (add, sub, mult, div)add\n",
      "What is number 1? 3\n",
      "What is number 2? 6\n",
      "Your result is 9.0. Calc u later!\n"
     ]
    }
   ],
   "source": [
    "op = input(\"What calculation would you like to do? (add, sub, mult, div)\")\n",
    "n1 = float(input(\"What is number 1? \"))\n",
    "n2 = float(input(\"What is number 2? \"))\n",
    "result = int()\n",
    "if op == 'add':\n",
    "    result = n1 + n2\n",
    "elif op == 'sub':\n",
    "    result = n1 - n2\n",
    "elif op == 'mult':\n",
    "    result = n1 * n2\n",
    "elif op == 'div':\n",
    "    result = int(n1 / n2)\n",
    "print(f\"Your result is {result}. Calc u later!\")"
   ]
  },
  {
   "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": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "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"
     ]
    }
   ],
   "source": [
    "cereals = [\"Froot Loops\", \"Wheaties\", \"Cap'n Crunch\"]\n",
    "name_lengths = []\n",
    "\n",
    "for cereal in cereals:\n",
    "    name_lengths.append(len(cereal.replace(' ','')))\n",
    "    \n",
    "for cereal, name_length in zip(cereals, name_lengths):\n",
    "    if name_length < 10:\n",
    "        print(f'{cereal} is icky and has {name_length} characters')\n",
    "    else:\n",
    "        print(f'{cereal} is delicious and has {name_length} characters')"
   ]
  },
  {
   "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": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "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"
     ]
    }
   ],
   "source": [
    "cereals = [\"Froot Loops\", \"Wheaties\", \"Cap'n Crunch\"]\n",
    "name_lengths = []\n",
    "is_are = str()\n",
    "\n",
    "for cereal in cereals:\n",
    "    name_lengths.append(len(cereal.replace(' ','')))\n",
    "    \n",
    "for cereal, name_length in zip(cereals, name_lengths):\n",
    "    if cereal[-1] == 's':\n",
    "        is_are = 'are'\n",
    "    else:\n",
    "        is_are = 'is'\n",
    "    if name_length < 10:\n",
    "        print(f'{cereal} {is_are} icky and has {name_length} characters')\n",
    "    else:\n",
    "        print(f'{cereal} {is_are} delicious and has {name_length} characters')"
   ]
  },
  {
   "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": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5, 10, 15, 20]\n"
     ]
    }
   ],
   "source": [
    "multiplier = 5\n",
    "numbers = [1, 2, 3, 4]\n",
    "\n",
    "for i, number in enumerate(numbers):\n",
    "    numbers[i] = number * multiplier\n",
    "\n",
    "print(numbers)"
   ]
  }
 ],
 "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.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
