-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcomFuncs.py
100 lines (60 loc) · 1.22 KB
/
comFuncs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
'''
COMBINATORIAL FUNCTIONS
-----------------------
General purpose use for cleaner
operations in the code of this directory
'''
from math import factorial
def combination(n, k):
try:
x = factorial(n) / (factorial(k) * factorial(n-k))
except:
x = 0
return x
def k_permutation(n, k):
try:
x = factorial(n) / factorial(n-k)
except:
x = 0
return x
'''SET OPERATIONS LISTED BELOW--
ASSUMES SETS ARE REPRESENTED IN
BINARY FORMAT WITH SAME LENGTH'''
def insert(a, A):
A[a-1] = 1
return A
def element_of(a, A):
if A[a-1] == 1:
return True
else:
return False
def union(A, B):
'''Union of two sets
Algorithm 1.6 in book'''
C = []
if len(A) != len(B):
return None
for i in range (0, len(A)):
C.append( A[i] | B[i] )
return C
def intersection(A, B):
'''Intersection of two sets
Algorithm 1.7 in book'''
C = []
if len(A) != len(B):
return None
for i in range (0, len(A)):
C.append( A[i] & B[i] )
return C
def set_diff(A, B):
C = []
if len(A) != len(B):
return None
for i in range (0, len(A)):
#1 exclusive or x flips bit
C.append( A[i] & (1^B[i]) )
return C
def sym_diff(A, B):
C = set_diff(A, B)
D = set_diff(B, A)
return union(C, D)