{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Using flag" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Build a loop which concatenates all the strings and displays the result. Then it tells us whether there was a name starting with letter M in the list without running the loop again and without using the in operator (you still may use it in the for loop)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "s = [\"John\", \"Jack\", \"Eleonora\", \"Mara\", \"Philip\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Dictionaries exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is the menu of a restaurant:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "Menu = {'meal_1':'Spaghetti', 'meal_2':'Fries', 'meal_3':'Hamburger', 'meal_4':'Lasagna'}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Print the second meal in the list." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add a new meal_5 with value \"Soup\"." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Replace the Hamburger with a Cheeseburger." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add to the dictionary an element with key Desserts and value a list of desserts: Pancakes, Ice-cream, Tiramisu" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a new empty dictionary. Then add, one after the other using assignment (in different statements), the first three meals' values as keys and the following three values as prices: 10, 5, 8." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Build function TotalCost which accepts as input a dictionary P containing names of dishes and prices and a dictionary Q containing names of dishes and quantities. The keys of dictionary Q should all be keys of dictionary P, obviously. The function calculates and returns the total cost of dishes.\n", "\n", "For example, TotalCost(prices,quantity) should return 23, calculated as 35+24+0*2.5" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "prices = {\n", " \"spaghetti\" : 4,\n", " \"lasagna\" : 5,\n", " \"fries\" : 3,\n", " \"hamburger\" : 2.5\n", " }\n", "quantity = {\n", " \"lasagna\" : 3,\n", " \"spaghetti\" : 2,\n", " \"hamburger\" : 0\n", " }\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Modify the previous function to check that the keys in Q are really contained in P and test it with the dictionaries here below." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "prices = {\n", " \"spaghetti\" : 4,\n", " \"lasagna\" : 5,\n", " \"fries\" : 3,\n", " \"hamburger\" : 2.5\n", " }\n", "quantity = {\n", " \"lasagna\" : 3,\n", " \"spaghetti\" : 2,\n", " \"hamburger\" : 0,\n", " \"ice cream\" : 4\n", " }\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Build function countOccurrence which takes as input a list L and returns a dictionary whose keys are the element of L and whose values are the number of occurrence of each element in L.\n", "\n", "For example, countOccurrence([\"Mara\",7,7,7,8,7,\"Mara\",\"John\"]) returns dictionary {8: 1, 'John': 1, 7: 4, 'Mara': 2}\n", "\n", "
Strategy: after having thought at the problem, if you can't come up with a working algorithm click below to reveal it. I said after having thought\n",
"