#UPDATE def hashtable_update(htable,key,value): bucket = hashtable_get_bucket(htable,key) for entry in bucket: if entry[0] == key: entry[1] = value return htable bucket.append([key,value]) return htable def hashtable_lookup(htable,key): bucket = hashtable_get_bucket(htable,key) for entry in bucket: if entry[0] == key: return entry[1] return None def hashtable_add(htable,key,value): bucket = hashtable_get_bucket(htable,key) bucket.append([key,value]) def hashtable_get_bucket(htable,keyword): return htable[hash_string(keyword,len(htable))] def hash_string(keyword,buckets): out = 0 for s in keyword: out = (out + ord(s)) % buckets return out #ALTERNATIVELY def hash_string(keyword,buckets): index = 0 hash_value = [] while index < len(keyword): for e in keyword: hash_value.append(ord(e)) index = index +1 return sum(hash_value) % buckets #END of alternative code def make_hashtable(nbuckets): table = [] for unused in range(0,nbuckets): table.append([]) return table table = [[['Ellis', 11], ['Francis', 13]], [], [['Bill', 17], ['Zoe', 14]], [['Coach', 4]], [['Louis', 29], ['Nick', 2], ['Rochelle', 4]]] hashtable_update(table, 'Bill', 42) hashtable_update(table, 'Rochelle', 94) hashtable_update(table, 'Zed', 68) print table #>>> [[['Ellis', 11], ['Francis', 13]], [['Zed', 68]], [['Bill', 42], #>>> ['Zoe', 14]], [['Coach', 4]], [['Louis', 29], ['Nick', 2], #>>> ['Rochelle', 94]]]
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question