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

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-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.

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