-
Notifications
You must be signed in to change notification settings - Fork 0
/
24.rb
56 lines (46 loc) · 1.04 KB
/
24.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
def permutations n
return Array(1..n).inject(:*)
end
def nth_permutation n, list
if list.length == 1
return list
end
first = list[0]
tail = list[1..-1]
perm_num = permutations( tail.length )
if perm_num >= n
return [first] + nth_permutation( n, tail )
else
replacement = list[(n-1)/perm_num]
tail = [first] + tail
first = replacement
tail.delete(replacement)
n -= ((n-1)/perm_num)*perm_num
if n == 0
return [first] + tail
else
return [first] + nth_permutation(n, tail)
end
end
end
def check i, orig, expected
got = nth_permutation(i, orig)
p "#{got == expected} - expected #{expected}, got #{got}"
end
def verify
nums = Array(0..3)
check 1, nums, [0,1,2,3]
check 2, nums, [0,1,3,2]
check 3, nums, [0,2,1,3]
check 4, nums, [0,2,3,1]
check 5, nums, [0,3,1,2]
check 6, nums, [0,3,2,1]
check 7, nums, [1,0,2,3]
check 8, nums, [1,0,3,2]
check 9, nums, [1,2,0,3]
check 10, nums, [1,2,3,0]
end
verify
nums = Array(0..9)
n = 1000000
p nth_permutation(n, nums).join("")