-
Notifications
You must be signed in to change notification settings - Fork 0
/
josephusPermutation.ts
48 lines (36 loc) · 1.27 KB
/
josephusPermutation.ts
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
export const josephus = <T>(items: T[], k: number): T[] => {
console.log(items)
let result: T[] = []
let keepGoing = true
let i = 0
while (keepGoing) {
if (items.length === 0) {
keepGoing = false;
break;
}
let indexToRemove = (i + k - 1) % items.length;
let itemToRemove = items.splice(indexToRemove, 1)[0];
result.push(itemToRemove);
i = indexToRemove;
}
return result;
};
//PREP
//Parameters:
//Items, an array of something and K, a number
//Return value: an array of whatever type
//Example:
/*
[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]
*/
//Psuedocode:
//k-1 is how much to move, assuming you begin on the first item
//when an item is removed, the pointer to current item should be decremented by 1
//