Skip to content

Instantly share code, notes, and snippets.

@rdormer
Created June 24, 2016 15:10
Show Gist options
  • Save rdormer/0d63b2dc83a7d9a8dce49f38db4b0855 to your computer and use it in GitHub Desktop.
Save rdormer/0d63b2dc83a7d9a8dce49f38db4b0855 to your computer and use it in GitHub Desktop.
Codebytes screening question
# 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