-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_table.py
133 lines (107 loc) · 3.59 KB
/
hash_table.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'''
Implement a hash table with the following methods:
HashTable() constructs a new instance of a hash table
put(int key, int val) updates the hash table such that key maps to val
get(int key) returns the value associated with key. If there is no such key, then return -1.
remove(int key) removes both the key and the value associated with it in the hash table.
This should be implemented without using built-in hash table.
'''
class Node:
def __init__(self, key=None, value=None):
self.key = key
self.value = value
self.next = None
class HashTable:
def __init__(self):
self.hashmap = [None for i in range(1001)]
def find_index(self, key):
return key % 1001
def find(self, key, node):
prev = None
cur = node
while cur and cur.key != key:
prev = cur
cur = cur.next
return prev
def put(self, key, value):
index = self.find_index(key)
if self.hashmap[index] is None:
self.hashmap[index] = Node()
node = self.hashmap[index]
node = self.find(key, node)
if node.next:
node.next.value = value
else:
node.next = Node(key, value)
def get(self, key):
index = self.find_index(key)
if self.hashmap[index] is None:
return -1
node = self.hashmap[index]
node = self.find(key, node)
if node.next:
return node.next.value
else:
return -1
def remove(self, key):
index = self.find_index(key)
if self.hashmap[index] is None:
return
node = self.hashmap[index]
node = self.find(key, node)
if node.next:
node.next = node.next.next
else:
return
----------------------------------------------------------------------------------------------------------
#my own rewriting
class Node:
def __init__(self, key= None, value=None):
self.key = key
self.value = value
self.next = None
class HashTable:
def __init__(self):
self.hashmap = [None for i in range(1001)]
def find_index(self,key):
return key % 1001
def find(self,key, node): #return prev
prev = None
cur = node
while cur and cur.key != key:
prev = cur
cur = cur.next
return prev
def put(self, key,value):
index = self.find_index(key)
if self.hashmap[index] is None:
self.hashmap[index] = Node()
node = self.hashmap[index] #this first finds the node
node = self.find(key,node)#this gives back prev of the node
if node.next: #this code adds value to the prev
node.next.value = value
else:
node.next = Node(key,value)
def get(self,key):
index = self.find_index(key)
if self.hashmap[index] is None:
return -1
else:
node = self.hashmap[index]
node = self.find(key,node)
if node.next:
return node.next.value
else:
return -1
#so for every operation - you always find the index of the key instead of directly looking via key!
def remove(self,key):
index = self.find_index(key)
if self.hashmap[index] is None:
return
else:
node = self.hashmap[index]
node = self.find(key,node)#this gives back prev node
if node.next:
node.next = node.next.next
else:
return