-
Notifications
You must be signed in to change notification settings - Fork 68
/
PartsOfList.rb
61 lines (43 loc) · 1.58 KB
/
PartsOfList.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 a function partlist that gives all the ways to divide a list (an array)
of at least two elements into two non-empty parts.
Each two non empty parts will be in a pair (or an array for languages without
tuples or a structin C - C: see Examples test Cases - )
Each part will be in a string
Elements of a pair must be in the same order as in the original array.
Example:
a = ["az", "toto", "picaro", "zone", "kiwi"] -->
[["az", "toto picaro zone kiwi"], ["az toto", "picaro zone kiwi"],
["az toto picaro", "zone kiwi"], ["az toto picaro zone", "kiwi"]]
or
a = {"az", "toto", "picaro", "zone", "kiwi"} -->
{{"az", "toto picaro zone kiwi"}, {"az toto", "picaro zone kiwi"},
{"az toto picaro", "zone kiwi"}, {"az toto picaro zone", "kiwi"}}
or
a = ["az", "toto", "picaro", "zone", "kiwi"] -->
[("az", "toto picaro zone kiwi"), ("az toto", "picaro zone kiwi"),
("az toto picaro", "zone kiwi"), ("az toto picaro zone", "kiwi")]
or
a = [|"az", "toto", "picaro", "zone", "kiwi"|] -->
[("az", "toto picaro zone kiwi"), ("az toto", "picaro zone kiwi"),
("az toto picaro", "zone kiwi"), ("az toto picaro zone", "kiwi")]
You can see other examples for each language in "Your test cases"
=end
# My Solution
def partlist(arr)
result = []
0.upto(arr.length-2) do |i|
result << [arr[0..i].join(" "), arr[i+1..-1].join(" ")]
end
result
end
# Better Solution
def partlist(arr)
(1...arr.size).map { |i|
[arr.take(i).join(' '), arr.drop(i).join(' ')]
}
end
# Another Solution
def partlist(arr)
(1...arr.size).map { |n| [arr.first(n).join(" "), arr.last(arr.size - n).join(" ")] }
end