Created
June 21, 2016 20:33
-
-
Save madisonsites/6d83cd57fe42d464b13d4819e02a7ac6 to your computer and use it in GitHub Desktop.
Flatten Array Play
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
#Write some code, that will flatten an array of arbitrarily nested arrays of integers into a flat array of integers. | |
#e.g. [[1,2,[3]],4] -> [1,2,3,4]. | |
#Try to avoid using language defined methods like Ruby's Array#flatten.* | |
# This feels like it can be solved with recursion, so let's try | |
#Okay, I'm not sure what's going on, but I'm starving, so I'm going to pause this for now | |
#I don't want to leave my application sitting in limbo and risk losing everything i already answered if the page re-loads | |
#so I'll 'submit' for now | |
@flattened_array = [] | |
def flatten_without_flattened(array_of_arrays) | |
array_of_arrays.each do |inner_array| | |
if inner_array.is_a?(Integer) | |
@flattened_array.push(inner_array) | |
elsif inner_array.is_a?(Array) | |
flatten_without_flattened(inner_array) | |
end | |
end | |
@flattened_array | |
end | |
flatten_without_flattened([[1,2,[3]],4]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment