Skip to content

Instantly share code, notes, and snippets.

@femmerling
Last active September 22, 2017 14:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save femmerling/4364067 to your computer and use it in GitHub Desktop.
Save femmerling/4364067 to your computer and use it in GitHub Desktop.
Hashtable algorithm written in python
#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