What is the "functional" way of doing this?

Arnau Sanchez arnau at ehas.org
Sun Aug 5 15:02:50 CEST 2007

> On Jul 30, 5:48 pm, beginner <zyzhu2... at gmail.com> wrote:
>> def f(n):
>>      l=[]
>>      while n>0:
>>          l.append(n%26)
>>          n /=26
>>     return l
>> I am wondering what is the 'functional' way to do the same.

  > I see. It is interesting (and not surprisingly) that recursion or
> yield are required. Thanks for everyone's help.

Why not use both recursion and generators?

def f2(n):
     if n > 0:
         n2, x = divmod(n, 26)
         yield x
         for next in f2(n2):
             yield next

Could this be considered functional?

Anyway, I wonder if there is a more elegant way to do the "yield x... yield 
next" part. Any ideas?


More information about the Python-list mailing list