[Tutor] How to implement function like this?

Kent Johnson kent37 at tds.net
Wed Oct 24 04:14:30 CEST 2007


Ricardo Aráoz wrote:
> Yinghe Chen wrote:
>> Hello gurus,
>>
>> I have a question, a function like below, it is implemented by me, :)
>>
>> def funcA(tarray):
>>        a = [2,3,4]
>>         if len(tarray) >=3:
>>              return a[0],a[1], a[2]
>>         elif len(tarray) == 2:
>>             return a[0],a[1], funcB(1)[0]
>>         elif len(tarray) == 1:
>>            return a[0], funcB(2)[0], funcB(2)[1]
>>         else:
>>             return funcB(3)[0], funcB(3)[1], funcB(3)[2]

> def funcA(tArray) :
>     a = [2,3,4]
>     L = min(len(tArray), 3)
>     return tuple(i for n, i in enumerate(a) if n < L)+tuple(funcB(len(a)-L))

tuple(i for n, i in enumerate(a) if n < L) can be written
tuple(a[:L])

funcB(len(a)-L) does not do what the OP requested. For example if len(a) 
is 10, L will be 3 and you will call funcB(7).

Kent


More information about the Tutor mailing list