Last active
May 13, 2016 11:34
-
-
Save EdDeAlmeidaJr/542b518a61fe0601c396943a28c50533 to your computer and use it in GitHub Desktop.
Flatten an array without using Array#flatten
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
def ints_only?(arr) | |
res = true | |
arr.map { |item| res = res && item.is_a?(Integer) ? true : false } | |
res | |
end | |
def array_flatten(arr) | |
while !ints_only?(arr) do | |
new_arr = Array.new | |
arr.map{ |item| | |
if item.is_a?(Integer) then | |
new_arr.push(item) | |
else | |
item.map{ |sitem| new_arr.push(sitem) } | |
end | |
arr = new_arr | |
} | |
end | |
arr | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment