forked from adrianeyre/codewars
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoeNum.rb
More file actions
52 lines (41 loc) · 1.81 KB
/
JoeNum.rb
File metadata and controls
52 lines (41 loc) · 1.81 KB
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
=begin
This problem takes its name by arguably the most important event in the life of
the ancient historian Josephus: according to his tale, he and his 40 soldiers
were trapped in a cave by the Romans during a siege.
Refusing to surrender to the enemy, they instead opted for mass suicide, with a
twist: they formed a circle and proceeded to kill one man every three, until one
last man was left (and that it was supposed to kill himself to end the act).
Well, Josephus and another man were the last two and, as we now know every
detail of the story, you may have correctly guessed that they didn't exactly
follow through the original idea.
You are now to create a function that returns a Josephus permutation, taking as
parameters the initial array/list of items to be permuted as if they were in a
circle and counted out every k places until none remained.
Tips and notes: it helps to start counting from 1 up to n, instead of the usual
range 0..n-1; k will always be >=1.
For example, with n=7 and k=3 josephus(7,3) should act this way.
[1,2,3,4,5,6,7] - initial sequence
[1,2,4,5,6,7] => 3 is counted out and goes into the result [3]
[1,2,4,5,7] => 6 is counted out and goes into the result [3,6]
[1,4,5,7] => 2 is counted out and goes into the result [3,6,2]
[1,4,5] => 7 is counted out and goes into the result [3,6,2,7]
[1,4] => 5 is counted out and goes into the result [3,6,2,7,5]
[4] => 1 is counted out and goes into the result [3,6,2,7,5,1]
[] => 4 is counted out and goes into the result [3,6,2,7,5,1,4]
So our final result is:
josephus([1,2,3,4,5,6,7],3)==[3,6,2,7,5,1,4]
=end
# My Solution
def josephus(items,k)
result = []
items.each do |x|
items = items.rotate(k-1)
result << items[0]
items.shift
end
result
end
# Better Solution
def josephus(items,k)
Array.new(items.length){items.rotate!(k-1).shift}
end