diff common/misc.py @ 355:430c9e92cd23

Added common directory
author Joseph Turian <turian@iro.umontreal.ca>
date Thu, 19 Jun 2008 16:12:29 -0400
parents
children 2291a244a887
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/misc.py	Thu Jun 19 16:12:29 2008 -0400
@@ -0,0 +1,25 @@
+def sign(i, assertions=True):
+    """
+    + or - 1
+    @precondition: i != 0
+    """
+    if assertions:
+        assert i != 0
+    else:
+        if i == 0: return 0
+
+    return +1 if i > 0 else -1
+
+def percent_string(a, b):
+    assert a <= b
+    assert a >= 0
+    assert b > 0
+    return "%s of %s (%.2f%%)" % (a, b, 100.*a/b)
+
+def unique_elements_list_intersection(list1,list2):
+    """
+    Return the unique elements that are in both list1 and list2
+    (repeated elements in listi will not be duplicated in the result).
+    This should run in O(n1+n2) where n1=|list1|, n2=|list2|.
+    """
+    return list(set.intersection(set(list1),set(list2)))