-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathexercise_19.py
56 lines (44 loc) · 1.29 KB
/
exercise_19.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
"""This set class represents a classical set."""
class Set:
def __init__(self, elements):
self.set = []
for i in elements:
self.set.append(i)
def addElement(self, x):
self.set.append(x)
def deleteElement(self, x):
if self.member(x):
self.set.remove(x)
def member(self, x):
if x in self.set:
return True
else:
return False
def intersection(self, set2):
intersection = []
for x in set2:
if x in (set2 and self.set):
intersection.append(x)
return intersection
def union(self, set2):
for x in set2:
self.set.append(x)
def subtract(self, set2):
for x in set2:
while x in self.set:
self.set.remove(x)
def getSet(self):
return self.set
def main():
list1 = ['cat', 'dog', 'fish', 'mouse', 'banana']
test = Set(list1)
test.addElement('small')
test.deleteElement('dog')
list2 = ['flat', 'cat', 'rectangle', 'banana', 'dog', 'mouse', 'lemon']
list3 = ['cat', 'dog', 'mouse', 'fish']
intersect = test.intersection(list2)
test.union(list2)
test.subtract(list3)
print(test.getSet())
print(intersect)
main()