DataStructuresAndAlgorithms/Codes/Practical-A1.py

189 lines
5.7 KiB
Python
Raw Normal View History

2024-02-24 01:05:11 +05:30
'''
Problem Statement: Consider telephone book database of N clients. Make use of a hash table implementation to quickly look up client's telephone number. Make use of two collision handling techniques and compare them using number of comparisons required to find a set of telephone numbers.
Code from Data Structures and Algorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
'''
# BEGINNING OF CODE
class HashEntry:
# Blueprint for entry in hash table
def __init__(self):
# Initializing empty values
self.name = None
self.num = -1
def insert(self, name, num):
# Insert value
self.name = name
self.num = num
def collision(self):
# Handle collision
return self.name is not None
def hashOne(size, val):
# Calculate first hashing function
return val % size
def hashTwo(val):
# Calculate second hashing function
return 7 - (val % 7)
def finalHash(val, size, i):
# Determine final position of value in hash table (after first and second hash is caluclated)
return (hashOne(size, val) + i * hashTwo(val)) % size
def stringToInt(strn):
# ASCII value of each character
sum = 0
for i in strn:
sum += ord(i)
return sum
class LinearProbing:
# Function to handle linear probing
def __init__(self, size):
self.size = size
self.HashTable = []
for _ in range(size):
self.HashTable.append(HashEntry())
def insert(self):
inputStr = input("Enter telephone NUMBER and NAME of client (separated by space):\t")
inputVal = inputStr.split()
if len(inputVal) != 2:
print("\n==========\nPlease enter both telephone number and name.\n==========")
return
num, name = inputVal
name2 = stringToInt(name)
pos = hashOne(self.size, name2)
i = 1
while self.HashTable[pos].collision():
pos = (pos + i) % self.size
i += 1
self.HashTable[pos].insert(name, num)
print("\n==========\nInserted\n==========")
def search(self):
name = input("Enter name of the client:\t")
name2 = stringToInt(name)
pos = hashOne(self.size, name2)
i = 1
while self.HashTable[pos].name != name:
pos = (pos + i) % self.size
i += 1
if i == self.size + 1:
break
else:
print("\n==========\nTelephone number of the client", name, "is", self.HashTable[pos].num, "\n==========")
return
print("\n==========\nClient not found.\n==========")
def display(self):
j = 0
print("Pos", "Name", "Value", sep="\t|\t")
print("-----", "-----", "-----", sep="\t+\t")
for i in self.HashTable:
print(j, i.name, i.num, sep="\t|\t")
j += 1
class DoubleHashing:
# Function to handle double hashing
def __init__(self, size):
self.size = size
self.HashTable = []
for _ in range(size):
self.HashTable.append(HashEntry())
def insert(self):
inputStr = input("Enter telephone NUMBER and NAME of client (separated by space):\t")
inputVal = inputStr.split()
if len(inputVal) != 2:
print("\n==========\nPlease enter both telephone number and name.\n==========")
return
num, name = inputVal
name2 = stringToInt(name)
i = 0
while True:
pos = finalHash(name2, self.size, i)
if self.HashTable[pos].collision():
i += 1
else:
break
self.HashTable[pos].insert(name, num)
print("\n==========\nInserted\n==========")
def search(self):
name = input("Enter name of the client:\t")
name2 = stringToInt(name)
i = 0
while True:
pos = finalHash(name2, self.size, i)
if self.HashTable[pos].name != name:
i += 1
else:
break
if i == self.size:
break
print("\n==========\nTelephone number of client", name, "is", self.HashTable[pos].num, "\n==========")
def display(self):
j = 0
print("Pos", "Name", "Value", sep="\t|\t")
print("-----", "-----", "-----", sep="\t+\t")
for i in self.HashTable:
print(j, i.name, i.num, sep="\t|\t")
j += 1
def main():
# Main function with options
tableSize = int(input("Enter size of hash table:\t"))
method = None
while True:
print("----- MAIN MENU -----")
print("1 -> Linear Probing")
print("2 -> Double Hashing")
print("3 -> Exit")
optn = int(input("Choose an option (1-3):\t"))
if optn == 1:
method = LinearProbing(tableSize)
elif optn == 2:
method = DoubleHashing(tableSize)
elif optn == 3:
print("\n\n## END OF CODE\n")
exit(1)
else:
print("Please choose a valid option (1-3).")
continue
while True:
print("\n----- CHOOSE OPERATION (HASH TABLE) -----")
print("1 -> Insert")
print("2 -> Search")
print("3 -> Display")
print("4 -> Return to previous menu")
optn = int(input("Choose an option (1-4):\t"))
if optn == 1:
method.insert()
elif optn == 2:
method.search()
elif optn == 3:
method.display()
elif optn == 4:
break
else:
print("Please choose a valid option (1-4).")
continue
# Calling main function
main()
# END OF CODE