4
\$\begingroup\$

A hashmap is a data structure that implements an associative array abstract data type using keys and values and has a hash function to compute an index into an array, from which the desired value can be set and get [reference].


Based on some tutorial, I wrote this hashmap class:

class HashMap(): def __init__(self): self.hahsmap_size = 32 self.hashmap_data = [None] * self.hahsmap_size def __get_hash_mod_size(self, key): hash_key_var = hash(key+str(self.hahsmap_size*0.01)) return hash_key_var % self.hahsmap_size def set_key_value(self, key, value): key_var = self.__get_hash_mod_size(key) key_value_list = [key, value] if self.hashmap_data[key_var] is None: self.hashmap_data[key_var] = list([key_value_list]) return True else: for pair in self.hashmap_data[key_var]: print(pair) if pair[0] == key: pair[1] = value return True self.hashmap_data[key_var].append(key_value_list) return True def get_key(self, key): key_var = self.__get_hash_mod_size(key) if self.hashmap_data[key_var] is not None: for pair in self.hashmap_data[key_var]: if pair[0] == key: return pair[1] return None def remove_key(self, key): key_var = self.__get_hash_mod_size(key) if self.hashmap_data[key_var] is not None: return False for i in range(len(self.hashmap_data[key_var])): if self.hashmap_data[key_var][i][0] == key: self.hashmap_data[key_var].pop(i) return True def print_hashmap(self): for item in self.hashmap_data: if item is not None: print(item) hm = HashMap() hm.set_key_value('A', '1') hm.set_key_value('A', '2') hm.set_key_value('B', '1') hm.set_key_value('A', '3') hm.set_key_value('A', '4') hm.set_key_value('C', '1') hm.set_key_value('D', '1') hm.set_key_value('E', '1') hm.set_key_value('E', '2') hm.remove_key('A') hm.remove_key('B') hm.remove_key('B') hm.print_hashmap() 

If you had time, I'd appreciate a review.

\$\endgroup\$
1
  • \$\begingroup\$I am not seeing it in this code, but how do you handle collisions and how do you get your hash value?\$\endgroup\$CommentedOct 16, 2021 at 16:04

1 Answer 1

2
\$\begingroup\$

The first thing that stands out here to me is that you should be using __get_item__ and __set_item__, and __repr__ instead of get-key, set-key-value and print_hashmap. This will opt you into the syntax of collections in python where you use d[key] to get a key, d[key] = val to set a value, and print(d) to print it. This may seem trivial, but the fact that python lets containers you have written to feel as nice as base ones is a large part of why Python is such a natural feeling language as opposed to something like Java, where this style would be considered correct.

\$\endgroup\$
4
  • \$\begingroup\$Acuatlly Python dictionaries serve as hash maps but the challenge i think is deriving it!\$\endgroup\$CommentedAug 7, 2019 at 19:15
  • 1
    \$\begingroup\$huh? None of what I said is keeping op from deriving it, I'm just showing how op can derive it such that it is nice to use after.\$\endgroup\$CommentedAug 7, 2019 at 19:18
  • \$\begingroup\$Costs nothing to actually write some illustrative codes in your answer ^^_\$\endgroup\$CommentedAug 7, 2019 at 19:20
  • 2
    \$\begingroup\$my point with this answer is that simply changing the names of the methods op has implemented would be a significant improvement. No other new code needed.\$\endgroup\$CommentedAug 7, 2019 at 19:23

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.