Skip to content

Instantly share code, notes, and snippets.

@sapereau83
Last active March 10, 2020 20:02
Show Gist options
  • Select an option

  • Save sapereau83/2017c5ef4015169ef5d8 to your computer and use it in GitHub Desktop.

Select an option

Save sapereau83/2017c5ef4015169ef5d8 to your computer and use it in GitHub Desktop.
A not efficient but easy-to-code implementation of Array#flatten
# Flat the array layer by layer
#
# Insights:
#
# - Using recursion.
#
class Array
def my_flatten(depth = nil)
if depth.nil? || depth.is_a?(Fixnum)
return flat_all if not depth
return flat_by_depth(depth)
else
raise(ArgumentError, ":depth should be a Fixnum")
end
end
private
def flat_all
new_ary = self.dup
contains_array = false
tmp_ary = []
loop do
contains_array = false
tmp_ary = []
new_ary.each do |elem|
if elem.is_a? Array
contains_array = true if elem.is_a? Array
elem.each { |sub_elem| tmp_ary << sub_elem }
else
tmp_ary << elem
end
end
new_ary = tmp_ary
break if not contains_array
end
new_ary
end
def flat_by_depth(depth)
new_ary = self.dup
contains_array = false
tmp_ary = []
(1..depth).each do |n|
contains_array = false
tmp_ary = []
new_ary.each do |elem|
if elem.is_a? Array
contains_array = true if elem.is_a? Array
elem.each { |sub_elem| tmp_ary << sub_elem }
else
tmp_ary << elem
end
end
new_ary = tmp_ary
break if !contains_array || n >= depth
end
new_ary
end
end
class ArgumentError < StandardError; end
s = [ 1, 2, 3 ] #=> [1, 2, 3]
t = [ 4, 5, 6, [7, 8] ] #=> [4, 5, 6, [7, 8]]
a = [ s, t, 9, 10 ] #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
p a.my_flatten #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a = [ 1, 2, [3, [4, 5]]]
p a.my_flatten(1) #=> [1, 2, 3, [4, 5]]
p a.my_flatten('lol') #=> :depth should be a Fixnum (ArgumentError)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment