Problem:
Helen is a school teacher that has
m dollars. Helen wants to buy the largest possible number of notebooks. There are
n stores that sell bundles of notebooks. An array
bundleQuantities stores the number of notebooks sold in a bundle by each of the
n stores. An array
bundleCosts holds the
n costs of the corresponding bundles.
Info:
-
m is a positive integer
-
n >= 2
- The elements of
bundleQuantities and
bundleCosts are positive integers, they both have length
n and the indices coorespond: The i-th element of
bundleQuantities corresponds to the i-th element of
bundleCosts.
Goal:
Write a function of the form: mostNotebooks(m, bundleQuantities, bundleCosts) that returns the greatest amount of notebooks purchasable with m dollars.
Example:
mostNotebooks(50, [20, 19], [24, 19]) would return 40.
What I have tried:
I found the most possible bundles purchasable at each store for <=
m dollars.
I created ranges out of each of these
most-purchasable-at-each-store bounds.
I wanted to try to find every permutation of all of these ranges and compare the number of notebooks this corresponded to (only valid if they cost <=
m dollars). I couldn't get this to work though :(.
from itertools import product
def mostNotebooks(n, bundleQuantities, bundleCosts):
maxPurchasable = []
for i in range(len(bundleCosts)):
ithRange = []
for j in range((n//bundleCosts[i])+1):
ithRange.append(j)
maxPurchasable.append(ithRange)
currPermute = product(maxPurchasable[0], maxPurchasable[1])
permute = []
for c in currPermute:
permute.append(list(c))
print(permute)
mostBundles = 0
for p in permute:
currCost = 0
currBundles = 0
for i in range(len(bundleCosts)):
currCost += bundleCosts[i] * p[i]
currBundles += bundleQuantities[i] * p[i]
if currCost > n:
currBundles = 0
break
if currBundles > mostBundles:
mostBundles = currBundles
print(mostBundles)
return mostBundles