473,799 Members | 2,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to implement function like this?

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,
Oct 23 '07 #1
5 1228
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_lengt h = len(result) - t_array_length
if t_array_length < len(result):
result = (result[:t_array_length]
+ func_b(remainin g_length)[:remaining_leng th])
return tuple(result)

Ciao,
Marc 'BlackJack' Rintsch
Oct 23 '07 #2
"Yinghe Chen" <yi*********@je ppesen.com.rmwr ites:
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))
Oct 23 '07 #3
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]]
Oct 23 '07 #4
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
Oct 23 '07 #5
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
Oct 23 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
3157
by: Oplec | last post by:
Hi, I am learning standard C++ as a hobby. The C++ Programming Language : Special Edition has been the principal source for my information. I read the entirety of the book and concluded that I had done myself a disservice by having not attempted and completed the exercises. I intend to rectify that. My current routine is to read a chapter and then attempt every exercise problem, and I find that this process is leading to a greater...
6
3832
by: Charles Law | last post by:
This is going to seem like a basic OO question, but it comes up and bites me every now and again. Suppose we have a multi-tiered protocol to implement, what is the logical, OO way to design the handler? For example, let's say that we have to implement the ISO 7-layer protocol, or something like an Allen-Bradley master-slave protocol. At the lowest layer we might only need to top and tail the data being transported, such as DLE STX DLE...
1
574
by: wukexin | last post by:
I write my own class Cfile, I want to know what about implement ctime().Who help me? My use function ctime, I sign it with $$$. my class Cfile: #------------------------ file.h #--------------------------- #include <io.h> #include <ctime> #include <string>
7
26465
by: Don | last post by:
Can anyone give me an example of implementing ICloneable to give a class I created a "Clone" method so I can make copies of objects. I have no idea where to begin with this. Thanks. - Don
4
16797
by: GrandpaB | last post by:
Hi, I recently had a post about how to block Mousewheel events. The answer was to implement an IMessageFilter. Sadly, I must report that after 24 hours of researching my library and online resources I am still stumped! I did find a promising code snippet on MSDN, but have yet to successfully implement it. Here is the code snippet: Public Class MessageFilter
5
19601
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was having a problem in the calling program. I searched online and found suggestions that I return an Array instead so I modified my code (below) to return an Array instead of an ArrayList. Now I get the message when I try to run just my webservice...
3
2321
by: alex | last post by:
I want to implement an async-function,just like Oracle OCI function: for example: I call oci "execute(strSQL)" function, and I can call "cancle" function to cancle the process if the SQL execute too long. How to implement the execute and cancle functions? thx!
0
2839
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass magic, I wrote the following module. It is mainly useful as a light weight tool to help programmers catch mistakes at definition time (e.g., forgetting to implement a method required by the given interface). This is handy when unit tests or...
19
40819
by: UG | last post by:
I just wanted to know whether any timer facility exists in C, as it is not mentioned in K&R 2, or in the ISO Draft. By timer function i mean that when we use standard input function like scanf() or getch() or any other function, the interface stops to take input from user but what if user doesn't give input for hours, the program will still be waiting. Is there any way to circumvent the scanf() (or any other input function for that matter)...
2
1678
shnaqawi
by: shnaqawi | last post by:
Dear Friends, I have an objectdatasource which is bound to a details view. the no. of columns are variable. I could do the select function by defining a dynamic sqlSelectCommand. The problem now is that how can I have an update function for myobjectdatasource? I tried a function with variable arguments within an array but after that the objectdatasource asked for defining the parameter for the array. I don't know how to implement the update...
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9546
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10243
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9078
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7570
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5590
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.