-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex18.5.lua
110 lines (99 loc) · 2.37 KB
/
ex18.5.lua
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
local function get_combination (m, n, k) -- choose k elements from m to n
-- inclusive
if k == 0 then
return {{}}
end
if n - m + 1 < k then
return nil
end
local all = {}
for i = m, n do
local others = get_combination(i + 1, n, k - 1)
if others then
for _, s in pairs(others) do
table.insert(s, i)
table.insert(all, s)
end
end
end
return all
end
local function all_subset(s, f)
local keys = {}
for _, i in pairs(s) do
table.insert(keys, i)
end
for i = 0, #keys do
local combinations = get_combination(1, #keys, i)
for _, c in pairs(combinations) do
local t = {}
for _, key_index in pairs(c) do
table.insert(t, keys[key_index])
end
f(t)
end
end
end
local function print_set(s)
for _, v in pairs(s) do
io.write(v)
io.write("\t")
end
io.write("\n")
end
local set = { }
table.insert(set, "ann")
table.insert(set, "bob")
table.insert(set, "charlie")
-- all_subset(set, print_set)
------------------------------------------------------------------
-- An alternative solution, or preferred
------------------------------------------------------------------
local function iterate_all_combinations(set, f)
local n = #set
local all_values = {}
for _, v in pairs(set) do
table.insert(all_values, v)
end
local length = 0
local t = {}
local subset = {}
repeat
assert(#t == length)
assert(#t == #subset)
f(subset)
if length == n then
break
end
repeat
if #t < 1 then
length = length + 1
for i = 1, length do
table.insert(t, i)
table.insert(subset, all_values[i])
end
break
else
if t[#t] < n then
t[#t] = t[#t] + 1
table.remove(subset)
table.insert(subset, all_values[t[#t]])
if length - #t <= n - t[#t] then
while #t < length do
table.insert(t, t[#t] + 1)
table.insert(subset, all_values[t[#t]])
end
break
else
table.remove(t)
table.remove(subset)
end
else
table.remove(t)
table.remove(subset)
end
end
until false
until false
end
iterate_all_combinations(set, print_set)