What is the "functional" way of doing this?

attn.steven.kuo at gmail.com attn.steven.kuo at gmail.com
Mon Jul 30 19:33:54 EDT 2007


On Jul 30, 3:48 pm, beginner <zyzhu2... at gmail.com> wrote:
> Hi,
>
> If I have a number n and want to generate a list based on like the
> following:
>
> 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.
>


Recursion is common in functional programming:

def f(n, l=None):
    if l == None:
        l = []
    if n > 0:
        return f(n/26, l + [n%26])
    else:
        return l

print f(1000)

--
Hope this helps,
Steven






More information about the Python-list mailing list