Created
May 27, 2011 08:45
-
-
Save gennad/994868 to your computer and use it in GitHub Desktop.
Hashmap implementation
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
class Hash: | |
def __init__(self): | |
self.f = [] | |
self.s = [] | |
def add(self, key, value): | |
found = False | |
for i, val in enumerate(self.f): | |
if val == key: | |
self.s[i] == value | |
found = True | |
if not found: | |
self.f.append(key) | |
self.s.append(value) | |
def del_by_key(self, key): | |
for i, val in enumerate(self.f): | |
if val == key: | |
self.f.pop(i) | |
self.s.pop(i) | |
def show_hashmap(self): | |
print 'start' | |
for i, val in enumerate(self.f): | |
print str(self.f[i]) + ' = ' + str(self.s[i]) | |
print 'end' | |
def get(self, key): | |
for i, val in enumerate(self.f): | |
if val == key: | |
return self.s[i] | |
cl = Hash() | |
cl.add('mykey', 'myval') | |
cl.add('mykey2', 'myval2') | |
cl.show_hashmap() | |
cl.get('mykey2') | |
cl.del_by_key('mykey') | |
cl.show_hashmap() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment