Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to implement function like this?

Reply
Thread Tools

How to implement function like this?

 
 
Yinghe Chen
Guest
Posts: n/a
 
      10-23-2007
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]


The return of funcA is always 3 values, but depending on the length of
tarray, I need to return different values accordingly. if tarray lenght is
2, I need to get another one value from funcB, if tarray length is 0, I need
to get all three values from funcB.


Is there a brief way to achieve it?

Thanks,


 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      10-23-2007
On Tue, 23 Oct 2007 16:28:37 +0800, 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]
>
>
> The return of funcA is always 3 values, but depending on the length of
> tarray, I need to return different values accordingly. if tarray lenght is
> 2, I need to get another one value from funcB, if tarray length is 0, I need
> to get all three values from funcB.


Untested:

def func_a(t_array):
result = [2, 3, 4]
t_array_length = len(t_array)
remaining_length = len(result) - t_array_length
if t_array_length < len(result):
result = (result[:t_array_length]
+ func_b(remaining_length)[:remaining_length])
return tuple(result)

Ciao,
Marc 'BlackJack' Rintsch
 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      10-23-2007
"Yinghe Chen" <(E-Mail Removed)> writes:
> 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]


untested:

from itertools import chain, islice
def funcA(tarray):
xB = max(3 - len(tarray), 0)
return chain(a, islice(funcB(xB), xB))
 
Reply With Quote
 
Loic Mahe
Guest
Posts: n/a
 
      10-23-2007
even shorter:

def funcA(tarray):
s = min(len(tarray), 3)
return [2, 3, 4][0:s] + [e for e in funcB(3-s)[0:3-s]]
 
Reply With Quote
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      10-23-2007
On Tue, 23 Oct 2007 11:48:08 +0200, Loic Mahe wrote:

> even shorter:
>
> def funcA(tarray):
> s = min(len(tarray), 3)
> return [2, 3, 4][0:s] + [e for e in funcB(3-s)[0:3-s]]


Why the list comprehension!?

Ciao,
Marc 'Blackjack' Rintsch
 
Reply With Quote
 
Loic Mahe
Guest
Posts: n/a
 
      10-23-2007
Marc 'BlackJack' Rintsch a écrit :
> On Tue, 23 Oct 2007 11:48:08 +0200, Loic Mahe wrote:
>
>> even shorter:
>>
>> def funcA(tarray):
>> s = min(len(tarray), 3)
>> return [2, 3, 4][0:s] + [e for e in funcB(3-s)[0:3-s]]

>
> Why the list comprehension!?
>
> Ciao,
> Marc 'Blackjack' Rintsch


sorry I just read too fast
and thought he worked with lists ...
anyway 'e for e in' and so list comprehension was useless here

def funcA(tarray):
s = min(len(tarray), 3)
return (2, 3, 4,)[0:s] + funcB(3-s)[0:3-s]

this is ok if funcB(...) returns a tuple ...
if it returns a list just add: tuple(funcB(...))

note: list comprehension transforms a tuple into a list
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to implement this function so that the vector can be passed backto the calling function? xz C++ 2 03-14-2008 11:02 AM
The best way to implement this non-power-of-two modulo-like function on a limited subtype? Murph VHDL 4 12-06-2006 09:35 AM
object-like macro used like function-like macro Patrick Kowalzick C++ 5 03-14-2006 03:30 PM
j2-like voicemail/fax: how to implement? Guan Yang Cisco 4 01-23-2004 10:49 PM



Advertisments