My hash codes returns only the whole title of the word. I want to make it to show the results with only using keywords for at least 2 word (onwards) then show the results (get function).
My hash code
class hashin:
def __init__(self):
self.size = 217 # size of hash table
self.map = [None] * self.size
def _get_hash(self, key):
hash = 0
for char in str(key):
hash += ord(char)
return hash % self.size
#returns the ASCII value of char in str(key)
def add(self, key, value): # add item to list
key_hash = self._get_hash(key)
key_value = [key, value]
if self.map[key_hash] is None:
self.map[key_hash] = list([key_value])
return True
else:
for pair in self.map[key_hash]:
if pair[0] == key:
pair[1] = value
return True
self.map[key_hash].append(key_value)
return True
def get(self, key): # search for item
key_hash = self._get_hash(key)
if self.map[key_hash] is not None:
for pair in self.map[key_hash]: # find pair of words
if pair[0] == key: # if pair is equals to the whole title of the word
return pair[0] + " - " + pair[1]
return "Error no results for %s \nEnter the correct word." % (key)
sample outputs:
when whole title was typed
When keyword was typed (i need to show the results even when keyword was typed)
What i need is : Output: Cheater - Kygos and the other words with chea in their name


A hash table isn't the right data structure for this task. The purpose of a hash value is to narrow the search to a small subset of the possibilities. Since the hash value is dependent on the entire string, using just a portion of the string will give the wrong subset.
A better data structure for this task is a trie (sometimes called a "prefix tree"). While it is not difficult to write this data structure on your own, there are already many tested, ready-to-use modules already available on PyPI.
See: https://pypi.python.org/pypi?%3Aaction=search&term=trie&submit=search