Re: [Tutor] endless loop

2010-07-05 Thread Alan Gauld


"prasad rao"  wrote



def rr(z,m=1):
 q=lambda n:m%n==0
 s=lambda False : 0


This is always false???


 a=filter(s,map(q,range(1,z)))


So this is always empty?


 if not a:


So this is always true


 m+=1
 rr(z,m)


So you contuinuaslly call rr with the original z and an increasing m.
But nothing about m terminates the recursion, so it recurses 
forever - or until you hit the recursion limit.



 else:return m


This is never executed


This code is going into endless loop.


Yep, I'd say so.


Can some one show me  why it is going into Endless loop?


Because you wrote iit that way. This is one reason recursion is hard,
you must make 100% certain that there is a terminatin condition that 
will somehow get you out again...


HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] endless loop

2010-07-05 Thread Emile van Sebille

On 7/5/2010 8:31 AM prasad rao said...

hi
   I am trying problem 6 in projecteuler.org.
What is the smallest positive number that is evenly divisible by all
of the numbers from 1 to 20?


def rr(z,m=1):
   q=lambda n:m%n==0
   s=lambda False : 0
   a=filter(s,map(q,range(1,z)))
   if not a:
   m+=1
   rr(z,m)
   else:return m

This code is going into endless loop.



You don't show us how you're invoking this function, but it seems to me 
the result of passing a terminally false function result (s is always 
false) into filter will always result in [] (so a is always false) and 
will thereby cause the else clause never to be reached.


Emile


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor