Created
September 19, 2016 16:37
-
-
Save AAlvarez90/9dccdc2f66cb7883c6c54516c2c572d4 to your computer and use it in GitHub Desktop.
A method to flatten an array of integers
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 array_flatten(array, result = []) | |
array.each do |element| | |
if element.is_a? Integer | |
result.push element | |
else | |
array_flatten(element, result) | |
end | |
end | |
result | |
end | |
result = array_flatten([[1,11,[20,21]],2,3,[4,5,6],7,[9,10,[45,78]]]) | |
puts result.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment