Skip to content

Commit

Permalink
add + method to SortedArray
Browse files Browse the repository at this point in the history
  • Loading branch information
GarrisonJ committed Apr 29, 2024
1 parent 5541dd8 commit 1f6f64b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
10 changes: 10 additions & 0 deletions lib/sorted_containers/sorted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ def multiply(num)
end
alias * multiply

# Returns a new SortedArray with the values from both arrays.
#
# @param other [SortedArray] The other array to add.
# @return [SortedArray] The combined array.
def +(other)
new_instance = self.class.new
new_instance.update(to_a + other.to_a)
new_instance
end

# rubocop:disable Metrics/MethodLength

# Adds a value to the sorted array.
Expand Down
60 changes: 46 additions & 14 deletions spec/sorted_array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,52 @@
end
end

describe "*" do
it "should multiply the array by the given number" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
array *= 2
expect(array.to_a).to eq([1, 1, 2, 2, 3, 3, 4, 4, 5, 5])
end

it "should multiply the array by the given large number" do
array = SortedContainers::SortedArray.new((1..1000).to_a)
array *= 2
expect(array.to_a).to eq(((1..1000).to_a * 2).sort)
end
end

describe "+" do
it "should concatenate two arrays" do
array1 = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
array2 = SortedContainers::SortedArray.new([6, 7, 8, 9, 10])
expect((array1 + array2).to_a).to eq([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
end

it "should concatenate two arrays with duplicates and sort them" do
array1 = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
array2 = SortedContainers::SortedArray.new([3, 4, 5, 6, 7])
expect((array1 + array2).to_a).to eq([1, 2, 3, 3, 4, 4, 5, 5, 6, 7])
end

it "should concatenate an empty array with a non-empty array" do
array1 = SortedContainers::SortedArray.new
array2 = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
expect((array1 + array2).to_a).to eq([1, 2, 3, 4, 5])
end

it "should concatenate a non-empty array with an empty array" do
array1 = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
array2 = SortedContainers::SortedArray.new
expect((array1 + array2).to_a).to eq([1, 2, 3, 4, 5])
end

it "should concatenate two empty arrays" do
array1 = SortedContainers::SortedArray.new
array2 = SortedContainers::SortedArray.new
expect((array1 + array2).to_a).to eq([])
end
end

describe "add" do
it "sorts items after being added in an arbitrary order" do
array = SortedContainers::SortedArray.new
Expand Down Expand Up @@ -401,20 +447,6 @@
end
end

describe "*" do
it "should multiply the array by the given number" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
array *= 2
expect(array.to_a).to eq([1, 1, 2, 2, 3, 3, 4, 4, 5, 5])
end

it "should multiply the array by the given large number" do
array = SortedContainers::SortedArray.new((1..1000).to_a)
array *= 2
expect(array.to_a).to eq(((1..1000).to_a * 2).sort)
end
end

describe "min" do
it "should return the minimum value in the array" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
Expand Down

0 comments on commit 1f6f64b

Please sign in to comment.