-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_set.py
57 lines (45 loc) · 1.12 KB
/
my_set.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
# my_set.py
class ListSet():
def __init__(self):
self.items = []
def __iter__(self):
return iter(self.items)
def __len__(self):
return len(self.items)
def __contains__(self, item):
return item in self.items
def __str__(self):
return "Set of " + str(len(self.items)) + " elements"
def add(self, item):
if not item in self.items:
self.items.append(item)
def union(self, other):
result = ListSet()
for item in self:
result.add(item)
for item in other:
result.add(item)
return result
def intersection(self, other):
result = ListSet()
for item in self:
if item is other:
result.add(item)
return result
def remove(self, item):
self.items.remove(item)
if __name__ == "__main__" :
a = ListSet()
a.add(1)
a.add(5.3)
a.add("Hi")
print(len(a))
print (a)
t = ListSet()
t.add(3)
t.add(10)
t.add(1)
b = a.union(t)
print(b)
c = a.intersection(t)
print(c)