Skip to content

Instantly share code, notes, and snippets.

@nisevi
Last active January 2, 2017 16:32
Show Gist options
  • Save nisevi/f79f01b3aff837ea59443b4199ef940a to your computer and use it in GitHub Desktop.
Save nisevi/f79f01b3aff837ea59443b4199ef940a to your computer and use it in GitHub Desktop.
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].
# a = [[1,2,[3]],4]
# aux = []
def flatten(a, aux)
a.each do |n|
if n.instance_of?(Fixnum)
aux<<n
elsif n.instance_of?(Array)
flatten(n, aux)
end
end
aux
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment