Skip to content

Commit bfd0269

Browse files
committed
start on array list
1 parent 3ea4088 commit bfd0269

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed

ruby/lib/dsa.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require_relative "dsa/deque"
66
require_relative "dsa/binary_tree"
77
require_relative "dsa/trie"
8+
require_relative "dsa/array_list"
89

910
# Data structures and algorithms practice.
1011
module DSA

ruby/lib/dsa/array_list.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
module DSA
2+
# An array list (a growable array).
3+
#
4+
class ArrayList
5+
# @return [Integer]
6+
attr_reader :capacity
7+
8+
# @return [Integer]
9+
attr_reader :length
10+
11+
# @param capacity [Integer]
12+
def initialize capacity: 100
13+
raise ArgumentError, "capacity must be positive" unless capacity.positive?
14+
15+
@capacity = capacity
16+
@length = 0
17+
@array = Array.new(capacity) { nil }
18+
end
19+
20+
# @param index [Integer]
21+
# @return [T]
22+
#
23+
def [] index
24+
raise ArgumentError, "index must be >= 0" if index.negative?
25+
26+
raise ArgumentError, "index out of bounds" if index > @length - 1
27+
28+
@length = index + 1 if index + 1 > @length
29+
30+
@array[index]
31+
end
32+
33+
# @param index [Integer]
34+
# @param item [T]
35+
# @return [T]
36+
#
37+
def []= index, item
38+
raise ArgumentError, "index must be >= 0" if index.negative?
39+
40+
raise ArgumentError, "TODO" if index > @capacity
41+
42+
@length = index + 1 if index + 1 > @length
43+
44+
@array[index] = item
45+
end
46+
47+
# @param item [T]
48+
# @return [T]
49+
#
50+
def append item
51+
@array[@length] = item
52+
@length += 1
53+
item
54+
end
55+
end
56+
end

ruby/spec/dsa/array_list_spec.rb

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
RSpec.describe DSA::ArrayList do
2+
describe "#initialize" do
3+
it "errors if capacity is invalid" do
4+
expect { described_class.new capacity: 0 }
5+
.to raise_error(ArgumentError, "capacity must be positive")
6+
expect { described_class.new capacity: -1 }
7+
.to raise_error(ArgumentError, "capacity must be positive")
8+
end
9+
end
10+
11+
describe "#capacity" do
12+
it "has a default capacity" do
13+
list = described_class.new
14+
expect(list.capacity).to eq 100
15+
end
16+
17+
it "has an intial capacity" do
18+
list = described_class.new capacity: 42
19+
expect(list.capacity).to eq 42
20+
end
21+
end
22+
23+
describe "#length" do
24+
it "returns the number of items in the list" do
25+
list = described_class.new
26+
expect(list.length).to eq 0
27+
28+
list[0] = 42
29+
expect(list.length).to eq 1
30+
end
31+
end
32+
33+
describe "#[]" do
34+
context "when index is invalid" do
35+
it "errors" do
36+
list = described_class.new
37+
expect { list[-1] }.to raise_error(ArgumentError, "index must be >= 0")
38+
end
39+
end
40+
41+
context "when index is less than the length" do
42+
it "returns the item at that index" do
43+
list = described_class.new
44+
list[0] = 42
45+
expect(list[0]).to eq 42
46+
end
47+
end
48+
49+
context "when index is greater than or equal to the length" do
50+
it "errors" do
51+
list = described_class.new
52+
list[0] = 42
53+
expect { list[1] }.to raise_error(ArgumentError, "index out of bounds")
54+
end
55+
end
56+
end
57+
58+
describe "#[]=" do
59+
context "when index is invalid" do
60+
it "errors" do
61+
list = described_class.new
62+
expect { list[-1] = 42 }.to raise_error(ArgumentError, "index must be >= 0")
63+
end
64+
end
65+
66+
context "when index is less than the capacity" do
67+
it "sets the item at that index" do
68+
list = described_class.new
69+
list[0] = 42
70+
expect(list[0]).to eq 42
71+
end
72+
end
73+
74+
context "when index is greater than or equal to the capacity" do
75+
it "resizes the array and sets the item at that index" do
76+
skip "TODO"
77+
end
78+
end
79+
end
80+
81+
describe "#append" do
82+
it "adds an item to the end of the list" do
83+
list = described_class.new
84+
list[0] = 1
85+
list[1] = 2
86+
list.append 42
87+
88+
expect(list[2]).to eq 42
89+
expect(list.length).to eq 3
90+
end
91+
end
92+
end

ruby/spec/dsa_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"LICENSE.txt",
1717
"README.md",
1818
"lib/dsa.rb",
19+
"lib/dsa/array_list.rb",
1920
"lib/dsa/binary_tree.rb",
2021
"lib/dsa/deque.rb",
2122
"lib/dsa/doubly_linked_list.rb",

0 commit comments

Comments
 (0)