-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet.py
58 lines (47 loc) · 1.82 KB
/
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
58
class Set:
# Creates an empty set instance
def __init__( self ):
self._theElements = list()
# Returns the number of items in the set.
def __len__( self ):
return len( self._theElements )
# Determines if an element is in the set.
def __contains__( self, element ):
return element in self._theElements
# Adds a new unique element to the set.
def add( self, element ):
if element not in self :
self._theElements.append( element )
# Removes an element from the set.
def remove( self, element ):
assert element in self,"The element must be in the set."
self._theElements.remove( item )
# Determines if two sets are equal.
def __eq__( self, setB ):
if len( self ) != len( setB ) :
return False
else:
return self.isSubsetOf( setB )
def isSubsetOf( self, setB ):
for element in self :
if element not in setB :
return False
return True
# Creates a new set from the union of this set and setB.
def union( self, setB ):
newSet = Set()
newSet._theElements.extend( self._theElements )
for element in setB :
if element not in self :
newSet._theElements.append( element )
return newSet
# Creates a new set from the intersection: self set and setB.
# def interset( self, setB ):
# Creates a new set from the difference: self set and setB.
# def difference( self, setB ):
# Returns an iterator for traversing the list of items.
def __iter__( self ):
return _SetIterator( self._theElements )
class _SetIterator:
for i in range(len(Set())):
print(Set())