Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Python-Min/max of seq WITHOUT min() and max()

Name: Anonymous 2007-02-08 21:02

Given a sequence such as x = [5, 9, 0, -14], I need to find the min and max without sorting the sequence or by using min() and max().  I've been instructed to use loop constructs, but I can't figure out how to do so.  Help is greatly appreciated.

Name: Anonymous 2007-02-08 21:12 (sage)

min = x[0]
for y in x:
  if y < min: min = y

something like that probably. and i've never even used python, you fucking retard.

Name: Anonymous 2007-02-08 22:07 (sage)

>>2
truth...

Name: Anonymous 2007-02-09 16:33

>>1
Quit your programming class, you're never going to competent. Put your energy into something you have a chance at succeeding at.

Name: Anonymous 2007-02-09 16:53

minmax :: [a] -> (a, a)
minmax x = (head . sort $ x, last . sort $ x)

Name: Anonymous 2007-02-09 16:55

>>5
without sorting the sequencewithout sorting the sequencewithout sorting the sequencewithout sorting the sequencewithout sorting the sequencewithout sorting the sequencewithout sorting the sequencewithout sorting the sequencewithout sorting the sequencewithout sorting without sorting the sequencewithout sorting the sequencewithout sorting the sequencewithout sorting the sequencewithout sorting the sequencewithout sorting the sequencewithout sorting the sequencethe sequence

Name: Anonymous 2007-02-09 17:27

>>6
I think >>5 does exactly the same as >>2

Name: Anonymous 2007-02-09 17:38

>>6
WTF I SKIPPED THAT PART AND ONLY READ THE LAST PART

OH AND LISTEN TO >>7 HE IS RIGHT, I AM RIGHT, YOU ARE WRRRRRRRRONG~~~~~

Here's another solution, then:
minmax :: [a] -> (a, a)
minmax x = (min x, max x)

Name: Anonymous 2007-02-09 18:07

>>8
There is a problem with your code. It's Haskell.


x = [5, 9, 0, -14]
max = x[0]
min = x[0]

for num in x:
  if num < min:
    min = num
  if num > max:
    max = num


That'd be my solution. Python seems to lack support for +/- infinity, which would have been nice...

If you want to clean up just a little, you could use x[1:] instead of x in the loop, to skip the first value.

Name: Anonymous 2007-02-09 18:25

>>9
there is a problem with your hypothesis. it is an ipsedixitism

Name: Anonymous 2007-02-09 18:42

>>9
You can easily add an int, float or long subclass that supports infinity.

Start like:
class PosInfinityType: pass
PosInfinity = PosInfinityType()
class NegInfinityType: pass
NegInfinity = NegInfinityType()

Then go on like:
class iint(int):

and redefine all the special methods (double underscored div, add, eq, lt, etc, check the docs). It's pretty easy.

Name: Anonymous 2007-02-09 18:50

>>11
wtf?

Name: Anonymous 2007-02-09 19:03

it's good that no one knows which anonymous I am. I wrote >>12 without reading the end of >>10, and thought >>11 is replying to the JavaScript thread

Name: Anonymous 2007-02-10 6:25

>>10
It's not a hypothesis, it's a block of code. Since it's a block of code, it can't possibly lack logical argumentation (any more than a block of wood can lack logical argumentation), hence it's not an ipsedixitism either.

tl;dr version: FAIL!

Name: Anonymous 2007-02-10 7:43

Name: Anonymous 2007-02-10 7:44

>>13
no, I wrote >>12, you write >>11 in reply to >>8

Name: Anonymous 2007-02-10 8:06

This is why Python sucks
x = [5, 9, 0, -14]
max = x[0]
min = x[0]

for num in x:
if num < min:
min = num
if num > max:
max = num

What was the algorithm again? You fail :D

Name: Anonymous 2007-02-10 9:38

>>17
the same happens if you on purpose remove braces from a language that uses them as block delimiters. you seem to assume that it's common for indentation to get trimmed off, but that argument obviously does not hold water, just because all the modern ides and editors would never do that by accident. logic failget.

Name: Anonymous 2007-02-10 9:39

>>17
If you indent code like that, YOU fail.

Name: Anonymous 2007-02-10 9:41

>>16
wtf, I did not, you libelous bastard

>>14
if I was referring to the code, I would have said so. the unfamiliar term must have confused you. I must remember to dumb it down for nincompoops like you next time

Name: Anonymous 2007-02-10 9:42

One word, a forced cultural item. Meme over.

Name: Anonymous 2007-02-10 10:10

OK GUYS HERES A REAL HASKELL SOLUTION NOTE I DID NOT ACHIEVE SATORI YET OK

minmax :: [a] -> (a, a)
minmax l = (foldr (min) (head l) l, foldr (max) (head l) l)


One words, no indentation. Thread over.

Name: Anonymous 2007-02-10 11:07

Here's a REAL Haskell solution:
import Control.Arrow ((&&&))
import Data.List (foldl1')

minmax :: Ord a => [a] -> (a,a)
minmax = foldl1' min &&& foldl1' max

Name: Anonymous 2007-02-10 13:29

>>23
I'm still trying to grok monads and now arrows are the cool thing :(

Name: Anonymous 2007-02-10 17:40

>>24
Just give it up, something new will come along and double the "syntax," making it impossible for even Post-Enlightenment Buddha to read Haskell.

Name: Anonymous 2007-02-11 8:19

>>22
>>23
Both of these do two loops over the input data, assuming the compiler can't fuse the two. Here's a single-pass version:

minmax lst = foldl (\(a, b) c -> (min a c, max b c)) (head lst, head lst) lst

Wankers can proceed to optimize it further with a strict foldl. As if the compiler didn't catch that when compiling with -O.

Name: Anonymous 2007-02-11 9:20

>>26
Variable with more than one letter?  BIG WARNING!!

Name: Anonymous 2007-02-11 9:21

>>26
Also, it is Haskellic to put a space after the lambda symbol.

Name: Anonymous 2007-02-11 12:17

Personally I hate using lst as a variable name because it looks too much like 1st.

Name: Anonymous 2007-02-11 14:53

>>28
No, it's just coddling to weak editors that mistake a \( for a quoted open brace and fail to match closing braces appropriately.

Name: Anonymous 2007-02-11 17:53

>>29
use better font.

Name: Anonymous 2007-02-11 18:37 (sage)

>>30
Emacs?

Name: Anonymous 2007-02-11 19:57

>>31
wingdings?

Name: Anonymous 2007-02-12 3:02

To find the min:
reduce(lambda x,y: x if x < y else y, [5, 9, 0, -14])

To find the max:
reduce(lambda x,y: x if x > y else y, [5, 9, 0, -14])

Name: Anonymous 2007-02-12 3:43

>>34
Win

Name: Anonymous 2007-02-12 6:02

>>34
wtf, reduce() fucking fails because it's harder to read and slower than list comprehensions.

Name: Anonymous 2010-06-25 14:50

WARNING: NECRO POST

Name: Sgt.Kabu諽팆kiman㠎츊 2012-05-28 22:21

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List