-2

I wrote a set of codes to calculate a Diophantine equation:

bestSoFar = 0     
packages = (6,9,20)   
numMc = 0
guess= 0
possibn = []
for n in xrange(1, 150):   
   for a in xrange(0, (n/ packages[0]) +1):
       for b in xrange(0,(n/ packages[1]) +1):
           c = (n - packages[0]* a - b * packages[1]) / packages[-1]
           numMc = packages[0] *a + packages[1] * b + packages[-1] * c
           if numMc == n and n not in possibn:
               possibn.append(n)
               print possibn
               if len(possibn) >6 and possibn [-1] - possibn[-6] == 5:
                  bestSoFar = n
                  break

The original problem set is designed by the MIT course. Basically it is to calculate the number of McNuggets that could be bought by arranging the ratio of packages-in-different-size ( McDonald does 6,9,20 McNuggets in a package). Say, 21 McNuggets could be bought by buying two 6-McNuggets and one 9-McNuggets. if the number of McNuggets are possible to be bought in exact quantity of packages, I store them into a list. It is found that if 6 consecutive numbers are also possible to be bought in exact quantity, the remained numbers could also be possible.

From my code the result of bestSoFar=149 while the expected answer is 40. The reason would be that it keeps looping until n reaches 149. I would like to stop right at 40 ( with the break statement). However, it fails and I am seeking the advices for you all. Also, if there is anyway to program the problem faster/easier, I am happy to know and learn it too.

Thank you so much. Casey

yukclam9
  • 336
  • 2
  • 12

3 Answers3

1

If you are not supposed to use a function, just assign a variable to cause breaks out of the other loops.

bestSoFar = 0     
packages = (6,9,20)   
numMc = 0
guess= 0
possibn = []
finished = False
for n in xrange(1, 150):   
   for a in xrange(0, (n/ packages[0]) +1):
       for b in xrange(0,(n/ packages[1]) +1):
           c = (n - packages[0]* a - b * packages[1]) / packages[-1]
           numMc = packages[0] *a + packages[1] * b + packages[-1] * c
           if numMc == n and n not in possibn:
               possibn.append(n)
#               print possibn
               if len(possibn) >6 and possibn [-1] - possibn[-6] == 5:
                  bestSoFar = n
                  finished = True
                  break
   if finished: break
print bestSoFar
Rob
  • 538
  • 2
  • 11
  • when I add a print bestSoFar after if finished, it keeps printing the list it returns the result as below: 0 0 0 0 0 [6] 0 0 0 [6,9] ..... up to [6,9,12...40] but I want the ans 40 only! Thanks! – yukclam9 May 27 '15 at 10:14
0

Turn it into a function and return:

from __future__ import print_function


def solve(*packages):
    bestSoFar = 0
    numMc = 0
    guess= 0
    possibn = []
    for n in xrange(1, 150):
       for a in xrange(0, (n / packages[0]) + 1):
           for b in xrange(0, (n / packages[1]) + 1):
               c = (n - packages[0] * a - b * packages[1]) / packages[-1]
               numMc = packages[0] * a + packages[1] * b + packages[-1] * c
               if numMc == n and n not in possibn:
                   possibn.append(n)
                   print possibn
                   if len(possibn) > 6 and possibn [-1] - possibn[-6] == 5:
                      return n
    return bestSoFar

x = solve(6, 9, 20)
print(x)
James Mills
  • 18,669
  • 3
  • 49
  • 62
  • 1
    yes a function would do however, the course does not introduce function when this problem set was introduced.. Thank you for your ans! – yukclam9 May 27 '15 at 09:56
-1

I am not actually clear on what you are expecting. But, what i see is you want to break out of everything. The break which you have given only exits inner loop. Put another break statement outside the inner loop and inside first loop.

Headrun
  • 129
  • 1
  • 11
  • he need to repeat the `if` statement if he want to do what you say so he will broke `DRY`, @Rob gave the same solution that you suggest but in clever way. – Chaker May 27 '15 at 10:07
  • Let me clarify: my expected ans is to return the number "40" as the numbers 35,36,37,38,39,40 ( 6 consecutive) would works. – yukclam9 May 27 '15 at 10:11