Question
USE PYTHON3; DO NOT IMPORT ANY PACKAGES Here's the code format in the text editor: def so_rich(self, money): TODO: Complete the docstring. #
USE PYTHON3; DO NOT IMPORT ANY PACKAGES
Here's the code format in the text editor:
def so_rich(self, money): """ TODO: Complete the docstring. """ # YOUR CODE GOES HERE #
# suppose you haven't seen any product yet # the only possible amount of money left is "money" # this is a set to record the possible money left left = set([money])
# get products lst = ...
for product in lst:
# a temporary set to save the updates of "left" # you don't want to modify the set you're iterating through tmp_left = set()
for m in left: # update tmp_left if type(product) != Limited_Product: new_left = m while ...: tmp_left.add(new_left) new_left = new_left - product.price else: # handle limited product ... ... while ... and ...: ... left = ...
return min(left)
def so_rich_recursive(self, money): """ TODO: Complete the docstring. """ # YOUR CODE GOES HERE #
# get products lst = ...
def helper(lst, money): # base case if ...: ...
cur_min = money product = lst[0] if type(product) != Limited_Product: tmp = money while ...: ... else: tmp = money ... while ... and ...: ... return ...
return helper(lst, money)
Here is the completed Premium User class:
class Premium_User(User): """ TODO: Complete the docstring. """
##### Part 4.2 ##### def __str__(self): """ TODO: Complete the docstring. """ return "premium user: {0} - {1}$".format(self.name, self.balance)
##### Part 5.4 ##### def buy_all(self, product_id): """ TODO: Complete the docstring. """ product = self.store.get_product(product_id) if isinstance(product, Limited_Product): for i in range(product.amount): self.add_cart(product_id) return self.checkout()
else: print("USER: not a limited product") return []
def undo_all(self): """ TODO: Complete the docstring. """ while True: last_purchase = self.purchase_history.peek() if self.purchase_history.is_empty(): break elif isinstance(last_purchase.product, Limited_Product): break self.undo_purchase()
Thank you for your work! Let me know ASAP if you need anything else!
The store managers decided to test whether their inventory could support customers to buy as many products as possible. Their idea is to find a sequence of purchase that costs the customers the most of their money, given an arbitrary amount of available money. If the user cannot spend all (or the most) of their money after purchasing every possible item they could buy, the warehouse is in lack of the cheap products. Complete the following functions defined in the store class given the skeleton in the starter code. O so_rich (self, money) Suppose the customer is a premium user. Given the amount of money (non-negative integer) to spend, what is the least amount of money left after a series of purchases? Examples: Il Il Il = = = = = = 11 >>>store.view_products) ====Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started