Lonnie Princehouse wrote:
Here's my take on the thing. It only prints one term, though.
http://www.magicpeacefarm.com/lonnie/code/morris.py.html
(a bit too long to post)
excerpt :
def morris(seed, n):
...
if n == 1:
return seed
else:
return
What's wrong with the following ?
def morris(seed,n) :
...
for k in xrange(n-1) :
seed=length_encode(seed)
return seed
Nothing's wrong with it.
I happen to think the recursive version is more elegant, but that's
just me ;-)
--
Em Seg, 2006-04-10 às 10:05 -0700, Lonnie Princehouse escreveu:
I happen to think the recursive version is more elegant, but that's
just me ;-)
It may be elegant, but it's not efficient when you talk about Python.
Method calls are expensive:
$ python2.4 -mtimeit 'pass'
1000 loops, best of
In general, you're right - if speed is a concern, loops should be used
instead of recursion in Python when possible.
In this case, the recursive overhead is insignificant compared to the
expense of iterating over the sequence at every iteration. By the time
there are 70 stack frames of recursion
John Salerno wrote:
Ben Cartwright wrote:
Definitely go for (1). The Morris sequence is a great candidate to
implement as a generator. As a generator, it will be more flexible and
efficient than (2).
Actually I was just thinking about this and it seems like, at least for
my purpose
Michael Spencer wrote:
itertools.groupby makes this very straightforward:
I was considering this function, but then it seemed like it was only
used for determing consecutive numbers like 1, 2, 3 -- not consecutive
equivalent numbers like 1, 1, 1. But is that not right?
--
John Salerno wrote:
Michael Spencer wrote:
itertools.groupby makes this very straightforward:
I was considering this function, but then it seemed like it was only
used for determing consecutive numbers like 1, 2, 3 -- not consecutive
equivalent numbers like 1, 1, 1. But is that not right?
John Salerno wrote:
Michael Spencer wrote:
itertools.groupby makes this very straightforward:
I was considering this function, but then it seemed like it was only
used for determing consecutive numbers like 1, 2, 3 -- not consecutive
equivalent numbers like 1, 1, 1. But is that not
Gerard Flanagan wrote:
John Salerno wrote:
Michael Spencer wrote:
itertools.groupby makes this very straightforward:
I was considering this function, but then it seemed like it was only
used for determing consecutive numbers like 1, 2, 3 -- not consecutive
equivalent numbers like 1, 1, 1.
Here's my take on the thing. It only prints one term, though.
http://www.magicpeacefarm.com/lonnie/code/morris.py.html
(a bit too long to post)
--
http://mail.python.org/mailman/listinfo/python-list
just couldn't help taking the bait...
def morris(seed) :
m = morris('3447221')
m.next()
'1324172211'
m.next()
'1113121411172221'
m.next()
'31131112111431173211'
assert isinstance(seed,basestring) and seed.isdigit(),bad seed
def
Lonnie Princehouse wrote:
Here's my take on the thing. It only prints one term, though.
http://www.magicpeacefarm.com/lonnie/code/morris.py.html
(a bit too long to post)
yikes, scary! :)
there was always the hint that using itertools might be helpful, as you
guys are doing, but
Ok, I wrote this all by myself, which explains why it doesn't work. It
is meant to take a number and generate the next number that follows
according to the Morris sequence. It works for a single number, but what
I'd like it to do is either:
1. repeat indefinitely and have the number of times
John Salerno wrote:
1. repeat indefinitely and have the number of times controlled elsewhere
in the program (e.g., use the morris() generator in a for loop and use
that context to tell it when to stop)
2. just make it a function that takes a second argument, that being the
number of
John Salerno wrote:
2. just make it a function that takes a second argument, that being the
number of times you want it to repeat itself and create numbers in the
sequence
Here's what I've come up with so far. Probably not the most elegant
solution because of the nested function, but it
The generator in your original post /does/ rebind seed, but only on the
last iteration of the loop. You'll need to wrap that loop in another
loop if you want the generator to yield more than once.
As for communicating with a generator --- e.g. telling it to stop ---
this might be done by passing
John Salerno wrote:
It
is meant to take a number and generate the next number that follows
according to the Morris sequence. It works for a single number, but what
I'd like it to do is either:
1. repeat indefinitely and have the number of times controlled elsewhere
in the program (e.g., use
Ben Cartwright wrote:
Definitely go for (1). The Morris sequence is a great candidate to
implement as a generator. As a generator, it will be more flexible and
efficient than (2).
Actually I was just thinking about this and it seems like, at least for
my purpose (to simply return a list of
John Salerno wrote:
Actually I was just thinking about this and it seems like, at least for
my purpose (to simply return a list of numbers), I don't need a
generator.
Yes, if it's just a list of numbers you need, a generator is more
flexibility than you need. A generator would only come in
19 matches
Mail list logo