On Mon, Jan 26, 2004 at 04:47:43PM -0800, Josiah Carlson wrote: > This results in the simplification of the (0, None) examples into one, > but introduces an index variable and sequence lookups for the general > case: > > def findmin_General(seq): > if not len(seq): > raise TypeError("Sequence is empty") > cur = seq[0] > for i in xrange(1, len(seq)): > cur = min(seq[i], cur) > return cur > > def findmin_Max(seq): > if not len(seq): > raise TypeError("Sequence is empty") > cur = Max > for obj in seq: > cur = min(obj, cur) > return cur > > Now they both have the same number of lines, but I find the second one > a bit clearer due its lack of sequence indexing. I'd avoid the sequence indexing by writing the following: def findmin_Iter(seq): seq = iter(seq) cur = seq.next() for i in seq: cur = min(i, cur) return cur This code will raise StopIteration, not TypeError, if the sequence is empty. Jeff
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4