Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :(.

Python
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
Posted
Updated 9-Mar-19 13:08pm
v3
Comments
OriginalGriff 9-Mar-19 15:18pm    
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
Zast23 9-Mar-19 16:01pm    
Thanks, I'll post the code I've tried.
CHill60 9-Mar-19 17:18pm    
Would you also explain what the problem currently is
Zast23 9-Mar-19 18:47pm    
I can't get permutations to work properly with itertools.product (I tried itertools' permutation and combination too). But this is also grossly inefficient and I wanted to know if there is a better way to approach this conceptually?
Patrice T 9-Mar-19 18:00pm    
What is the problem ?

1 solution

Quote:
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 :(.

I think "find every permutation" is the wrong approach. Permutations are about the order of bundles.
You need to find the best combination of quantities of bundles. The position of a bundle does not really matters, just the quantity.
A smart permutation sort can only be some kind of pre-processing to speedup the search of best solution.
[UpDate]
Quote:
But this is also grossly inefficient

Advice: take a sheet of paper an solve the problem by hand, solve it again with a set of 5 diff bundles. See how you solved it and translate it to an algorithm and code it. See how efficient it is, thivk how you can improve it.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900