-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.py
More file actions
47 lines (33 loc) · 1006 Bytes
/
hash.py
File metadata and controls
47 lines (33 loc) · 1006 Bytes
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
def insert(h,k,v):
index = hash(k)%100
found_pair = False
for pairs in h[index]:
if k == pairs[0]:
pairs[1] = v
found_pair = True
if not found_pair:
h[index].append([k,v])
return h
def get(h,k):
index = hash(k)%100
for pairs in h[index]:
if k == pairs[0]:
return pairs[1]
return 0
def main():
hash_list = range(100)
hash_list = [[] for i in hash_list]
# print hash_list
keep_going = "y"
# prompts user for a key-value pair
while keep_going.lower() == 'y':
key = raw_input("key: ")
value = raw_input("value: ")
keep_going = raw_input("Want to add more? ('y' or 'n') ")
hash_list = insert(hash_list,key,value)
print hash_list
key_to_find_value = raw_input("Find value of: ")
found_value = get(hash_list,key_to_find_value)
print found_value
# print hash_list
main()