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