473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to construct this function

I am trying to construct a function called maxSquare. In which it takes a list of integers X and returns Xi with the maximum square value, without using loops.

Sample Input/Output

>>> maxSquare([5, ‐7, 3])
-7


If you coul help me with this, I would appreciate it.

Thanks in advance.
Apr 1 '11 #1
4 1536
bvdet
2,851 Expert Mod 2GB
Here's one way of doing it without using a loop:
Expand|Select|Wrap|Line Numbers
  1. >>> import operator
  2. >>> max(map(lambda x:operator.pow(x, 2), [5, -7, 3]))
  3. 49
  4. >>> 
Apr 1 '11 #2
Thank you bvdet for your solution, you are almost there.
But, how can we return the original value which are -7?

>>> maxSquare([5, ‐7, 3])
-7
Apr 1 '11 #3
Rabbit
12,516 Expert Mod 8TB
Do the same thing but instead of using the pow function, use an absolute value function.
Apr 1 '11 #4
bvdet
2,851 Expert Mod 2GB
It can be done using an additional variable, list method index() and the indexing operator.
Expand|Select|Wrap|Line Numbers
  1. >>> numList = 5, -7, 3
  2. >>> sqList = map(lambda x:operator.pow(x, 2), numList)
  3. >>> numList[sqList.index(max(sqList))], max(sqList)
  4. (-7, 49)
  5. >>> 
Apr 2 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Karl Scalet | last post by:
Hi, quite often there is a need to just print out the items of a list. that would be nice, except prt() does not exist, and print is a statement not a function, hence cannot replace prt as...
9
by: Matt Eberts | last post by:
Sorry, bad title. Anyway, is there a way to pass the arguments to an object instantiated via a constructor using the arguments object and have it expanded, so to speak, so that it doesn't appear as...
1
by: glennpierce | last post by:
Hi I was wondering if anyone knows of a method to achieve the creation of events in c. currently I use a function pointer to call one callback. However, I really need to map the function pointer or...
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
17
by: mouseit101 | last post by:
Hello, I'm trying to make a navigation bar and need to assign mouseover, mouseclick etc. events to a img tag dynamically, here's what im using: function getP_Element(p_imgsrc,p_id) {var tuf =...
16
by: Ilias Lazaridis | last post by:
I have a few small questions subjecting python functionality, most importantly the "alias_method". - *IMPORT* I would like to know, if this construct is valid, or if it can result in...
4
by: golubovsky | last post by:
Hi, Is there an easy way to construct a function call out of a function and an array of actual arguments? e. g. given a function `fun' and an array I need to obtain equivalent of...
14
by: Nickolay Ponomarev | last post by:
Hi, Why does http://www.jibbering.com/faq/ uses new Function constructor instead of function expressions (function(...) { ... }) when defining new functions? E.g. for LTrim and toFixed. Is the...
5
by: kkirtac | last post by:
Hi, i have a class "myClass", and i want to return two instances of the same class from a function. I define an array as follows : "myClass sample ;" and i initialize two instances: "myClass...
6
by: eeriehunk | last post by:
Hi Guys, This is a very common interview question but I am unable to find the right answer for this. Kindly let me know. What occurs if a procedure or function terminates with failure without being...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.