How to implement function like this?

Discussion in 'Python' started by Yinghe Chen, Oct 23, 2007.

  1. Yinghe Chen

    Yinghe Chen Guest

    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,
     
    Yinghe Chen, Oct 23, 2007
    #1
    1. Advertising

  2. 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
     
    Marc 'BlackJack' Rintsch, Oct 23, 2007
    #2
    1. Advertising

  3. Yinghe Chen

    Paul Rubin Guest

    "Yinghe Chen" <> 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))
     
    Paul Rubin, Oct 23, 2007
    #3
  4. Yinghe Chen

    Loic Mahe Guest

    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]]
     
    Loic Mahe, Oct 23, 2007
    #4
  5. 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
     
    Marc 'BlackJack' Rintsch, Oct 23, 2007
    #5
  6. Yinghe Chen

    Loic Mahe Guest

    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
     
    Loic Mahe, Oct 23, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vince C.
    Replies:
    4
    Views:
    2,797
    Vince C.
    Jul 27, 2003
  2. Murph
    Replies:
    4
    Views:
    542
    Ben Jones
    Dec 6, 2006
  3. Patrick Kowalzick
    Replies:
    5
    Views:
    485
    Patrick Kowalzick
    Mar 14, 2006
  4. xz
    Replies:
    2
    Views:
    407
    James Kanze
    Mar 14, 2008
  5. iMath
    Replies:
    12
    Views:
    193
    Ned Batchelder
    Dec 2, 2013
Loading...

Share This Page