This repository has been archived by the owner on Jan 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise19.rb
59 lines (54 loc) · 1.6 KB
/
exercise19.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
# Exercise 19
def PermuteKofNwithDuplicates(index,selections,items,results)
if(index==selections.size)
result = []
0.upto(selections.size-1) do |i|
result << items[selections[i]]
end
results << result
else
0.upto(items.size-1) do |i|
selections[index] = i
PermuteKofNwithDuplicates(index+1,selections,items,results)
end
end
end
def PermuteKofNwithoutDuplicates(index,selections,items,results)
if(index==selections.size)
result = []
0.upto(selections.size-1) do |i|
result << items[selections[i]]
end
results << result
else
0.upto(items.size-1) do |i|
used = false
0.upto(index-1) do |j|
if(selections[j] == i) then used = true end
end
if(!used)
selections[index] = i
PermuteKofNwithoutDuplicates(index+1,selections,items,results)
end
end
end
end
# PERMUTATIONS
a = []
b = []
PermuteKofNwithDuplicates(0,Array.new(3,0),[4,5,6],b)
raise "Error" if b!= [[4, 4, 4], [4, 4, 5], [4, 4, 6],
[4, 5, 4], [4, 5, 5], [4, 5, 6],
[4, 6, 4], [4, 6, 5], [4, 6, 6],
[5, 4, 4], [5, 4, 5], [5, 4, 6],
[5, 5, 4], [5, 5, 5], [5, 5, 6],
[5, 6, 4], [5, 6, 5], [5, 6, 6],
[6, 4, 4], [6, 4, 5], [6, 4, 6],
[6, 5, 4], [6, 5, 5], [6, 5, 6],
[6, 6, 4], [6, 6, 5], [6, 6, 6]]
a = []
b = []
PermuteKofNwithoutDuplicates(0,Array.new(3,0),[4,5,6],b)
raise "Error" if b != [[4, 5, 6], [4, 6, 5],
[5, 4, 6], [5, 6, 4],
[6, 4, 5], [6, 5, 4]]