Skip to content

Instantly share code, notes, and snippets.

@mahnouel
Created March 9, 2018 13:17
Show Gist options
  • Save mahnouel/b3436e15d33de2d2393b86a38f53c4de to your computer and use it in GitHub Desktop.
Save mahnouel/b3436e15d33de2d2393b86a38f53c4de to your computer and use it in GitHub Desktop.
a searchable hash list
class HashList:
list = {}
def add(self, string):
key = self.get_hash(string)
while key in self.list:
key+=1
self.list[key] = string
def get_hash(self, string):
result = 0
string = string.lower()
for letter in string:
value = ord(letter) - 96
result += value
result *= 100 * len(string)
result %= 280000
return result
def search(self, string):
key = self.get_hash(string)
while key in self.list:
if self.list[key] == string:
return key
key+=1
return -1
hl = HashList()
hl.add('Bach')
hl.add('Goethe')
hl.add('Klitt')
hl.add('Schiller')
hl.add('Shakrespeare')
hl.add('Droste-Hülshoff')
hl.add('Aristoteles')
print(hl.list)
result = hl.search('Klitt')
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment