forked from zwmccall/Python_Projects-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashSet.py
57 lines (52 loc) · 1.36 KB
/
HashSet.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
#Simple HashSet
#@author Prohunt
import sys
class HashSet:
cap = sys.maxint/20
list = sys.maxint/20*[None]
size = 0
def hash(self, element):
index = hash(element)
if index < 0:
index = index * -1
while index > self.cap:
index = index / 10
return index
def contains(self, element):
index = self.hash(element)
print " "
print index
if self.list[index] is element :
return True
else :
while self.list[index] is not element:
if self.list[index] is None:
return False
index = index + 1
return True
def add(self, element):
if self.contains(element):
return False
index = self.hash(element)
while self.list[index] is not None:
index = index + 1
self.list[index] = element
self.size = self.size + 1
return True
def getSize(self):
return self.size
def init(self):
self.size = 0
def main():
string1 = "hello"
string2 = "world"
stringSet = HashSet()
stringSet.add(string1)
stringSet.add(string2)
if stringSet.contains("hello") and stringSet.contains("world"):
print "set contains both hello and world"
if not stringSet.contains("Hello") and not stringSet.contains("World"):
print "set does not contain Hello and World"
print "Size of the list: "
print stringSet.getSize()
if __name__ == "__main__" :main()