What is the "functional" way of doing this?
James Stroud
jstroud at mbi.ucla.edu
Mon Jul 30 21:11:47 EDT 2007
beginner 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.
>
> Thanks,
> beginner
>
Does it get any more functional than lambda?
py> f = lambda n, r=None: f(n/26, (r if r else [])) + [n%26] if n/26
else [n%26]
py> f(300000)
[17, 1, 20, 12]
py> f(30000)
[1, 18, 9, 22]
py> f(3000)
[4, 11, 10]
py> f(1000)
[1, 12, 12]
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095
http://www.jamesstroud.com/
More information about the Python-list
mailing list