-
Notifications
You must be signed in to change notification settings - Fork 0
/
flatten_array.rb
61 lines (56 loc) · 1.94 KB
/
flatten_array.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
=begin
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.
Please include unit tests and any documentation you feel is
necessary. In general, 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 or JavaScript's Array.flat.
=end
# Implementation
def flatten array
# create new array
@new_arr = []
# create definition to iterate the array
def iterate_array array
# for loop
for i in array do
# if it is an array, recurse with iterate function
# else, add the element to the new array
if i.is_a?(Array)
iterate_array i
else
@new_arr << i
end
end
end
# call the definition to iterate array
iterate_array array
# return the created array
@new_arr
end
# Assertions
def flatten_equal(array1, array2)
puts "Assert equal: "
flatten(array1) == array2 ? "Ok" : "Error"
end
def flatten_equal_null(array1, array2)
puts "Assert equal null: "
flatten(array1) - array2 == [] ? "Ok" : "Error"
end
# Tests
p flatten_equal([1, 2, [3]], [1, 2, 3])
p flatten_equal([1, 2, [3, 4]], [1, 2, 3, 4])
p flatten_equal([1, 2, [3, 4, [5, [6]]]], [1, 2, 3, 4, 5, 6])
p flatten_equal([1, 2, [3, 4, [5, [6, 7]]]], [1, 2, 3, 4, 5, 6, 7])
p flatten_equal([[1], [2], [3, 4], [5, 6, [7, 8]]], [1, 2, 3, 4, 5, 6, 7, 8])
p flatten_equal_null([1, 2, [3]], [1, 2, 3])
p flatten_equal_null([1, 2, [3, 4]], [1, 2, 3, 4])
p flatten_equal_null([1, 2, [3, 4, [5, [6]]]], [1, 2, 3, 4, 5, 6])
p flatten_equal_null([1, 2, [3, 4, [5, [6, 7]]]], [1, 2, 3, 4, 5, 6, 7])
p flatten_equal_null([[1], [2], [3, 4], [5, 6, [7, 8]]], [1, 2, 3, 4, 5, 6, 7, 8])