Created
June 11, 2016 09:17
-
-
Save pshoukry/bc0207e68cfcc80b9b2be2a9f8de37fa 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
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 |
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 '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