Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

implement buy, which takes a list of required _ fruits ( strings ) , a dictionary prices ( strings for key, positive integers for value

implement buy, which takes a list of required_fruits (strings), a dictionary prices (strings for key, positive integers for value), and a total_amount (integer). It prints all the ways to buy some of each required fruit so that the total price equals total_amount. You must include at least one of every fruit in required_fruit and cannot include any other fruits that are not in required_fruit.
The display function will be helpful. You can call display on a fruit and its count to create a string containing both.
What does fruits and amount represent? How are they used in the recursive?
def buy(required_fruits, prices, total_amount):
"""Print ways to buy some of each fruit so that the sum of prices is amount.
>>> prices ={'oranges': 4, 'apples': 3, 'bananas': 2, 'kiwis': 9}
>>> buy(['apples', 'oranges', 'bananas'], prices, 12)
[2 apples][1 orange][1 banana]
>>> buy(['apples', 'oranges', 'bananas'], prices, 16)
[2 apples][1 orange][3 bananas]
[2 apples][2 oranges][1 banana]
>>> buy(['apples', 'kiwis'], prices, 36)
[3 apples][3 kiwis]
[6 apples][2 kiwis]
[9 apples][1 kiwi]
"""
def add(fruits, amount, cart):
if fruits ==[] and amount ==0:
print(cart)
elif fruits and amount >0:
fruit = fruits[0]
price =____
for k in ____:
add(____,____,____)
add(required_fruits, total_amount, '')
def display(fruit, count):
"""Display a count of a fruit in square brackets.
>>> display('apples',3)
'[3 apples]'
>>> display('apples',1)
'[1 apple]'
"""
assert count >=1 and fruit[-1]=='s'
if count ==1:
fruit = fruit[:-1] # get rid of the plural s
return '['+ str(count)+''+ fruit +']'

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions

Question

Why could the Robert Bosch approach make sense to the company?

Answered: 1 week ago