{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Before you turn in this homework, make sure everything runs as expected. First, **restart the kernel** (in the menubar, select Kernel$\\rightarrow$Restart) and then **run all cells** (in the menubar, select Cell$\\rightarrow$Run All).\n", "\n", "Make sure you execute every single code cell, in order, filling with your solutions in any place that says `YOUR CODE HERE`, and always DELETE the line that says:\n", "\n", "```python\n", "raise NotImplementedError()\n", "```\n", "The purpose of this line is to tell you if you forgot to answer a question (it will throw an error if the line is there)\n", "\n", "**IMPORTANT**: DO NOT DELETE ANY CELL.\n", "\n", "Fill your name below:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "NAME = \"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Accessing values of a list and slicing strings. \n", "\n", "Given the list named `pizzas`:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "pizzas = ['cheese', 'pepperoni', 'margherita', 'hawaiian', 'italian sausage', 'green peppers and onions']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1)\n", "\n", "Grab the first flavor, assign the value to a variable named `first_flavor`, and print it." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cheese\n" ] } ], "source": [ "### BEGIN SOLUTION\n", "first_flavor = pizzas[0]\n", "print(first_flavor)\n", "### END SOLUTION" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "#BEGIN HIDDEN TESTS\n", "from nose.tools import assert_equal\n", "\n", "assert_equal(first_flavor, 'cheese')\n", "#END HIDDEN TESTS" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2)\n", "\n", "Grab the last flavor, assign the value to a variable named `last_flavor`, and print it." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "green peppers and onions\n" ] } ], "source": [ "### BEGIN SOLUTION\n", "last_flavor = pizzas[-1]\n", "print(last_flavor)\n", "### END SOLUTION" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "#BEGIN HIDDEN TESTS\n", "assert_equal(last_flavor, 'green peppers and onions')\n", "#END HIDDEN TESTS" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3)\n", "\n", "Access the element of the pizza list that corresponds to 'pepperoni', replace it for 'mushroom', and print the pizzas list." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['cheese', 'mushroom', 'margherita', 'hawaiian', 'italian sausage', 'green peppers and onions']\n" ] } ], "source": [ "### BEGIN SOLUTION\n", "pizzas[1] = 'mushroom'\n", "print(pizzas)\n", "### END SOLUTION" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "#BEGIN HIDDEN TESTS\n", "assert_equal(pizzas, ['cheese', 'mushroom', 'margherita', 'hawaiian', 'italian sausage', 'green peppers and onions'])\n", "assert_equal(pizzas[1], 'mushroom')\n", "#END HIDDEN TESTS" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Remember!!\n", "\n", "Before you turn this problem in, make sure everything runs as expected. First, restart the kernel (in the menubar, select Kernel→→Restart) and then run all cells (in the menubar, select Cell→→Run All).\n", "\n", "Make sure you filled in any place that says YOUR CODE HERE or \"YOUR ANSWER HERE\", as well as your name at the beggining of the notebook." ] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 2 }