From cff7f221591d0d8b5dbcf0b4df00f035fe2bffeb Mon Sep 17 00:00:00 2001 From: Edward Choi Date: Tue, 29 May 2018 16:48:07 -0700 Subject: [PATCH 1/2] SEP Hashes part 1 tests all pass --- .../04-hashes-part-1/hash_item.rb | 4 +++- .../04-hashes-part-1/hashclass.rb | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/01-data-structures/04-hashes-part-1/hash_item.rb b/01-data-structures/04-hashes-part-1/hash_item.rb index 4a420212..e0b9e30c 100644 --- a/01-data-structures/04-hashes-part-1/hash_item.rb +++ b/01-data-structures/04-hashes-part-1/hash_item.rb @@ -1,7 +1,9 @@ class HashItem attr_accessor :key attr_accessor :value - + def initialize(key, value) + @key = key + @value = value end end \ No newline at end of file diff --git a/01-data-structures/04-hashes-part-1/hashclass.rb b/01-data-structures/04-hashes-part-1/hashclass.rb index e538428a..74d2a671 100644 --- a/01-data-structures/04-hashes-part-1/hashclass.rb +++ b/01-data-structures/04-hashes-part-1/hashclass.rb @@ -1,3 +1,5 @@ +require_relative 'hash_item' + class HashClass def initialize(size) @@ -5,23 +7,41 @@ def initialize(size) end def []=(key, value) + item = HashItem.new(key, value) + i = index(key, size) + + if @items[i].nil? + @items[i] = item + elsif @items[i].key != item.key + self.resize + self[key] = value + elsif @items[i].value != item.value + self.resize + @items[index(item.key, size)] = value + end end def [](key) + @items[self.index(key, self.size)].value end def resize + old_items = @items.compact + @items = Array.new(self.size * 2) + old_items.each { |item| self[item.key] = item.value } end # Returns a unique, deterministically reproducible index into an array # We are hashing based on strings, let's use the ascii value of each string as # a starting point. def index(key, size) + key.each_byte.sum % size end # Simple method to return the number of items in the hash def size + @items.length end end \ No newline at end of file From 956112744acde42b56fab9b66a626a78eff92b82 Mon Sep 17 00:00:00 2001 From: Edward Choi Date: Tue, 29 May 2018 16:57:31 -0700 Subject: [PATCH 2/2] added answer --- 01-data-structures/04-hashes-part-1/hash_item.rb | 3 +-- 01-data-structures/04-hashes-part-1/hashes-1-answers.txt | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/01-data-structures/04-hashes-part-1/hash_item.rb b/01-data-structures/04-hashes-part-1/hash_item.rb index e0b9e30c..bec784f0 100644 --- a/01-data-structures/04-hashes-part-1/hash_item.rb +++ b/01-data-structures/04-hashes-part-1/hash_item.rb @@ -3,7 +3,6 @@ class HashItem attr_accessor :value def initialize(key, value) - @key = key - @value = value + @key, @value = key, value end end \ No newline at end of file diff --git a/01-data-structures/04-hashes-part-1/hashes-1-answers.txt b/01-data-structures/04-hashes-part-1/hashes-1-answers.txt index e69de29b..07ab6a1d 100644 --- a/01-data-structures/04-hashes-part-1/hashes-1-answers.txt +++ b/01-data-structures/04-hashes-part-1/hashes-1-answers.txt @@ -0,0 +1 @@ +Doubling the size of the underlying array of my HashClass may be a poor idea because it'll increase the open slots in the array exponentially which may lead to more fragmentation. Doubling the size is also a slow process. Using prime numbers to resize is better as that'll reduce the number of collisions. \ No newline at end of file