Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.68 KB | None | 0 0
  1. class HashMap:
  2.     """HashMap class"""
  3.  
  4.     def __init__(self, size):
  5.         self.size = size
  6.         self.map = [[] for x in range(size)]
  7.  
  8.     def hashkey(self, key):
  9.         """interne hashfunction uses the python hashfunction
  10.  
  11.        args: the key that needs hashing as string
  12.  
  13.        return: the hashvalue
  14.  
  15.        examples: there are no examples because the hash function is random
  16.        each time...
  17.  
  18.        """
  19.         return hash(key) % self.size
  20.  
  21.     def insert (self, key, value):
  22.         """Insert the given key (given as a string) with the given value (given as
  23.        an integer)
  24.  
  25.        args: key as string, and value as int
  26.  
  27.        return : not return  value, it just updates the HashMap with the new value at location key
  28.        
  29.        examples:
  30.        >>> h = HashMap(6)
  31.        >>> h.insert('foo', 69)
  32.  
  33.        """
  34.  
  35.         if (type(key)!= str or type(value) != int):
  36.             raise TypeError ('type value hat to be int and of key str')
  37.         else:
  38.             hash_key = self.hashkey(key)
  39.             if self.map[hash_key]:
  40.                 sp = False
  41.                 for i, pair in enumerate(self.map[hash_key]):
  42.                     if(pair[0]==key):
  43.                         self.map[hash_key][i] = (key, value)
  44.                         sp = False
  45.                         break
  46.                 if not sp:
  47.                     self.map[hash_key].append((key, value))
  48.  
  49.             else:
  50.                 self.map[hash_key].append((key, value))
  51.        
  52.     def lookup(self, key):
  53.         """ lookup method for the objects of the class HashTable
  54.  
  55.        args: key given as string
  56.  
  57.        return: value of the given key as int or 0 if key not in self.HT
  58.  
  59.        examples:
  60.        »> h = HashTable(5)
  61.        »> h.insert('name', 69)
  62.        »> h.lookup('name')
  63.        69
  64.        »> h.lookup("aaa")
  65.        0
  66.        """
  67.         hash_key = self.hashkey(key)
  68.         for elem in self.map[hash_key]:
  69.             if elem[0] == key:
  70.                 return elem[1]
  71.         return 0
  72.  
  73.  
  74.  
  75. if __name__=="__main__":
  76.  
  77.     def test_hashMap():
  78.         # just a little test function...doctstrings are ok but I preffer test
  79.         # functions when it comes to objects...
  80.  
  81.  
  82.         HashMap(5).insert("name", 2)
  83.         HashMap(5).insert("NAME", 2)
  84.         HashMap(5).insert("etc", 5)
  85.         HashMap(5).insert("qdmhs", 21)
  86.         HashMap(5).insert("ddd", 1)
  87.         HashMap(5).insert("mbnd", 10)
  88.         assert HashMap(5).lookup("A.Merkel") == 0
  89.         assert HashMap(5).lookup("qdmhs") == 21
  90.         assert HashMap(5).lookup("name") == 2
  91.         Hash.Map(5).insert("name", 69)
  92.         assert HashMap(5).lookup("name") == 69
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement