Answered step by step
Verified Expert Solution
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 requiredfruits strings a dictionary prices strings for key, positive integers for value and a totalamount integer It prints all the ways to buy some of each required fruit so that the total price equals totalamount. You must include at least one of every fruit in requiredfruit and cannot include any other fruits that are not in requiredfruit.
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 buyrequiredfruits, prices, totalamount:
Print ways to buy some of each fruit so that the sum of prices is amount.
prices oranges: 'apples': 'bananas': 'kiwis':
buyapples 'oranges', 'bananas' prices,
apples orange banana
buyapples 'oranges', 'bananas' prices,
apples orange bananas
apples oranges banana
buyapples 'kiwis' prices,
apples kiwis
apples kiwis
apples kiwi
def addfruits amount, cart:
if fruits and amount :
printcart
elif fruits and amount :
fruit fruits
price
for k in :
add
addrequiredfruits, totalamount,
def displayfruit count:
Display a count of a fruit in square brackets.
displayapples
apples
displayapples
apple
assert count and fruits
if count :
fruit fruit: # get rid of the plural s
return strcount fruit
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