Skip to content

Instantly share code, notes, and snippets.

@sgeef
Last active July 10, 2019 15:02
Show Gist options
  • Save sgeef/570dee21fc1407dd9f6f09d484bfdb15 to your computer and use it in GitHub Desktop.
Save sgeef/570dee21fc1407dd9f6f09d484bfdb15 to your computer and use it in GitHub Desktop.
Interview assignment 10/07/19 (#flatten)

Interview task made by Stephan Teeuwen

build flatten without using Array#flatten example: [[1,2,[3]],4] to [1,2,3,4]

Testing:

Run following command to test the functioning of the extension method on Array rspec ./flatten_spec.rb --format documentation

Implementation of flatten assignment:

As commented as well in the code, i used the most basic methods i can think of to avoid #each, #flatten and others and make it as plain ruby as possible. Basically what we do is making an recursive loop, to make an infinitly deeply nested array even flattable, it would be the developers responsibility to use this carefully as for sure it's not the optimal way to work with to large array's as this would require memory usage and speed usage testing.

Usage of the new flatten method

We extended the Array class as the method can practically be used on any array and has been tested to work with flat and non flat array's. Besides this there would be a need to require 'array_extensions' if not auto loader is used.

After requiring the right file we can now use the awsome interview_flattener method by calling it on any array we've created. example: [1,2,[3,4]].interview_flattener

# If we build something like a flattener we would build it as an extension of an array, as it would always be used on an array and this way it is a re-usable method. Offcourse depending on the usecase we would need to decide if we make it so dynamic and re-usable.
# By building it this way or in an seperate class we also make it unit-testable
class Array
# create the extension method
def interview_flattener
# instantiate return value
retval = []
# prefferably in such case offcourse we use #flatten or if needed without flatten we loop with #each, but for the sake of the assignment we'll go basic ruby here to avoid build in methods as request, as much as possible
for item in self
# Decide if we need to go a level deeper or not.
if item.kind_of?(Array)
# Loop trough the deeper array recursively (Note we use += to merge arrays)
retval += item.interview_flattener()
else
# if we get a single value add it straight to the retval variable
retval << item
end
end
return retval
end
end
require 'array_extensions'
describe Array do
# Basic nested test compared to static result
it 'it flattens an nested array with Array#interview_flattener' do
# Make a test array to test this with
nested_array = [1,[2,3],[4,[5,[6]]],7]
result = nested_array.interview_flattener()
expect(result).to eq [1,2,3,4,5,6,7]
end
# basic nested test, to compare #flatten output to our method
it 'it flattens an nested array the same way as Array#flatten with Array#interview_flattener' do
# Make a test array to test this with
nested_array = [1,[2,3],[4,[5,[6]]],7]
result = nested_array.interview_flattener()
ruby_method_result = nested_array.flatten()
expect(result).to eq ruby_method_result
end
# basic test to test non flatten array's will also output correctly
it 'it returns an non-nested array with Array#interview_flattener correctly' do
# Make a test array to test this with
nested_array = [1,2,3,4,5,6,7]
result = nested_array.interview_flattener()
expect(result).to eq nested_array
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment