-
Code import java.util.List; import java.util.ArrayList; class Mastermind{ static String evaluate(String secret, String guess) { String[] secretColors = parseColors(secret); String[] guessColors = parseColors(guess); Result result = computeCorrectAndMisplaced(secretColors, guessColors); return "[" + result.correct() + "," + result.misplaced() + "]"; } private static String[] parseColors(String colors) { return colors.replace("[", "").replace("]", "").split(","); } private static Result computeCorrectAndMisplaced(String[] secretColors, String[] guessColors) { int correct = 0; int misplaced = 0; List<String> secretsLeftovers = new ArrayList<>(); List<String> guessLeftovers = new ArrayList<>(); for(int i = 0; i < secretColors.length; i++) { if (secretColors[i].equals(guessColors[i])) { correct++; } else { secretsLeftovers.add(secretColors[i]); guessLeftovers.add(guessColors[i]); } } for (String color : secretsLeftovers) { if (guessLeftovers.contains(color)) { misplaced++; } } return new Result(correct, misplaced); } } public record Result(int correct, int misplaced) {};
Test Cases import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { void one_peg_zero_correct() { assertEquals(Mastermind.evaluate("[blue]", "[red]"), "[0,0]"); } void one_peg_one_correct() { assertEquals(Mastermind.evaluate("[red]", "[red]"), "[1,0]"); } void two_pegs_one_correct() { assertEquals(Mastermind.evaluate("[red,blue]", "[red,green]"), "[1,0]"); } void two_pegs_two_correct() { assertEquals(Mastermind.evaluate("[red,blue]", "[red,blue]"), "[2,0]"); } void three_pegs_two_correct() { assertEquals(Mastermind.evaluate("[red,blue,green]", "[red,blue,yellow]"), "[2,0]"); } void three_pegs_one_correct_one_misplaced() { assertEquals(Mastermind.evaluate("[red,blue,green]", "[red,yellow,blue]"), "[1,1]"); } }
Output:
-
Code - import java.util.List;
- import java.util.ArrayList;
- class Mastermind{
- static String evaluate(String secret, String guess) {
- String[] secretColors = parseColors(secret);
- String[] guessColors = parseColors(guess);
int correct = countCorrect(secretColors, guessColors);int misplaced = countMisplaced(secretColors, guessColors);return "[" + correct + "," + misplaced + "]";- Result result = computeCorrectAndMisplaced(secretColors, guessColors);
- return "[" + result.correct() + "," + result.misplaced() + "]";
- }
- private static String[] parseColors(String colors) {
- return colors.replace("[", "").replace("]", "").split(",");
- }
private static int countCorrect(String[] secretColors, String[] guessColors) {- private static Result computeCorrectAndMisplaced(String[] secretColors, String[] guessColors) {
- int correct = 0;
for(int i = 0; i < secretColors.length; i++) {if (secretColors[i].equals(guessColors[i])) {correct++;}}return correct;}private static int countMisplaced(String[] secretColors, String[] guessColors) {- int misplaced = 0;
- List<String> secretsLeftovers = new ArrayList<>();
- List<String> guessLeftovers = new ArrayList<>();
for(int i = 0; i < secretColors.length; i++) {if (secretColors[i].equals(guessColors[i])) {continue;- for(int i = 0; i < secretColors.length; i++) {
- if (secretColors[i].equals(guessColors[i])) {
- correct++;
- } else {
- secretsLeftovers.add(secretColors[i]);
- guessLeftovers.add(guessColors[i]);
}}for (String color : secretsLeftovers) {if (guessLeftovers.contains(color)) {misplaced++;}- }
- }
- for (String color : secretsLeftovers) {
- if (guessLeftovers.contains(color)) {
- misplaced++;
- }
return misplaced;- }
}- return new Result(correct, misplaced);
- }
- }
- public record Result(int correct, int misplaced) {};
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}