misc.py

changeset 1
f9788970fa46
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/misc.py	Wed Jul 29 23:45:53 2020 +0300
@@ -0,0 +1,21 @@
+from configparser import ConfigParser
+profile = ConfigParser()
+
+def minmax(data):
+	'''
+	From: http://code.activestate.com/recipes/577916-fast-minmax-function/
+	Computes the minimum and maximum values in one-pass using only
+	1.5*len(data) comparisons
+	'''
+	import itertools
+	it = iter(data)
+	try:
+		lo = hi = next(it)
+	except StopIteration:
+		raise ValueError('minmax() arg is an empty sequence')
+	for x, y in itertools.zip_longest(it, it, fillvalue = lo):
+		if x > y:
+			x, y = y, x
+		lo = min(x, lo)
+		hi = max(y, hi)
+	return lo, hi

mercurial