Untitled
unknown
plain_text
5 months ago
5.5 kB
3
Indexable
# ECOR 1041 Lab 5 __author__ = " Jainil Patel" __student_number__ = "101345467" #====================================================== # Exercise 1 def tip(cost: float, satisfaction: int ) -> float : """ Calulates the amount of tip based on the cost of the meal and the level satisfaction the diner has recieved. Parameters: cost (float): The cost of the meal. satisfaction_level (int): The satisfaction rating. (1 = Totally satisfied, 2 = Somewhat satisfied, 3 = Dissatisfied) Returns: float: The calculated tip amount. Examples: >>> tip(50, 1) 10.0 >>> tip(80, 2) 12.0 >>> tip(100, 3) 5.0 """ if satisfaction == 1 : return cost * 0.20 elif satisfaction == 2 : return cost * 0.15 elif satisfaction == 3 : return cost * 0.05 #====================================================== # Exercise 2 def coupon(grocery_cost: float ) -> float: """ Calculates the coupon value based on the grocery cost. Parameters: grocery_cost (float): The total cost of groceries. Returns: float: The calculated coupon amount, based on a percentage of the grocery cost. Examples: >>> coupon(50) 4.0 >>> coupon(200) 24.0 >>> coupon(9) 0 """ if 10 <= grocery_cost <= 60 : return grocery_cost * 0.08 elif 60 < grocery_cost <= 150 : return grocery_cost * 0.10 elif 150 < grocery_cost <= 210 : return grocery_cost * 0.12 elif grocery_cost > 210 : return grocery_cost * 0.14 else: return 0 #====================================================== # Exercise 3 #Step 1 Truth Chart: # true --> below 40 = False # true --> between 40 and 60 = True # true --> above 60 = True # false --> below 40 = False # false --> between 40 and 60 = True # false --> above 60 = False #Step 2: def bakers_party(is_weekend: bool, num_of_pastries: int) -> bool: """ Determine if the bakers' party is successful based on the day and number of pastries. Parameters: is_weekend (bool): True if it is the weekend, False if it is a weekday. num_pastries (int): The number of pastries at the party. Returns: bool: True if the party is successful, False otherwise. Examples: >>> bakers_party(True, 30) False >>> bakers_party(False, 50) True >>> bakers_party(False, 70) False """ if is_weekend : return num_of_pastries >= 40 else: return 40 <= num_of_pastries <= 60 #====================================================== # Exercise 4 def squirrel_play( is_summer: bool, temperature: int) -> bool: """ Determines if squirrels are playing based on the temperature and season. Parameters: is_summer (bool): True if it is summer, False if it is not summer. temperature (int): The current temperature in degrees Fahrenheit. Returns: bool: True if the squirrels are playing, False otherwise. Examples: >>> squirrel_play(True, 95) True >>> squirrel_play(False, 95) False >>> squirrel_play(False, 85) True >>> squirrel_play(True, 101) False >>> squirrel_play(False, 59) False """ if is_summer: return 60 <= temperature <= 100 else: return 60 <= temperature <= 90 #====================================================== # Exercise 5 def great_42(a: int, b: int) -> bool: """ Determine if either integer is 42, or if their sum or the absolute value of their difference is 42. Parameters: a (int): The first integer. b (int): The second integer. Returns: bool: True if either a or b is 42, or if their sum or absolute difference is 42. Otherwise, returns False. Examples: >>> great_42(42, 10) True >>> great_42(17, 90) False >>> great_42(5, 0) False >>> great_42(10, 18) False >>> great_42(40, 3) False """ return a == 42 or b == 42 or ( a + b == 42) or (abs(a-b) == 42) #====================================================== # Exercise 6 def multiply_uniques(a: int, b: int, c: int) -> int: """ Calculate the product of unique values among a, b, and c. If two values are the same, only one is used in the product. If all three values are the same, the function returns 1. Parameters: a (int): First integer value b (int): Second integer value c (int): Third integer value Returns: int: The product of unique values, or 1 if all values are the same Examples: >>> multiply_uniques(3, 2, 3) 2 >>> multiply_uniques(3, 3, 3) 1 >>> multiply_uniques(1, 2, 3) 6 >>> multiply_uniques(2, 2, 4) 4 >>> multiply_uniques(5, 5, 5) 1 """ if a == b == c: return 1 elif a == b: return c elif b == c: return a elif a == c: return b else: return a * b * c
Editor is loading...
Leave a Comment