-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.py
81 lines (77 loc) · 2.41 KB
/
Test.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
# Consider a telephone book db of N clients. Make use of a hash table implementation to quickly
# look up client's telephone number. Make use of 2 collision handling techniques.
# and compare them using number of comparisons required to find a set of telephone numbers.
# Input :
# N clients
# For each client : Name & Telephone No.
def hash(num) -> int:
return int(num % total_numbers)
def linearProbingWithoutReplacement(h) -> int:
if(h==total_numbers-1):
h = 0
return(h)
else:
return(h+1)
def search(num) -> bool:
if(hashTable[num]!=(None, None)):
return True
else:
return False
def insert(name, num) -> int:
hashed_num = hash(num)
if(search(hashed_num)):
hashed_num = hash(num)
counter = 0
while(search(hashed_num)):
if(counter==total_numbers-1):
print("Hash Table is full!")
hashed_num = linearProbingWithoutReplacement(hashed_num)
counter += 1
hashTable[int(hashed_num)] = (name, num)
return 2
else:
hashed_num = hash(num)
hashTable[int(hashed_num)] = (name, num)
return 1
def displayHashTable() -> None:
print(f"\nEntries : {total_numbers}")
i = 0
for bucket in hashTable:
print(f"\t{i} : {bucket[0]} - {bucket[1]}")
i += 1
def isFull() -> bool:
count = 0
for bucket in hashTable:
if(bucket[0]!=None):
count += 1
if(count==total_numbers):
return True
else:
return False
while(True):
total_numbers = int(input("Enter total numbers :"))
if(total_numbers != 0):
break
hashTable = [(None, None)]*total_numbers
while(True):
choice = input("Enter Choice :\n1. Insert new number\n2. Display\n3. Exit\n-->")
try:
match int(choice):
case 1 :
if(isFull()):
print("Hash Table is full!")
else:
name = input("Name : ")
while(True):
number = int(input("Num : "))
if(number<=999999999):
print("Invalid Number! Please re-enter!")
else:
break
insert(name, number)
case 2 :
displayHashTable()
case 3:
exit()
except:
print("Enter valid choice.")