Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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) ==== Product - Price A - 20$ B - 7$ (special) C - 1$ (2 left) === = II I! = = = >>>store.so_rich(45) 1 # You have 45 dollars to spend. The optimal solution is buying six B items and two C items. This will leave you with 45 - 7* 6-1* 2 = 1 dollars left. The least amount of money left is 1 since there is no way to spend all of the money. (although C's price is 1$, you have purchased all of them already!) >>> store.so_rich(61) 0 # You have 61 dollars to spend. You can spend all of them by buying two A and three B (20 * 2 + 7 * 3 = 61). So the least amount of money left is 0. so_rich_recursive (self, money) You must use recursion to implement the same function. You might find the recursion version of the function simpler

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle PL/SQL Programming Database Management Systems

Authors: Steven Feuerstein

1st Edition

978-1565921429

More Books

Students also viewed these Databases questions