-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathSplitandJoin.rb
64 lines (50 loc) · 1.39 KB
/
SplitandJoin.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
62
63
64
=begin
There is a 2D array arr:
[[1],[2,3],[4,5,6],[7,8,9,10]]
Task1: Write a function split. Split 2D array to two arrays:
Result:
array 1: all numbers in 2D array array 2: Leave all subarrays and
fill in the length of
each subarray
[ [1,2,3,4,5,6,7,8,9,10], [[1],[2],[3],[4]] ]
Task2: Write a function join. Join two array(like the result above) to one
array(like the arr above)
Some Examples
split:
arr= [[1],[2,3],[4,5,6],[7,8,9,10]]
split(arr) === [ [1,2,3,4,5,6,7,8,9,10], [[1],[2],[3],[4]] ]
arr= [[1,3,5],[2,4,6],[7,8,9]]
split(arr) === [ [1,3,5,2,4,6,7,8,9], [[3],[3],[3]] ]
join:
arr1=[1,2,3,4,5,6,7,8,9,10], arr2=[[1],[2],[3],[4]]
join(arr1,arr2) === [[1],[2,3],[4,5,6],[7,8,9,10]]
arr1=[1,3,5,2,4,6,7,8,9], arr2=[[3],[3],[3]]
join(arr1,arr2) === [[1,3,5],[2,4,6],[7,8,9]]
=end
# My Solution
def split(arr)
second = []
arr.each {|x| second << [x.length]}
[arr.flatten, second]
end
def join(arr1, arr2)
tempresult = []
result = []
num = -1
0.upto(arr2.length-1) do |x|
tempresult = []
0.upto(arr2[x][0]-1) do |y|
num += 1
tempresult << arr1[num]
end
result << tempresult
end
result
end
# Better Solution
def split(arr)
[arr.flatten, arr.map { |subarr| [subarr.length] }]
end
def join(arr1, arr2)
arr2.map { |(n)| arr1.shift(n) }
end