From: Steven D'Aprano on
On Wed, 11 Aug 2010 13:14:35 -0700, Baba wrote:

> level: beginner
>
> exercise: given that packs of McNuggets can only be bought in 6, 9 or 20
> packs, write an exhaustive search to find the largest number of
> McNuggets that cannot be bought in exact quantity.

Is this a trick question?

I'd like to see somebody try to buy exactly 10**100**100 (1 googleplex)
McNuggets. And that's not even close to the largest number that you can't
buy.


Unhelpfully yours,


--
Steven
From: MRAB on
Steven D'Aprano wrote:
> On Wed, 11 Aug 2010 13:14:35 -0700, Baba wrote:
>
>> level: beginner
>>
>> exercise: given that packs of McNuggets can only be bought in 6, 9 or 20
>> packs, write an exhaustive search to find the largest number of
>> McNuggets that cannot be bought in exact quantity.
>
> Is this a trick question?
>
> I'd like to see somebody try to buy exactly 10**100**100 (1 googleplex)
> McNuggets. And that's not even close to the largest number that you can't
> buy.
>
If you'd looked at the link then you would've seen that it's
mathematically possible.

But then I expect you have a life! :-)
From: Baba on
Hi News123

Thank You for helping me out. Indeed i am not looking for the code but
rather for hints that direct my reasoning as well as hints as to how
to write basic programs like this.

You have broken down the approach into 2 parts. I have tried to solve
part 1 but i'm not quite there yet. Here's my code:

def can_buy(n_nuggets):
for a in range (1,n_nuggets):
for b in range (1,n_nuggets):
for c in range (1,n_nuggets):
if 6*a+9*b+20*c==n_nuggets:
#print a,b,c,'n_nuggets=',n_nuggets
return True
else:
return False


can_buy(55)

as you can see i am trying to loop through all combinations of values
bewtween 1 and n_nuggets and when the equation resolves it should
return True, else it should return False.

I was hoping that when i then call my function and ask it to test a
value nothing happens. What is wrong? My syntax? My semantic? Both?

tnx
Baba
From: Brian Victor on
Baba wrote:
> def can_buy(n_nuggets):
[snip]
> can_buy(55)
>
> as you can see i am trying to loop through all combinations of values
> bewtween 1 and n_nuggets and when the equation resolves it should
> return True, else it should return False.
>
> I was hoping that when i then call my function and ask it to test a
> value nothing happens. What is wrong? My syntax? My semantic? Both?

You're calling the function, but you're not doing anything with the
result. If you use "print can_buy(55)" you'll see the result on the
console. Presumably you'll actually want to use it in an if statement.

--
Brian

From: MRAB on
Baba wrote:
> Hi News123
>
> Thank You for helping me out. Indeed i am not looking for the code but
> rather for hints that direct my reasoning as well as hints as to how
> to write basic programs like this.
>
> You have broken down the approach into 2 parts. I have tried to solve
> part 1 but i'm not quite there yet. Here's my code:
>
> def can_buy(n_nuggets):
> for a in range (1,n_nuggets):
> for b in range (1,n_nuggets):
> for c in range (1,n_nuggets):
> if 6*a+9*b+20*c==n_nuggets:
> #print a,b,c,'n_nuggets=',n_nuggets
> return True
> else:
> return False
>
>
> can_buy(55)
>
> as you can see i am trying to loop through all combinations of values
> bewtween 1 and n_nuggets and when the equation resolves it should
> return True, else it should return False.
>
> I was hoping that when i then call my function and ask it to test a
> value nothing happens. What is wrong? My syntax? My semantic? Both?
>
Think about it this way.

How many packs of 20 would you need? You don't want too many, but too
few is OK.

Then, how many packs of 9 for the remaining nuggets? (Again, you don't
want too many.)

Then, how many packs of 6?

If all the nuggets are accounted for, good, otherwise reduce the number
of one of the packs and try again. Repeat as necessary.

A couple of 'for' loops will do it.