Skip to content

Instantly share code, notes, and snippets.

@madis
Created October 16, 2016 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madis/849b436e3fe24c2a06bd52fde4a3afe5 to your computer and use it in GitHub Desktop.
Save madis/849b436e3fe24c2a06bd52fde4a3afe5 to your computer and use it in GitHub Desktop.
Flatland
def flatten(input, output=[])
input.each do |e|
if e.is_a? Array
output += flatten(e)
else
output.push e
end
end
output
end
require_relative 'flatten'
describe 'flatten' do
subject { flatten(input) }
context 'empty array' do
let(:input) { [] }
it { should eq [] }
end
context 'flat with multiple elements' do
let(:input) { [1, 2] }
it { should eq [1, 2] }
end
context 'nested arrays 1 level' do
let(:input) { [[1], [2]] }
it { should eq [1, 2] }
end
context 'mixed nested 1 level' do
let(:input) { [[1], 2, [3]] }
it { should eq [1, 2, 3] }
end
context 'multi level nesting' do
let(:input) { [1, [2], [[3]]] }
it { should eq [1, 2, 3] }
end
context 'task data' do
let(:input) { [[1, 2, [3]], 4] }
it { should eq [1, 2, 3, 4] }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment