Created
August 9, 2016 13:10
-
-
Save pocha/61e2f05a1df91348a230508ac7023b97 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(a) | |
out = [] | |
if !a.kind_of?(Array) | |
puts "not an array" | |
return -1 | |
end | |
a.each do |val| | |
out += _flatten(val) | |
end | |
out | |
end | |
def _flatten(val) | |
out = [] | |
if val.kind_of?(Array) | |
val.each do |_val| | |
out += _flatten(_val) | |
end | |
else | |
out << val | |
end | |
out | |
end | |
#a = [1,2,[[3],4]] | |
a = "pocha" | |
puts flatten(a).inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment