-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpossibilities.rb
More file actions
53 lines (43 loc) · 954 Bytes
/
possibilities.rb
File metadata and controls
53 lines (43 loc) · 954 Bytes
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
=begin
Source: Codewars
Difficulty: 4 kyu
Title: Life of Possibilities
Description:
Given a map of words and their translations, generate a list of
all possible unique combinations of translations, sorted lexically.
Example:
Given the map of words:
words = {
life: %w{ vida vie Leben },
death: %w{ muerte mort Tode }
}
The method should return the result:
{
life: [
['Leben'],
['Leben', 'vida'],
['Leben', 'vida', 'vie'],
['Leben', 'vie'],
['vida'],
['vida', 'vie'],
['vie']
],
death: [
['Tode'],
['Tode', 'mort'],
['Tode', 'mort', 'muerte'],
['Tode', 'muerte'],
['mort'],
['mort', 'muerte'],
['muerte']
]
}
=end
def possibilities(words)
words.each do |k, v|
res = []
(1..(v.length)).each { |n| res << v.sort.combination(n).to_a }
words[k] = res.flatten(1).sort
end
words
end