From 1f5df6051cc3763b04c0ecb202dab4fe4814cc5f Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner <kloeckner@haamster.(none)> Date: Thu, 22 Jan 2009 10:31:03 -0600 Subject: [PATCH] Add pytools.common_prefix(). --- src/__init__.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/__init__.py b/src/__init__.py index b4a0871..fe54717 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -575,6 +575,26 @@ def all_roughly_equal(iterable, threshold): +def common_prefix(iterable, empty=None): + it = iter(iterable) + try: + pfx = it.next() + except StopIteration: + return empty + + for v in it: + for j in range(len(pfx)): + if pfx[j] != v[j]: + pfx = pfx[:j] + if j == 0: + return pfx + break + + return pfx + + + + def decorate(function, list): return map(lambda x: (x, function(x)), list) -- GitLab