355
|
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)))
|