 A+
Category：Languages
The input is an integer that specifies the amount to be ordered. There are predefined package sizes that have to be used to create that order.
e.g.
Packs 3 for $5 5 for $9 9 for $16
for an input order 13 the output should be:
2x5 + 1x3
So far I've the following approach
remaining_order = 13 package_numbers = [9,5,3] required_packages = [] while remaining_order > 0: found = False for pack_num in package_numbers: if pack_num <= remaining_order: required_packages.append(pack_num) remaining_order = pack_num found = True break if not found: break
But this will lead to the wrong result
1x9 + 1x3 remaining: 1
You can use itertools.product
:
import itertools remaining_order = 13 package_numbers = [9,5,3] required_packages = [] a=min([x for i in range(1,remaining_order+1//min(package_numbers)) for x in itertools.product(package_numbers,repeat=i)],key=lambda x: abs(sum(x)remaining_order)) remaining_order=sum(a) print(a) print(remaining_order)
Output:
(5, 5, 3) 0
This simply does the below steps:

Get value closest to
13
, in the list with all the product values. 
Then simply make it modify the number of
remaining_order
.