What is the "functional" way of doing this?

Ricardo Aráoz ricaraoz at gmail.com
Tue Jul 31 08:01:42 EDT 2007


Considering I am a beginner I did a little test. Funny results too. The
function I proposed (lists1.py) took 11.4529998302 seconds, while the
other one (lists2.py) took 16.1410000324 seconds, thats about 40% more.
They were run in IDLE from their own windows (F5).
Of course my little test may me wrong (just started with this language),
in which case I would appreciate any corrections, or comments.


------------------------------------------------
lists1.py :
def f(n):
    if n > 0:
        return ([n%26] + f(n/26))
    else:
        return []

import time

start = time.time()
for x in range(1,1000000):
    f(2100000000)
end = time.time()

print end - start
-----------------------------------------------
lists2.py :
def f(n):
   def mseq(n):
      while n > 0:
         n,a = divmod(n, 26)
         yield a
   return list(mseq(n))

import time

start = time.time()
for x in range(1,1000000):
    f(2100000000)
end = time.time()

print end - start
------------------------------------------------



More information about the Python-list mailing list