hw02

.py

School

University of Michigan *

*We aren’t endorsed by this school

Course

206

Subject

Computer Science

Date

Feb 20, 2024

Type

py

Pages

6

Uploaded by erzas088

# -*- coding: utf-8 -*- """hw02.ipynb Automatically generated by Colaboratory. Original file is located at https://colab.research.google.com/drive/1MolVV1OaiWD8Xb-gJA-i8LspM1aAODWC """ import numpy as np # run this first """# Homework 2, Stats 206/DataSci 101 ## Question 1 We will start this homework with some questions on collections in Python. ### Q1.a Recall that *sets* are collections where each value in the collection only appears once. Using the `set` function, turn both of these strings into sets. Then use set methods to find * How many letters are shared between the two names? * Which letters are in "George Washington" that are not in "Abraham Lincoln"? * How many letters are in neither president's name? """ gw = "George Washington".lower() al = "Abraham Lincoln".lower() az = "abcdefghijklmnopqrstuvwxyz" gw_set = set(gw) al_set = set(al) az_set = set(az) print(gw_set & al_set) print(gw_set - al_set) print(az_set - (gw_set & al_set)) """ ### Q1.b Use a `for` loop to find the unique values in the lunch orders below. Using Python, print out the number of unique items being ordered. (*Hint*: You can use `set()` to create an empty set.)""" lunch_orders = [{"pasta", "coffee"}, {"fries","sandwich", "cookie"}, {"pasta", "salad", "water"}, {"salad", "pasta", "coffee"},
{"sandwich", "water"}] u_set = set() for order in lunch_orders: for item in order: u_set.add(item) print(u_set) """### Q1.c Here is a menu at the resturant, with items and their cost: | Item | Price | Vegetarian | | --- | --- | --- | | salad | 8 | True | | soup | 5 | False | | pasta | 12 | True | | sandwich | 9 | False | | burger | 13 | False | | fries | 6 | True | | cake | 6 | True | | cookie| 3 | True | | water | 0 | True | | coffee| 2 | True | | soda | 3 | True | Create a dictionary called `menu` that has the item names as keys and a two-tuple with (price, vegetarian) as the value. Demonstrate by retrieving the price and vegetarian status of "burger". """ menu = {"salad" : (8, True), "soup" : (5, False), "pasta" : (12, True), "sandwich": (9, False), "burger": (13, False), "fries": (6, True), "cake": (6, True), "cookie": (3, True), "water": (0, True), "coffee": (2, True), "soda": (3, True) } print(menu["burger"]) """### Q1.d Write a function that takes a lunch order (a set of menu items) and returns the total price. Write a function that takes a lunch order a returns true if the entire order is vegetarian. Use list comprehensions to compute the price of each order and whether each order
is vegtarian. """ def total_price(lunch_order): price = 0 for item in lunch_order: price += menu[item][0] return price def veg_status(lunch_order): return all(menu[item][1] for item in order) # Hints: try running the following code # ("this", "is", "a tuple")[1] # [s.upper() for s in {"hello", "world"}] """### Q1.e When you have completed your menu in the part (c) you can remove the `#` charcters make this code run: """ menu_dict = { "name": list(menu.keys()), "price": [i[0] for i in menu.values()], "vegetarian": [i[1] for i in menu.values()] } """Use this table to compute the following: * What is the price of the most expensive item in the list? * What is the name of the most expensive item? (You can use the `np.argmax()` function to help you.) * How many vegetarian items are there? """ import numpy as np #price of most expensive highest = max(menu_dict["price"]) print(highest) #name of most expensive highest_index = np.argmax(menu_dict["price"]) item_name = menu_dict["name"][highest_index] print(item_name) #number of veg items num_veg_items = sum(menu_dict["vegetarian"]) print(num_veg_items) """### Question 2 """ # run this to do question 2 import pandas as pd
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help