Hope I have got the problem right.
Here is a possible Python solution,
# amount : amount of pennies
# pigs : dict according to the structure of specs
# Example:
# pigs = {
# 'boar': { 'price': 10, 'qty': 1 },
# 'sow': { 'price': 5, 'qty': 1 },
# 'piglet': { 'price': 1, 'qty': 2}
# }
defbuyPigs(amount:int,pigs:dict)->dict:oneBoarPrice=pigs['boar']['price']/pigs['boar']['qty']oneSowPrice=pigs['sow']['price']/pigs['sow']['qty']onePigletPrice=pigs['piglet']['price']/pigs['piglet']['qty']# i : number of boar he can buy
# j : number of sow he can buy
# k : number of piglet he can buy
foriinrange(1,int(min(amount-1,amount/oneBoarPrice))):forjinrange(1,int(min(amount-1,amount/oneSowPrice))):forkinrange(1,int(min(amount-1,amount/onePigletPrice))):# Given that, the amount of pigs equals the amount of pennies
# so (i+j+k) == amount
# And, calculated total price == amount
ifi+j+k==amount==(oneBoarPrice*i+oneSowPrice*j+onePigletPrice*k):return{'boar':i,'sow':j,'piglet':k}# if no case found then return this
return{'error':'Purchase not possible'}
Hope I have got the problem right.
Here is a possible Python solution,
Output,
Is the output of
print(buyPigs(150, pigs))
right?Yes, my bad. I made a mistake when I submitted the challenge.
yes , even I got the same output for print(buyPigs(150, pigs)) .