Created
January 19, 2016 00:19
-
-
Save jennli/dd0ccbde96368addc2de 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
def flatten_r(array) | |
if array.empty? | |
array | |
elsif array.size == 1 | |
if array[0].is_a? Array | |
flatten_r(array[0]) | |
else | |
array | |
end | |
else | |
if array[0].is_a? Array | |
flatten_r(array[0]) + flatten_r(array[1..-1]) | |
else | |
[array[0]] + flatten_r(array[1..-1]) | |
end | |
end | |
end | |
p flatten_r([1,2,[3,4,5],[6,7,8, [9,10]],[1,2,3],[]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment