Last active
September 22, 2017 14:35
-
-
Save femmerling/4364067 to your computer and use it in GitHub Desktop.
Hashtable algorithm written in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#create the key-value object | |
class KeyValue: | |
def __init__(self,key,value): | |
self.key=key | |
self.value=value | |
class HashTable: | |
#initiate an empty list that will store all key value pair based on the KeyValue object | |
def __init__(self): | |
self.list=[] | |
#get matching item based on key in the hashtable | |
def get(self,key): | |
#loop through all items in the list and find the KeyValue pair with the correct key | |
for item in self.list: | |
if item.key == key: | |
return item.value | |
break | |
return "not found" | |
#add a new KeyValue object into the hashtable | |
def set(self,key,value): | |
keyValue = KeyValue(key,value) | |
self.list.append(keyValue) | |
#testrun | |
hashTable = HashTable() | |
hashTable.set('paijo',123) | |
hashTable.set('kangmas',234) | |
print hashTable.get('paijo') | |
print hashTable.get('samidjan') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment