Instructions
Create a function split_by_sign that takes an array of numbers
and returns two arrays: one with non-negative numbers (>= 0) and
one with negative numbers.
Use the partition method:
```ruby
[1, -2, 3, -4].partition { |n| n >= 0 }
=> [[1, 3], [-2, -4]]
Return format: `[non_negatives, negatives]`
Hints:
- partition returns [truthy_elements, falsy_elements]
- First array contains elements where block is true
- Second array contains elements where block is false
Your Code
def split_by_sign(numbers)
numbers.partition { |n| n >= 0 }
end
RSpec.describe "split_by_sign" do
it "splits by sign" do
positives, negatives = split_by_sign([1, -2, 3, -4, 0])
expect(positives).to eq([1, 3, 0])
expect(negatives).to eq([-2, -4])
end
it "handles all positive" do
positives, negatives = split_by_sign([1, 2, 3])
expect(positives).to eq([1, 2, 3])
expect(negatives).to eq([])
end
it "handles empty array" do
positives, negatives = split_by_sign([])
expect(positives).to eq([])
expect(negatives).to eq([])
end
end
Results
Click "Run Tests" to see results