Last active
December 7, 2018 18:18
-
-
Save ChrisDrit/d5ee5243a3d1d13648e88453a0e534a0 to your computer and use it in GitHub Desktop.
Example code
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
# ~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~ | |
# | |
# ~= Coding Challenge =~ | |
# | |
# The following coding challenge was presented to me: | |
# | |
# 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]. | |
# | |
# Your solution should be a link to a gist on gist.github.com with | |
# your implementation. | |
# | |
# When writing this code, you can use any language you're comfortable | |
# with. The code must be well tested and documented if necessary, | |
# and in general please treat the quality of the code as if it was | |
# ready to ship to production. | |
# | |
# Try to avoid using language defined methods like | |
# Ruby's Array#flatten.* | |
# | |
# Below is my solution: | |
# | |
# ~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~ | |
# ----------------------------------------------------------------------- | |
# Count Class | |
# | |
# HACK: Do NOT use in production. | |
# | |
# What's really being asked is to show you that I know how to | |
# use the 'splat' operator and a recursive function. Of course, | |
# one would NEVER hand code a slow routine like this in | |
# Ruby when the core lib has done it for you with the SUPER speady | |
# C language ;) | |
# ----------------------------------------------------------------------- | |
class Count | |
# Accepts a nested Array and returns a flattened Array | |
# | |
# [[1,2,3,4,5,6,7,8,9,0]] => [1,2,3,4,5,6,7,8,9,0] | |
# | |
# Uses the 'splat' operator (*) to unbundle each nested array | |
# and pass each unbundled array recursively back to itself. | |
# Continues until there are no more nested arrays. | |
# | |
# [[1,[2],3,[[4,5]],6,7,8,9,0]] => [1,2,3,4,5,6,7,8,9,0] | |
# | |
# Does not convert String elements to Integers and no | |
# exception is raised if a nested string is found. | |
# | |
# [1,2,[3,4],5,6,7,8,"9",0] => [1,2,3,4,5,6,7,8,"9",0] | |
# | |
def flatterizer(array=nil) | |
is_array?(array) | |
array.each_with_object([]).each do |item, flattened| | |
flattened.push *(item.is_a?(Array)) ? flatterizer(item) : item | |
end | |
end | |
# Raises exception if not an <code>Array</code> | |
# Returns <code>true</code> if <code>Array</code> | |
def is_array?(obj) | |
if obj.is_a? Array | |
return true | |
else | |
raise "Array arguments only!" | |
end | |
end | |
end | |
# ---------------------------------------------------------------------- | |
# Rspec Tests | |
# count_spec.rb | |
# ---------------------------------------------------------------------- | |
require "spec_helper" | |
describe Count do | |
let(:array_flat) {[1,2,3,4,5,6,7,8,9,0]} | |
let(:array_nested) {[1,[2],3,[4,5,6,7],8,9,0]} | |
let(:array_deeply_nested) {[[1,[[2,3]],4,[[[5,6,]]],7,8,9,0]]} | |
describe "flatterizer" do | |
it "accepts a flat array of integers" do | |
expect(described_class.new.flatterizer(array_flat)).to eq array_flat | |
end | |
it "accepts a nested array of integers" do | |
expect(described_class.new.flatterizer(array_nested)).to eq array_flat | |
end | |
it "succe accepts a deeply nested array" do | |
expect(described_class.new.flatterizer(array_deeply_nested)).to eq array_flat | |
end | |
it "raises exception 'Array arguments only!' when not an array" do | |
expect(described_class.new.flatterizer(array_flat.to_s)).to raise_exception('Array arguments only!') | |
end | |
end | |
describe "is_array?" do | |
it "returns true when flat array passed" do | |
expect(described_class.new.flatterizer(array_flat)).to be_truthy | |
end | |
it 'return true when nested array passed' do | |
expect(described_class.new.flatterizer(array_nested)).to be_truthy | |
end | |
it "raises exception 'Array arguments only!' when not an array" do | |
expect(described_class.new.flatterizer(array_flat.to_s)).to raise_exception('Array arguments only!') | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment