0% found this document useful (0 votes)
40 views

dsa1

DSA Lab Manual Assignment 1 Code for Computer Engineering and Artificial Intelligence and Data Science Branch SPPU

Uploaded by

abhinavsargar1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

dsa1

DSA Lab Manual Assignment 1 Code for Computer Engineering and Artificial Intelligence and Data Science Branch SPPU

Uploaded by

abhinavsargar1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

class HashFunction:

def __init__(self):
self.h = [{"key": -1, "name": "NULL"} for _ in
range(10)]
def find(self, k):
for i in range(10):
if self.h[i]["key"] == k:
print(f"\n\t{k} is Found at {i}
Location with Name {self.h[i]['name']}")
return i
return -1
def delete(self, k):
index = self.find(k)
if index == -1:
print("\n\tKey Not Found")
else:
self.h[index] = {"key": -1, "name": "NULL"}
print("\n\tKey is Deleted")
def display(self):
print("\n\t\tKey\t\tName")
for i in range(10):
# Adjust formatting with padding to align
the columns properly
print(f"\th[{i}]\t{self.h[i]['key']}\t\
t{self.h[i]['name']}")
def insert(self):
cnt = 0
while cnt < 10:
ans = input("\n\t..... Do You Want to
Insert More Key (y/n): ")
if ans.lower() != 'y':
break
k = int(input("\n\tEnter a Telephone No:
"))
name = input("\n\tEnter a Client Name: ")
hi = k % 10 # Hash function
if self.h[hi]["key"] == -1:
self.h[hi] = {"key": k, "name": name}
else:
temp_key, temp_name = self.h[hi]
["key"], self.h[hi]["name"]
self.h[hi] = {"key": k, "name": name}
flag = False
for i in range(hi + 1, 10):
if self.h[i]["key"] == -1:
self.h[i] = {"key": temp_key,
"name": temp_name}
flag = True
break
if not flag:
for i in range(hi):
if self.h[i]["key"] == -1:
self.h[i] = {"key":
temp_key, "name": temp_name}
break
cnt += 1
def main(self):
while True:
print("\n\t***** Dictionary (ADT) *****")
print("\t1. Insert\n\t2. Display\n\t3.
Find\n\t4. Delete\n\t5. Exit")
choice = int(input("\n\t..... Enter Your
Choice: "))
if choice == 1:
self.insert()
elif choice == 2:
self.display()
elif choice == 3:
k = int(input("\n\tEnter a Key Which
You Want to Search: "))
if self.find(k) == -1:
print("\n\tKey Not Found")
elif choice == 4:
k = int(input("\n\tEnter a Key Which
You Want to Delete: "))
self.delete(k)
elif choice == 5:
break
else:
print("\n\tInvalid Choice")
ans = input("\n\t..... Do You Want to
Continue in Main Menu (y/n): ")
if ans.lower() != 'y':
break
# Create an instance of HashFunction and start the
program
hash_table = HashFunction()
hash_table.main()
OUTPUT:

You might also like