-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathJosephus.java
86 lines (79 loc) · 2.21 KB
/
Josephus.java
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
/*
* File: Josephus.java
* Author: Ashish Chopra
* Date: 29 May, 2013
* ---------------------------------------
* Josephus problem is a theoritcal computer science
* problem or a counting-out game in which N number of
* people are waiting in a circle to be executed. The executor
* will pick every Mth person and executes him. The only one left
* in the end will survive.
*
*/
package com.applications;
import com.queues.Queue;
/**
* Josephus is simulation of Josephus problem which accepts two integers
* from command line N, M and will print the series of execution.
* This implementation of Josephus problem is based on Queue data structure
* from com.queues package.
*
* @author Ashish Chopra
* @version 1.0
*/
public class Josephus {
private Queue<Integer> people;
private int N;
/**
* constructs a new Josephus object to
* execute with the initial number of people.
* @param N number of people as <code>int</code>.
*/
public Josephus(int N) {
if (N <= 0)
throw new IllegalArgumentException("You have entered a bad N :(");
people = new Queue<Integer>();
this.N = N;
for (int i = 0; i < N; i++) {
people.enqueue(i);
}
}
/**
* executes the person on the position specified by the
* argument.
* @param M position of a person as <code>int</code>.
* @return sequence of execution as <code>String</code>
*/
public String execute(int M) {
if (M < 1 || M >= N)
throw new IllegalArgumentException("M is out of range!");
String sequence = "";
while (!people.isEmpty()) {
sequence += removeAt(M) + " ";
}
return sequence;
}
/*
* removes the person from the data structure
* persistently.
*/
private String removeAt(int index) {
int count = 1;
while (count < index) {
int person = people.dequeue();
people.enqueue(person);
count++;
}
return people.dequeue().toString();
}
// Unit Test
public static void main(String[] args) {
if (args.length != 0) {
Josephus josephus = new Josephus(Integer.parseInt(args[0]));
String sequence = josephus.execute(Integer.parseInt(args[1]));
System.out.println(sequence);
} else {
throw new IllegalArgumentException("Command line arguments are not provided as required :(");
}
}
}