I have written an iterative function to solve a very simplified version of the knapsack problem and it is given below.
Iterative Code
def so_rich(self, money):
""" Find the minimum amount left from the given 'money' after buying s series of products """
# 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 = list(self.warehouse.inventory.values())
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()
# update tmp_left
for m in left:
if type(product) != Limited_Product:
new_left = m
while new_left >= product.price:
new_left = new_left - product.price
tmp_left.add(new_left)
else:
# handle limited product
new_left = m
product_count = product.amount
while new_left >= product.price and product_count > 0:
new_left = new_left - product.price
tmp_left.add(new_left)
product_count -= 1
left.update(tmp_left)
return min(left)
And Now I need to write the same function in the recursive format as well and I don't have much of a clue how to do that. I have written the following code but it does not give the correct answer for me. Can anyone help me correct the code?
Recursive Code
def so_rich_recursive(self, money):
""" recursively find the minimum amount left from the given 'money' after buying s series of products """
# YOUR CODE GOES HERE #
# get products
lst = list(self.warehouse.inventory.values())
def helper(lst, money):
# base case
if not lst:
return money
cur_min = money
product = lst[0]
print(product)
print(cur_min)
if type(product) != Limited_Product:
tmp = money
while tmp >= product.price:
print(product.name, tmp)
tmp = tmp - product.price
else:
tmp = money
product_count = product.amount
while tmp >= product.price and product_count > 0:
print(product.name, tmp)
tmp = tmp - product.price
product_count -= 1
cur_min = tmp
lst.pop(0)
return helper(lst, min(money, cur_min))
return helper(lst, money)
One way to think about it is to start with the idea you will use recursion to replace your for product in lst:
loop. To do that can use pop()
to take the next product off the list and then pass the remaining list to the next call of the function. An empty product_list
is the trigger that ends the recursion. Note: since you need the accumulated value of left
for each product you would forward it as a parameter as well. Initially, left should be None
which can be accomplished by using its default.
Note: I made a standalone function for simplicity, but it can also be implemented as a class method.
Example:
def so_rich_recursive(product_list, money, left=None):
if not product_list:
return min(left)
product = product_list.pop()
if not left:
left = set([money])
tmp_left = set()
# update tmp_left
for m in left:
if type(product) != Limited_Product:
new_left = m
while new_left >= product.price:
new_left = new_left - product.price
tmp_left.add(new_left)
else:
# handle limited product
new_left = m
product_count = product.amount
while new_left >= product.price and product_count > 0:
new_left = new_left - product.price
tmp_left.add(new_left)
product_count -= 1
left.update(tmp_left)
return so_rich_recursive(product_list, money, left)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.