in [Python]

Prev: most popular gui framework for python
Next: looping through possible combinations of McNuggets packs of 6,9and 20
From: News123 on 12 Aug 2010 16:35 One more small tip to verify whether your code is working: On 08/12/2010 10:28 PM, News123 wrote: > Hi Baba, Your code, but returning the result as suggested in my preious post: > 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): > print "trying for %2d: %2d %2d %2d" % (n,a,b,c) > if 6*a+9*b+20*c==n_nuggets: > return [a,b,c] > else: > return [] > # show the results for the first 50 numbers. # and verify whether it's true for n in xrange(50): result = can_buy(n) # get the result (see Brian's commen print("result of can_buy(%2d) is %s" % (n,result)) if result: a,b,c = result if a*6+b*9+c*12 != n: print "ERROR: can_buy() gives wrong result" Alternatively you can use a nice python feature called asserts assert statements can be used to vrify whether your code works as expected. Nothing will be printed if the assertion is correct otherwise your program will abort and print the assertion error: Example: for n in xrange(50): result = can_buy(n) # get the result (see Brian's commen print("result of can_buy(%2d) is %s" % (n,result)) if result: a,b,c = result assert a*6+b*9+c*12 == n > > > with the print statement in can_buy() you should immediately see some > issues. > as soon as can_buy seems to work, you could comment the print statement. > > > Additionally: > I would return [a,b,c] if you got a result > and [] if you got no result. > > So you don't only seem whether you can buy n nuggets, but you can also > see how (AND even verify the solution) > > > the print statement should show you what Peter said. > You just make one test and return immediately in the else statement > > Instead you should continue until you found a solution or until you > tried all possibilities. > > After having fixed the fact, that you don't try all options, you'll see > that your first success would be with 6+9+12 = 27 nuggets es you never > try with 0 boxes of a kind. (as Peter mentioned) > > Finally as Roald says. > > if you can reduce the upper limit of your search ranges. > if you have for example 13 nuggets you do not have to try 3 boxes of 6 > as this is already 18, so trying only 0,1 or two boxes is enough > > > > > > > > You will now also see, what > On 08/12/2010 09:42 PM, Roald de Vries wrote: >> >> On Aug 12, 2010, at 9:02 PM, Peter Otten wrote: >> >>> Baba wrote: >>> >>> >>>> 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? >>> >>> First, the function gives up too early; it should only return False >>> when all >>> combinations of a, b, c (technically: the product of the ranges) have >>> been >>> tried. >>> >>> Second, can_buy(0) should return True, but the solution 0*6 + 0*9 + >>> 0*20 is >>> never tried; fix your ranges accordingly. >> >> Moreover: a, b and c can range over n_nuggets/6, n_nuggets/9 and >> n_nuggets/20 respectively. This will work, but does too much work. >> >> Cheers, Roald >> >
From: News123 on 13 Aug 2010 17:09
Hi BAba, On 08/13/2010 09:25 PM, Ian Kelly wrote: > On Fri, Aug 13, 2010 at 12:25 PM, Baba <raoulbia (a)gmail.com> wrote:>> Hi News 123, >> >> Ok i'm getting closer. I am able to write code that will output values >> that can be bought in exact quantity (truelist) and values that cannot >> be bought in exact quantities. >> >> For a range up to 29 i get this: >> true [6, 9, 12, 15, 18, 20, 21, 24, 26, 27, 29] >> false [0, 1, 2, 3, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 22, 23, 25, >> 28] >> >> the sixth value that passes the test of having an exact solution is 20 >> so that would mean that the last number i got that cannot be bought in >> exact quantity is 19 >> >> that doesn't seem quite right, does it? As Thomas says: > > It's not. You're not just trying to find the sixth value that can be > bought in exact quantity, but a sequence of six values that can all be > bought in exact quantity. The integers [6, 9, 12, 15, 18, 20] are not > sequential. Six True values in a row without a False value n between tells you, that you can stop searching. So you that's what you have to write A piece of code, which fetches true fals values for 0 to e.g. 200 nuggets and stops if you had 6 True values in a row. Think how you do it manually: you can try this even without the can_buy function and plug in th can_buy() function only if you ahve your detection of 6 True values in a row working. test_sequence = "0010011011101110111101111111011111" # below I use the enumerate function. # rather useful for going through a list AND having a counter value. # when plugging in your function you can switch back to # for n_nuggets in xramge(200): # perhaps here some initialisation for your searching for i, a_char in enumerat(test_suequence): print "entry %2d (%s) is %s" % (i,a_char,result) result = a_char == '1' # result is now true for a '1' # and false for a '0' # here some code to determine the length of the sequence if sequence_length == 6: print "I found a sequence of 6 and can stop searching" print "my solution is " |