Josephus Problem with cyclic iterator
up vote
0
down vote
favorite
Given the Josephus Problem.
Josephus Problem
N people (numbered 1 to N) are standing in a circle. Person 1 kills Person 2 with a sword and gives it to Person 3. Person 3 kills Person 4 and gives the sword to Person 5. This process is repeated until only one person is alive.
Task:
(Medium) Given the number of people N, write a program to find the number of the person that stays alive at the end.
(Hard) Show each step of the process.
(The description from Sololearn application)"
This is my code. The forEachRemaining
method solution is correct? I have to do something with this inherited method, but it has no meaning.
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
class CyclicIterator implements Iterator {
private final List list;
private Iterator iterator;
public CyclicIterator(List list) {
this.list = list;
initIterator(list);
}
private void initIterator(List list) {
this.iterator = list.iterator();
}
@Override
public boolean hasNext() {
return !list.isEmpty();
}
@Override
public Object next() {
if (!this.iterator.hasNext())
initIterator(list);
return this.iterator.next();
}
@Override
public void remove() {
this.iterator.remove();
}
@Override
public void forEachRemaining(Consumer action) {
throw new UnsupportedOperationException("This method has no meaning in CyclicIterator class!");
}
}
public class JosephusProblem {
public static void main(String args) {
execution(0);
execution(1);
execution(2);
execution(4);
execution(6);
}
private static void execution(int members) {
if (members < 1) {
System.out.println("The parameter (members) has to be bigger than 0!");
return;
}
if (members == 1) {
System.out.println("There is olny one person, so he is the survivor. Peaceful version! :)");
return;
}
LinkedList<Integer> list = new LinkedList();
for (int index = 0; index < members; index++)
list.add(index + 1);
Iterator<Integer> it = new CyclicIterator(list);
System.out.println("For " + members + " members: ");
while (members-- > 1) {
System.out.print(it.next() + " kills " + it.next() + ", ");
it.remove();
}
System.out.println("n The survivor: " + it.next());
}
}
java linked-list circular-list
bumped to the homepage by Community♦ 9 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
0
down vote
favorite
Given the Josephus Problem.
Josephus Problem
N people (numbered 1 to N) are standing in a circle. Person 1 kills Person 2 with a sword and gives it to Person 3. Person 3 kills Person 4 and gives the sword to Person 5. This process is repeated until only one person is alive.
Task:
(Medium) Given the number of people N, write a program to find the number of the person that stays alive at the end.
(Hard) Show each step of the process.
(The description from Sololearn application)"
This is my code. The forEachRemaining
method solution is correct? I have to do something with this inherited method, but it has no meaning.
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
class CyclicIterator implements Iterator {
private final List list;
private Iterator iterator;
public CyclicIterator(List list) {
this.list = list;
initIterator(list);
}
private void initIterator(List list) {
this.iterator = list.iterator();
}
@Override
public boolean hasNext() {
return !list.isEmpty();
}
@Override
public Object next() {
if (!this.iterator.hasNext())
initIterator(list);
return this.iterator.next();
}
@Override
public void remove() {
this.iterator.remove();
}
@Override
public void forEachRemaining(Consumer action) {
throw new UnsupportedOperationException("This method has no meaning in CyclicIterator class!");
}
}
public class JosephusProblem {
public static void main(String args) {
execution(0);
execution(1);
execution(2);
execution(4);
execution(6);
}
private static void execution(int members) {
if (members < 1) {
System.out.println("The parameter (members) has to be bigger than 0!");
return;
}
if (members == 1) {
System.out.println("There is olny one person, so he is the survivor. Peaceful version! :)");
return;
}
LinkedList<Integer> list = new LinkedList();
for (int index = 0; index < members; index++)
list.add(index + 1);
Iterator<Integer> it = new CyclicIterator(list);
System.out.println("For " + members + " members: ");
while (members-- > 1) {
System.out.print(it.next() + " kills " + it.next() + ", ");
it.remove();
}
System.out.println("n The survivor: " + it.next());
}
}
java linked-list circular-list
bumped to the homepage by Community♦ 9 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Given the Josephus Problem.
Josephus Problem
N people (numbered 1 to N) are standing in a circle. Person 1 kills Person 2 with a sword and gives it to Person 3. Person 3 kills Person 4 and gives the sword to Person 5. This process is repeated until only one person is alive.
Task:
(Medium) Given the number of people N, write a program to find the number of the person that stays alive at the end.
(Hard) Show each step of the process.
(The description from Sololearn application)"
This is my code. The forEachRemaining
method solution is correct? I have to do something with this inherited method, but it has no meaning.
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
class CyclicIterator implements Iterator {
private final List list;
private Iterator iterator;
public CyclicIterator(List list) {
this.list = list;
initIterator(list);
}
private void initIterator(List list) {
this.iterator = list.iterator();
}
@Override
public boolean hasNext() {
return !list.isEmpty();
}
@Override
public Object next() {
if (!this.iterator.hasNext())
initIterator(list);
return this.iterator.next();
}
@Override
public void remove() {
this.iterator.remove();
}
@Override
public void forEachRemaining(Consumer action) {
throw new UnsupportedOperationException("This method has no meaning in CyclicIterator class!");
}
}
public class JosephusProblem {
public static void main(String args) {
execution(0);
execution(1);
execution(2);
execution(4);
execution(6);
}
private static void execution(int members) {
if (members < 1) {
System.out.println("The parameter (members) has to be bigger than 0!");
return;
}
if (members == 1) {
System.out.println("There is olny one person, so he is the survivor. Peaceful version! :)");
return;
}
LinkedList<Integer> list = new LinkedList();
for (int index = 0; index < members; index++)
list.add(index + 1);
Iterator<Integer> it = new CyclicIterator(list);
System.out.println("For " + members + " members: ");
while (members-- > 1) {
System.out.print(it.next() + " kills " + it.next() + ", ");
it.remove();
}
System.out.println("n The survivor: " + it.next());
}
}
java linked-list circular-list
Given the Josephus Problem.
Josephus Problem
N people (numbered 1 to N) are standing in a circle. Person 1 kills Person 2 with a sword and gives it to Person 3. Person 3 kills Person 4 and gives the sword to Person 5. This process is repeated until only one person is alive.
Task:
(Medium) Given the number of people N, write a program to find the number of the person that stays alive at the end.
(Hard) Show each step of the process.
(The description from Sololearn application)"
This is my code. The forEachRemaining
method solution is correct? I have to do something with this inherited method, but it has no meaning.
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
class CyclicIterator implements Iterator {
private final List list;
private Iterator iterator;
public CyclicIterator(List list) {
this.list = list;
initIterator(list);
}
private void initIterator(List list) {
this.iterator = list.iterator();
}
@Override
public boolean hasNext() {
return !list.isEmpty();
}
@Override
public Object next() {
if (!this.iterator.hasNext())
initIterator(list);
return this.iterator.next();
}
@Override
public void remove() {
this.iterator.remove();
}
@Override
public void forEachRemaining(Consumer action) {
throw new UnsupportedOperationException("This method has no meaning in CyclicIterator class!");
}
}
public class JosephusProblem {
public static void main(String args) {
execution(0);
execution(1);
execution(2);
execution(4);
execution(6);
}
private static void execution(int members) {
if (members < 1) {
System.out.println("The parameter (members) has to be bigger than 0!");
return;
}
if (members == 1) {
System.out.println("There is olny one person, so he is the survivor. Peaceful version! :)");
return;
}
LinkedList<Integer> list = new LinkedList();
for (int index = 0; index < members; index++)
list.add(index + 1);
Iterator<Integer> it = new CyclicIterator(list);
System.out.println("For " + members + " members: ");
while (members-- > 1) {
System.out.print(it.next() + " kills " + it.next() + ", ");
it.remove();
}
System.out.println("n The survivor: " + it.next());
}
}
java linked-list circular-list
java linked-list circular-list
asked Nov 14 at 13:27
MAttti
184
184
bumped to the homepage by Community♦ 9 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 9 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
The documentation for forEachRemaining
states that the behavior is equivalent to
while (hasNext())
action.accept(next());
so why not just put that there?
I think that would be an infinite loop. ThehasNext()
is always true if the list is not empty.
– MAttti
Nov 15 at 21:28
Of course the next line has a next() call in it …action.accept(next())
.
– K.Nicholas
Nov 15 at 22:48
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207657%2fjosephus-problem-with-cyclic-iterator%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
The documentation for forEachRemaining
states that the behavior is equivalent to
while (hasNext())
action.accept(next());
so why not just put that there?
I think that would be an infinite loop. ThehasNext()
is always true if the list is not empty.
– MAttti
Nov 15 at 21:28
Of course the next line has a next() call in it …action.accept(next())
.
– K.Nicholas
Nov 15 at 22:48
add a comment |
up vote
0
down vote
The documentation for forEachRemaining
states that the behavior is equivalent to
while (hasNext())
action.accept(next());
so why not just put that there?
I think that would be an infinite loop. ThehasNext()
is always true if the list is not empty.
– MAttti
Nov 15 at 21:28
Of course the next line has a next() call in it …action.accept(next())
.
– K.Nicholas
Nov 15 at 22:48
add a comment |
up vote
0
down vote
up vote
0
down vote
The documentation for forEachRemaining
states that the behavior is equivalent to
while (hasNext())
action.accept(next());
so why not just put that there?
The documentation for forEachRemaining
states that the behavior is equivalent to
while (hasNext())
action.accept(next());
so why not just put that there?
answered Nov 15 at 5:04
K.Nicholas
1011
1011
I think that would be an infinite loop. ThehasNext()
is always true if the list is not empty.
– MAttti
Nov 15 at 21:28
Of course the next line has a next() call in it …action.accept(next())
.
– K.Nicholas
Nov 15 at 22:48
add a comment |
I think that would be an infinite loop. ThehasNext()
is always true if the list is not empty.
– MAttti
Nov 15 at 21:28
Of course the next line has a next() call in it …action.accept(next())
.
– K.Nicholas
Nov 15 at 22:48
I think that would be an infinite loop. The
hasNext()
is always true if the list is not empty.– MAttti
Nov 15 at 21:28
I think that would be an infinite loop. The
hasNext()
is always true if the list is not empty.– MAttti
Nov 15 at 21:28
Of course the next line has a next() call in it …
action.accept(next())
.– K.Nicholas
Nov 15 at 22:48
Of course the next line has a next() call in it …
action.accept(next())
.– K.Nicholas
Nov 15 at 22:48
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207657%2fjosephus-problem-with-cyclic-iterator%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown