{ "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 Practicum**\n", "## **Instructions**: Provide your name below and complete the following two questions. Name this file following the convention LASTNAME_FIRSTNAME_FP.ipynb and upload it to the appropriate dropbox." ], "metadata": { "id": "37TdF0oQ53gr" } }, { "cell_type": "markdown", "source": [ "Student Name (Last, First):" ], "metadata": { "id": "UUujLi0vxolH" } }, { "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 (25 points)\n", "In the following, ```menu``` contains information needed to \"make\" smoothies." ], "metadata": { "id": "2ZK3SWmO5ore" } }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "cbrSLsw43ssQ" }, "outputs": [], "source": [ "# Question 1 starter code\n", "\n", "menu = [ \\\n", " {\"name\":\"Banana Sunrise\", \"ingredients\":[\"bananas\", \"almond milk\", \"ice\"], \"blend_time\":60}, \\\n", " {\"name\":\"Berry Surprise\", \"ingredients\":[\"mixed berries\", \"bananas\", \"almond milk\", \"ice\"], \"blend_time\":90}, \\\n", " {\"name\":\"Cookie Monster\", \"ingredients\":[\"cookies\", \"ice cream\", \"milk\", \"ice\"], \"blend_time\":120}]" ] }, { "cell_type": "markdown", "source": [ "In the code block below:\n", "\n", "* Populate the 1-dimensional array called ```blend_times``` so that it contains each ```blend_time``` in the ```menu``` above. Populate this array using a **for loop**. This for loop needs to iterate through the ```menu``` above. When you are done, ensure that each ```blend_time``` is stored in the array called ```blend_times```.\n", "\n", "* **Sort** the blend times in the ```blend_times``` array from longest duration to shortest duration. It is your choice whether you do this sorting as part of the for loop or outside of the for loop.\n", "\n", "* Check your output using the print statements below.\n", "\n" ], "metadata": { "id": "BXOAI7tn6OgG" } }, { "cell_type": "code", "source": [ "# Initialize the blend times array\n", "blend_times = np.array([], dtype = np.float64)\n", "\n", "#####--- START STUDENT CODE ---#####\n", "\n", "\n", "#####--- END STUDENT CODE ---#####\n", "\n", "# Check your output:\n", "print(blend_times) # should print [120. 90. 60.]\n", "print(type(blend_times)) # should print \n", "print(blend_times.dtype) # should print float64\n" ], "metadata": { "id": "1LDQzKgE6OC6", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "8c50b2d9-97f5-45d5-c9f0-28e5fd953259" }, "execution_count": 3, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[120. 90. 60.]\n", "\n", "float64\n" ] } ] }, { "cell_type": "markdown", "source": [ "## Question 2 (25 points)\n", "Write the missing code to complete the ```__str__``` method in the ```Blender``` class below. When the main function is run, it will produce the following print statements:\n", "* If ```1``` is selected, the correct print statement is ```This drink is called the Banana Sunrise. It contains bananas, almond milk, and ice, and was blended for 60 seconds.```\n", "* If ```2``` is selected, the correct print statement is ```This drink is called the Berry Surprise. It contains mixed berries, bananas, almond milk, and ice, and was blended for 90 seconds.```\n", "* If ```3``` is selected, the correct print statement is ```This drink is called the Cookie Monster. It contains cookies, ice cream, milk, and ice, and was blended for 120 seconds.```\n" ], "metadata": { "id": "FLWIbzeL6fAi" } }, { "cell_type": "code", "source": [ "class Blender():\n", "\n", " def __init__(self, stuff, time, name):\n", " self.contents = stuff\n", " self.duration = time\n", " self.result = \"\"\n", " self.name = name\n", "\n", " def __str__(self):\n", " #####--- START STUDENT CODE ---#####\n", "\n", "\n", " #####--- END STUDENT CODE ---#####\n", "\n", "\n", " def blend(self):\n", " if self.contents != [] and self.duration > 0:\n", " self.result = self.contents_string_builder()\n", " else:\n", " self.result = \"ERROR: problem with the blending.\"\n", "\n", " def contents_string_builder(self):\n", " self.contents_string = \"\"\n", " for i in range(len(self.contents)-1):\n", " self.contents_string += self.contents[i] + \", \"\n", " self.contents_string += \"and \" + self.contents[len(self.contents)-1]\n", "\n", "def main():\n", "\n", " smoothie_idx = int(input(\"Select 1 for \" + menu[0].get(\"name\") + \". \\n\" +\\\n", " \"Select 2 for \" + menu[1].get(\"name\") + \". \\n\"+\\\n", " \"Select 3 for \"+ menu[2].get(\"name\")+ \". \\n\",\\\n", " ))-1\n", "\n", " smoothie = Blender(menu[smoothie_idx].get(\"ingredients\"), \\\n", " menu[smoothie_idx].get(\"blend_time\"), \\\n", " menu[smoothie_idx].get(\"name\"))\n", " smoothie.blend()\n", " print(smoothie)\n", "\n", "main()\n", "\n", "# Be sure to check your printed output with the sample output provided above.\n", "# For instance, selecting 3 should result in the following print statement:\n", "# This drink is called the Cookie Monster. It contains cookies, ice cream, milk, and ice, and was blended for 120 seconds." ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "lDEzCemh6oPN", "outputId": "718cd6aa-3750-41ed-da45-a0bad4927504" }, "execution_count": 4, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Select 1 for Banana Sunrise. \n", "Select 2 for Berry Surprise. \n", "Select 3 for Cookie Monster. \n", "3\n", "This drink is called the Cookie Monster. It contains cookies, ice cream, milk, and ice, and was blended for 120 seconds.\n" ] } ] } ] }