Created
October 13, 2017 16:52
-
-
Save fugufish/5b0d751d2491e732b78fc961f07da243 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
def flatten(array) | |
ret = [] | |
array.each do |i| | |
if i.is_a?(Array) | |
ret += flatten(i) | |
next | |
end | |
ret << i | |
end | |
ret | |
end | |
describe '#flatten' do | |
it 'should flatten a given array' do | |
expect(flatten([[1,2,[3]],4])).to eq([1,2,3,4]) | |
end | |
it 'should flatten the array regardless of the level of nesting' do | |
expect(flatten([[1,2,[3,5,[6,7]]],4])).to eq([1,2,3,5,6,7,4]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment