Skip to content

Instantly share code, notes, and snippets.

@BigGillyStyle
Created September 17, 2016 14:30
Show Gist options
  • Save BigGillyStyle/8b6ee4263be41f68c8fe6bc641792fd3 to your computer and use it in GitHub Desktop.
Save BigGillyStyle/8b6ee4263be41f68c8fe6bc641792fd3 to your computer and use it in GitHub Desktop.
# I tried to avoid as many Ruby-isms as possible for this assignment, including
# Ruby's unique Array methods and Ruby's functional concepts...as I thought
# this got to the "heart" of the assignment. I figure *most* languages
# support basic iteration and recursion and some method of adding an element
# to an array. The one possible outlier here is using Ruby's splat operator.
# I'm not sure if this violates the intent of the assignment or not. Also,
# since Minitest was removed from Ruby core, I just did some more basic tests.
#
# Additionally, the assignment mentioned flattening an array of integers, so
# I did not include a bunch of nil and type checks for input.
def andy_flatten(arr)
flattened_array = []
for i in 0..(arr.length - 1)
element = arr[i]
if element.is_a?(Array)
flattened_array.push(*andy_flatten(element))
else
flattened_array.push(element)
end
end
flattened_array
end
def assert_equal(expected, actual)
raise "Expected: #{expected} does not == Actual: #{actual}" unless expected == actual
end
assert_equal andy_flatten([1]), [1]
assert_equal andy_flatten([1,2,3]), [1,2,3]
assert_equal andy_flatten([1,2,[3,4]]), [1,2,3,4]
assert_equal andy_flatten([1,2,[3,[4,5]]]), [1,2,3,4,5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment