python segfault

Kiuhnm kiuhnm03.4t.yahoo.it
Wed Mar 28 09:59:31 EDT 2012


On 3/28/2012 8:16, Michael Poeltl wrote:
> yeah - of course 'while True' was the first, most obvious best way... ;-)
> but I was asked if there was a way without 'while True'
> and so I started the 'recursive function'
>
> and quick quick; RuntimeError-Exception ->  not thinking much ->  just adding
> two zeros to the default limit (quick and dirty) ->  segfault ==>  subject: python segfault ;-)

You give up too easily! Here's another way:

--->
def get_steps2(pos=0, steps=0, level = 100):
     if steps == 0:
         pos = random.randint(-1,1)
     if pos == 0:
         return steps
     steps += 2
     pos += random.randint(-1,1)

     if level == 0:
         return (pos, steps)
     res = get_steps2(pos,steps, level-1)
     if not isinstance(res, tuple):
         return res
     return get_steps2(res[0], res[1], level-1)

import random
for i in range(200):
     print ( get_steps2() )

print("done")
input("")
<---

Now the limit is 1267650600228229401496703205376. I hope that's enough.

Kiuhnm



More information about the Python-list mailing list