-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet.py
More file actions
93 lines (72 loc) · 2.54 KB
/
Set.py
File metadata and controls
93 lines (72 loc) · 2.54 KB
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
class Set(object):
def __init__(self, elements):
self.elements = list()
for i in elements:
if i not in self.elements:
self.elements.append(i)
def add(self, element):
'''Add an element into the set
Args:
element: The element.
'''
if element not in self.elements:
self.elements.append(element)
def remove(self, element):
'''Remove an element from the set
Args:
element: The element.
'''
if element in self.elements:
self.elements.remove(element)
def diff(self, setB):
'''Difference between two sets
Args:
setB: The set passed as a parameter.
Returns:
A list containing the difference between the two sets.
'''
return [a for a in self.elements if a not in setB.elements]
def intersection(self, setB):
'''Intersection between two sets
Args:
setB: The set passed as a parameter.
Returns:
A list containing the elements present in both sets.
'''
return [a for a in self.elements if a in setB.elements]
def isIncluded(self, setB):
'''Verify id the set includes the setB
Args:
setB: The set passed as a parameter.
Returns:
True if the set is included in setB, otherwise returns false
'''
return reduce(lambda x, y: x and y, map(lambda x: True if x in setB.elements else False, self.elements), True)
def diffSim(self, setB):
'''Simetric Difference between two sets
Args:
setB: The set passed as a parameter.
Returns:
A list containing the simetric difference between the two sets.
'''
return self.diff(setB) + setB.diff(self)
def prodCart(self, setB):
'''Cartesian product between two sets
Args:
setB: The set passed as a parameter.
Returns:
A list containing each subset of the cartesian product between the two sets.
'''
return [(a, b) for a in self.elements for b in setB.elements]
def pot(self):
'''Simetric Difference between two sets
Returns:
A list containing the simetric difference between the two sets.
'''
result = [[]]
for i in self.elements:
newSets = [x + [i] for x in result]
result.extend(newSets)
return result
def __str__(self):
return " ".join(str(self.elements))