diff --git a/Codes/Practical-A1.cpp b/Codes/Practical-A1.cpp deleted file mode 100644 index ca7c00e..0000000 --- a/Codes/Practical-A1.cpp +++ /dev/null @@ -1,168 +0,0 @@ -// WARNING: THIS CODE HAS NOT BEEN TESTED. -// IT WILL TAKE 1-2 MONTHS TO PERFECT THE CODE. -// IF YOU FACE ANY ERRORS, UNEXPECTED BEHAVIOUR OR HAVE ANY SUGGESTIONS, -// LET US KNOW BY CREATING AN ISSUE ON OUR KSKA GIT REPOSITORY. - -/* -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 -#include -#include -#define max 10 -using namespace std; - -struct client -{ - long int iPhno; - char name[20]; - -};//end of structure - -class hashtable -{ - client ht[max]; - - public: - hashtable() //constructor - { - for(int i=0;i>c.iPhno; - iPos=hash(c.iPhno); - - if(ht[iPos].iPhno==0) - { - ht[iPos]=c; - } - else - { - for(int i=iPos+1;i%max!=iPos;i++) - { - ht[i]=c; - break; - } - } - cout<<"\n Add More:"; - cin>>cAns; - }while(cAns=='y' || cAns=='Y'); -}//end of insert - -int hashtable::hash(long int key) -{ - return(key%max); -}//end of hash - -void hashtable::display() -{ - cout<<"------------------------------------"; - cout<<"\nSrno\tPhone number\n"; - cout<<"------------------------------------\n"; - for(int i=0;i>x; - for(int i=0;i>s; - for(int i=0;i>iCh; - cout<<"\n"; - - switch(iCh) - { - case 1://insert - h.insert(); - cout<<"\n"; - break; - - case 2://display - h.display(); - cout<<"\n"; - break; - - case 3://search - h.search(y); - cout<<"\n"; - break; - - case 4://delete - h.del(s); - cout<<"\n"; - break; - - case 5://exit - break; - }//end of switch - }while(iCh!=5);//end of do -return 0; -} -// END OF CODE -// EXPERIMENTAL CODE \ No newline at end of file diff --git a/Codes/Practical-A1.py b/Codes/Practical-A1.py new file mode 100644 index 0000000..93761ba --- /dev/null +++ b/Codes/Practical-A1.py @@ -0,0 +1,188 @@ +''' +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