Computer Science Canada Find Highest/Lowest Number in an Array |
Author: | Planet_Fall [ Mon Jan 20, 2014 9:49 am ] | ||
Post subject: | Find Highest/Lowest Number in an Array | ||
Is there a command that searches an array to find the lowest/highest number? They way I do it looks a little inefficient.
|
Author: | Insectoid [ Mon Jan 20, 2014 12:44 pm ] |
Post subject: | RE:Find Highest/Lowest Number in an Array |
Without sorting, this is the most efficient method. You can, of course, halve the runtime by looking for both the lowest and highest values in the same loop. |
Author: | Raknarg [ Mon Jan 20, 2014 1:19 pm ] |
Post subject: | RE:Find Highest/Lowest Number in an Array |
This method is quite efficient. It runs in linear time (basically means you only have to go through the list once) to find your solution, which is fast. you could have 100000 elements in the list with no real difference in speed. |
Author: | Zren [ Mon Jan 20, 2014 8:48 pm ] | ||||
Post subject: | RE:Find Highest/Lowest Number in an Array | ||||
Because one liners are awesome.
http://docs.python.org/2/library/functions.html#reduce Less efficient, but also less code. |
Author: | Dreadnought [ Mon Jan 20, 2014 8:57 pm ] | ||||||
Post subject: | Re: Find Highest/Lowest Number in an Array | ||||||
Zren wrote: Because one liners are awesome.
http://docs.python.org/2/library/functions.html#reduce Less efficient, but also less code. If you're going to use min/max anyway why not just do this?
|
Author: | Zren [ Mon Jan 20, 2014 9:12 pm ] |
Post subject: | Re: Find Highest/Lowest Number in an Array |
Dreadnought @ Mon Jan 20, 2014 8:57 pm wrote: If you're going to use min/max anyway why not just do this? Because I've never read the docs for min/max. xD Never realized it accepted iterables. For anyone interested in the source code of the built in min/max functions: http://hg.python.org/cpython/file/0bcf1669912a/Python/bltinmodule.c#l1348 |