Python presentations
Chris Angelico
rosuav at gmail.com
Thu Sep 13 18:38:43 EDT 2012
On Fri, Sep 14, 2012 at 8:33 AM, Alexander Blinne <news at blinne.net> wrote:
> On 13.09.2012 21:01, 88888 Dihedral wrote:
>> def powerlist(x, n):
>> # n is a natural number
>> result=[]
>> y=1
>> for i in xrange(n):
>> result.append(y)
>> y*=x
>> return result # any object in the local function can be returned
>
> def powerlist(x, n):
> result=[1]
> for i in xrange(n-1):
> result.append(result[-1]*x)
> return result
>
> def powerlist(x,n):
> if n==1:
> return [1]
> p = powerlist(x,n-1)
> return p + [p[-1]*x]
Eh, much simpler.
def powerlist(x,n):
return [x*i for i in xrange(n-1)]
But you're responding to a bot there. Rather clever as bots go, though.
ChrisA
More information about the Python-list
mailing list