Created
March 3, 2018 14:45
-
-
Save rubyisbeautiful/4f4d1ff124ebb5e2f52f62c0e9bbb7d4 to your computer and use it in GitHub Desktop.
arbitrarily nested array of integers flattener
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
# frozen_string_literal: true | |
# Will flatten an arbitrary nested array of Integer | |
# | |
# NB: I am not very happy with this solution. | |
# I preferred to do it in either Scala or Elm. | |
# I'm out of practice in Scala and still learning Elm. | |
# I did try to keep this "functinal thinking" | |
# to have a deterministic function and | |
# eliminate any side effects. | |
# | |
# Usage: Flattener.flattenize(ary) | |
# | |
# @param [Array<Integer,Array>] array to flatten | |
# | |
# @return [Array<Integer>] flattened array | |
# | |
# @raise IllegalArgumentError when Array contains a non-Integer | |
# | |
# @example | |
# puts "Flattening []" | |
# puts Flattener.flattenize([]).to_s | |
# | |
# @example | |
# puts "Flattening [1, [2]" | |
# puts Flattener.flattenize([1,[2]]).to_s | |
# | |
# @example | |
# puts "Flattening [1,2,3,4]" | |
# puts Flattener.flattenize([1,2,3,4]).to_s | |
# | |
# @example | |
# puts "Flattening [[1,2,[3]],4]" | |
# puts Flattener.flattenize([[1,2,[3]],4]).to_s | |
# | |
# @example | |
# begin | |
# puts "Flattening [[1,2,[3]],'c']" | |
# puts Flattener.flattenize([[1,2,[3]],'c']).to_s | |
# rescue Flattener::IllegalArgumentError | |
# puts 'This one raised because of a non-Integer argument' | |
# end | |
module Flattener | |
class IllegalArgumentError < StandardError; end | |
def self.flattenize(ary) | |
head = ary[0] | |
rest = ary.drop 1 | |
puts "ary: #{ary}" if $DEBUG | |
puts "head: #{head}" if $DEBUG | |
puts "rest: #{rest}" if $DEBUG | |
if ary.empty? | |
ary | |
else | |
case head | |
when Integer | |
if rest.empty? | |
rest.unshift(head) | |
else | |
Flattener.flattenize(rest).unshift(head) | |
end | |
when Array | |
Flattener.flattenize(head) + Flattener.flattenize(rest) | |
else | |
raise IllegalArgumentError, 'Array must contain only Integer or Array' | |
end | |
end | |
end | |
end |
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
require "minitest/autorun" | |
require 'minitest/rg' | |
require 'flattener' | |
class TestFlattener < Minitest::Test | |
def setup | |
end | |
def test_that_empty_works | |
assert_equal [], Flattener.flattenize([]) | |
end | |
def test_that_simple_works | |
assert_equal [1,2,3,4], Flattener.flattenize([1,2,3,4]) | |
end | |
def test_that_nested_works | |
assert_equal [1,2,3,4], Flattener.flattenize([[1,2,[3]],4]) | |
end | |
def test_that_non_ints_raise | |
assert_raises(Flattener::IllegalArgumentError){ Flattener.flattenize([[1,2,[3]],'c']) } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment