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.
4 1536 bvdet 2,851
Expert Mod 2GB
Here's one way of doing it without using a loop: - >>> import operator
-
>>> max(map(lambda x:operator.pow(x, 2), [5, -7, 3]))
-
49
-
>>>
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
Do the same thing but instead of using the pow function, use an absolute value function.
bvdet 2,851
Expert Mod 2GB
It can be done using an additional variable, list method index() and the indexing operator. - >>> numList = 5, -7, 3
-
>>> sqList = map(lambda x:operator.pow(x, 2), numList)
-
>>> numList[sqList.index(max(sqList))], max(sqList)
-
(-7, 49)
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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 =...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
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,...
|
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...
|
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,...
| |