Skip to content

Commit a64e70f

Browse files
committed
hash part 2
1 parent ac3ffad commit a64e70f

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

hash_part_2/node.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Node
2+
attr_accessor :next
3+
attr_accessor :key
4+
attr_accessor :value
5+
6+
def initialize(key, value)
7+
end
8+
end

hash_part_2/open_addressing.rb

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require_relative 'node'
2+
3+
class OpenAddressing
4+
def initialize(size)
5+
end
6+
7+
def []=(key, value)
8+
end
9+
10+
def [](key)
11+
end
12+
13+
# Returns a unique, deterministically reproducible index into an array
14+
# We are hashing based on strings, let's use the ascii value of each string as
15+
# a starting point.
16+
def index(key, size)
17+
end
18+
19+
# Given an index, find the next open index in @items
20+
def next_open_index(index)
21+
end
22+
23+
# Simple method to return the number of items in the hash
24+
def size
25+
end
26+
27+
# Resize the hash
28+
def resize
29+
end
30+
end

hash_part_2/open_addressing_spec.rb

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
include RSpec
2+
3+
require_relative 'node'
4+
require_relative 'open_addressing'
5+
6+
RSpec.describe OpenAddressing, type: Class do
7+
let(:star_wars_movies) { OpenAddressing.new(6) }
8+
9+
before do
10+
star_wars_movies["Star Wars: The Phantom Menace"] = "Number One"
11+
star_wars_movies["Star Wars: Attack of the Clones"] = "Number Two"
12+
star_wars_movies["Star Wars: Revenge of the Sith"] = "Number Three"
13+
star_wars_movies["Star Wars: A New Hope"] = "Number Four"
14+
star_wars_movies["Star Wars: The Empire Strikes Back"] = "Number Five"
15+
star_wars_movies["Star Wars: Return of the Jedi"] = "Number Six"
16+
end
17+
18+
describe "#index" do
19+
it "creates a hash key based on the string value passed in" do
20+
i = star_wars_movies.index("Star Wars: A New Hope", 6)
21+
expect(i).to eq 4
22+
end
23+
end
24+
25+
describe "#key" do
26+
it "returns the sum of the ascii values of the string value" do
27+
key = "test"
28+
expect(star_wars_movies.index(key, 6)).to eq 4
29+
end
30+
end
31+
32+
describe "#hash[key] = value" do
33+
it "does not resizes the array when a collision occurs and hash is not full" do
34+
hash = OpenAddressing.new(4)
35+
hash["key"] = "value"
36+
expect(hash.size).to eq 4
37+
hash["key"] = "second value"
38+
expect(hash.size).to eq 4
39+
end
40+
41+
it "resizes the array when a collision occurs and hash is full" do
42+
hash = OpenAddressing.new(1)
43+
hash["key"] = "value"
44+
expect(hash.size).to eq 1
45+
hash["key"] = "second value"
46+
expect(hash.size).to eq 2
47+
end
48+
49+
it "sets the value of key to value" do
50+
expect(star_wars_movies["Star Wars: The Phantom Menace"]).to eq "Number One"
51+
expect(star_wars_movies["Star Wars: Attack of the Clones"]).to eq "Number Two"
52+
expect(star_wars_movies["Star Wars: Revenge of the Sith"]).to eq "Number Three"
53+
expect(star_wars_movies["Star Wars: A New Hope"]).to eq "Number Four"
54+
expect(star_wars_movies["Star Wars: The Empire Strikes Back"]).to eq "Number Five"
55+
expect(star_wars_movies["Star Wars: Return of the Jedi"]).to eq "Number Six"
56+
end
57+
end
58+
59+
describe "#next_open_index" do
60+
it "returns -1 if there are no open indices" do
61+
inception = OpenAddressing.new(1)
62+
inception["The Original"] = "The Best Movie Ever"
63+
expect(inception.next_open_index(0)).to eq -1
64+
end
65+
66+
end
67+
68+
describe "#resize" do
69+
it "doubles the size of the array when invoked" do
70+
movies = OpenAddressing.new(6)
71+
expect(movies.size).to eq 6
72+
movies.resize
73+
expect(movies.size).to eq 12
74+
end
75+
76+
it "copies existing values properly when the array is resized" do
77+
movies = OpenAddressing.new(6)
78+
movies["A New Hope"] = "Average"
79+
movies["Empire Strikes Back"] = "Excellent"
80+
movies["Return of the Jedi"] = "The Best"
81+
movies.resize
82+
expect(movies.size).to eq 12
83+
expect(movies["A New Hope"]).to eq "Average"
84+
expect(movies["Empire Strikes Back"]).to eq "Excellent"
85+
expect(movies["Return of the Jedi"]).to eq "The Best"
86+
end
87+
end
88+
end

0 commit comments

Comments
 (0)