Skip to content

Instantly share code, notes, and snippets.

@pshoukry
Created June 11, 2016 09:17
Show Gist options
  • Save pshoukry/bc0207e68cfcc80b9b2be2a9f8de37fa to your computer and use it in GitHub Desktop.
Save pshoukry/bc0207e68cfcc80b9b2be2a9f8de37fa to your computer and use it in GitHub Desktop.
class CB
def self.flatten(arr)
flat_array = []
arr.each do |e|
if e.is_a?(Array)
CB.flatten(e).each {|se| flat_array << se }
else
flat_array << e
end
end
flat_array
end
end
require 'minitest/autorun'
require_relative 'flatten'
describe CB do
describe "#flatten" do
it "should flatten an array of arbitary nested arrays" do
CB.flatten([]).must_equal []
CB.flatten([1, 2, 3]).must_equal [1, 2, 3]
CB.flatten([1, [1,2], 3]).must_equal [1,1, 2, 3]
CB.flatten([1, [1, [1, 2]], 3]).must_equal [1, 1, 1, 2, 3]
CB.flatten([1, [[5, [6, [7, [8, 9]]]],2],3]).must_equal [1, 5, 6, 7, 8, 9, 2, 3]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment