Created
June 24, 2016 15:10
-
-
Save rdormer/0d63b2dc83a7d9a8dce49f38db4b0855 to your computer and use it in GitHub Desktop.
Codebytes screening question
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
# This is a re-implementation of the flatten method for arrays. | |
# Accepts any number of arbitrarily nested arrays and non-array | |
# values, flattening them all into a single un-nested array. The | |
# output of elements in the array is dependent on the order of input | |
# elements. | |
def flatten(*arrs) | |
flattened = [] | |
#outer loop for splat arguments | |
arrs.each do |arr| | |
#handle non array values | |
unless arr.is_a? Array | |
flattened << arr | |
next | |
end | |
#handle array values | |
arr.each do |current| | |
rval = flatten(current) | |
rval.each {|x| flattened << x} | |
end | |
end | |
flattened | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment