comparison 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
comparison
equal deleted inserted replaced
354:d580b3a369a4 355:430c9e92cd23
1 def sign(i, assertions=True):
2 """
3 + or - 1
4 @precondition: i != 0
5 """
6 if assertions:
7 assert i != 0
8 else:
9 if i == 0: return 0
10
11 return +1 if i > 0 else -1
12
13 def percent_string(a, b):
14 assert a <= b
15 assert a >= 0
16 assert b > 0
17 return "%s of %s (%.2f%%)" % (a, b, 100.*a/b)
18
19 def unique_elements_list_intersection(list1,list2):
20 """
21 Return the unique elements that are in both list1 and list2
22 (repeated elements in listi will not be duplicated in the result).
23 This should run in O(n1+n2) where n1=|list1|, n2=|list2|.
24 """
25 return list(set.intersection(set(list1),set(list2)))