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

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