Wednesday, February 21, 2024
HomeSoftware EngineeringThe Deaf Rats of Hamelin Problem in Java

# The Deaf Rats of Hamelin Problem in Java

## The problem#

The Pied Piper has been enlisted to play his magical tune and coax all of the rats out of city.

However a few of the rats are deaf and are going the improper manner!

Process

What number of deaf rats are there?

Legend

• `P` = The Pied Piper
• `O~` = Rat going left
• `~O` = Rat going proper

Examples

• ex1 `~O~O~O~O P` has 0 deaf rats
• ex2 `P O~ O~ ~O O~` has 1 deaf rat
• ex3 `~O~O~O~OP~O~OO~` has 2 deaf rats

## The answer in Java code#

Possibility 1:

``````public class DeafRatsOfHamelin {
public static int countDeafRats(remaining String city) {
String t = city.replaceAll(" ","");
int rely = 0;
for (int i = 0 ; i < t.size() ; i+=2) if (t.charAt(i) == 'O') rely++;
return rely;
}
}
``````

Possibility 2:

``````import java.util.stream.IntStream;

public class DeafRatsOfHamelin {
public static int countDeafRats(remaining String city) {
remaining String s = city.replaceAll("s+", "");
return (int) IntStream.vary(0, s.size()).filter(i -> s.charAt(i) == '~' && ipercent2 > 0).rely();
}
}
``````

Possibility 3:

``````public class DeafRatsOfHamelin {
public static int countDeafRats(remaining String city) {
int DeafRats=0;
String CompactTown;
CompactTown = city.exchange(" ","");
for(int i=0;i<CompactTown.size();i+=2){
if(CompactTown.charAt(i) == 'O') DeafRats++;
}
return DeafRats;
}
}
``````

## Take a look at circumstances to validate our resolution#

``````import org.junit.jupiter.api.Take a look at;
import static org.junit.jupiter.api.Assertions.*;

class DeafRatsOfHamelinTest {
@Take a look at
public void ex1() {
assertEquals(0, DeafRatsOfHamelin.countDeafRats("~O~O~O~O P"));
}
@Take a look at
public void ex2() {
assertEquals(1, DeafRatsOfHamelin.countDeafRats("P O~ O~ ~O O~"));
}
@Take a look at
public void ex3() {
assertEquals(2, DeafRatsOfHamelin.countDeafRats("~O~O~O~OP~O~OO~"));
}
}
``````

## Extra take a look at circumstances#

``````import org.junit.Take a look at;
import static org.junit.Assert.assertEquals;

public class SolutionTests {
@Take a look at
public void ex1() {
assertEquals(0, DeafRatsOfHamelin.countDeafRats("~O~O~O~O P"));
}

@Take a look at
public void ex2() {
assertEquals(1, DeafRatsOfHamelin.countDeafRats("P O~ O~ ~O O~"));
}

@Take a look at
public void ex3() {
assertEquals(2, DeafRatsOfHamelin.countDeafRats("~O~O~O~OP~O~OO~"));
}

@Take a look at
public void rats() {
assertEquals(8, DeafRatsOfHamelin.countDeafRats("O~~OO~~OO~~OO~P~OO~~OO~~OO~~O"));
assertEquals(8, DeafRatsOfHamelin.countDeafRats("O~~OO~~OO~~OO~ P~OO~~OO~~OO~~O"));
assertEquals(8, DeafRatsOfHamelin.countDeafRats("O~~OO~~OO~~OO~P ~OO~~OO~~OO~~O"));
}

@Take a look at
public void highlander() {
assertEquals(0, DeafRatsOfHamelin.countDeafRats("~OP"));
assertEquals(0, DeafRatsOfHamelin.countDeafRats("PO~"));
assertEquals(1, DeafRatsOfHamelin.countDeafRats("O~P"));
assertEquals(1, DeafRatsOfHamelin.countDeafRats("P~O"));
}

@Take a look at
public void empty() {
assertEquals(0, DeafRatsOfHamelin.countDeafRats("         P"));
assertEquals(0, DeafRatsOfHamelin.countDeafRats("P         "));
assertEquals(0, DeafRatsOfHamelin.countDeafRats("         P      "));
assertEquals(0, DeafRatsOfHamelin.countDeafRats("P"));
}

// ====================================

// Reference implementation for the Random take a look at circumstances
personal static class DeafRatsOfHamelin {

static int countDeafRats(remaining String city) {
String s = "";
for (int i = 0; i < city.size(); i++) {
remaining char c = city.charAt(i);
if (c == '~') { s += "R"; i ++; }
else if (c == 'O') { s += "L"; i ++; }
else if (c == 'P') { s += c; }
}
remaining String s1 = s.substring(0, s.indexOf("P")), s2 = s.substring(s.indexOf("P")+1);
int deaf = 0;
// Rats to left of the Piper needs to be going proper
deaf += s1.exchange("R","").size();
// Rats to proper of the Piper needs to be going left
deaf += s2.exchange("L","").size();
return deaf;
}

}

personal static String makeRatFacing(remaining char largely) {
remaining double d = Math.random() * 3;
if (largely == 'L') {
// Principally going through left
if (d < 2.5) return "O~";
if (d < 2.8) return "~O";
} else {
// Principally going through proper
if (d < 2.5) return "~O";
if (d < 2.8) return "O~";
}
return "  ";
}

personal static String makeTown(char piperPos) {
remaining int ratsLeft = (int)(Math.random() * 20) + 5;
remaining int ratsRight = (int)(Math.random() * 20) + 5;
String city = "";
boolean piper = false;
if (piperPos == 'L') { city += "P"; piper = true; }
for (int r = 0; r < ratsLeft; r++) {
city += makeRatFacing(piper ? 'L' : 'R');
}
if (piperPos == 'M') { city += "P"; piper = true; }
for (int r = 0; r < ratsRight; r++) {
city += makeRatFacing(piper ? 'L' : 'R');
}
if (piperPos == 'R') city += "P";
return city;
}

@Take a look at
public void random() {
for (int r = 1; r <= 200; r++) {
remaining int p = (int)(Math.random() * 3);
remaining char piper = p == 0 ? 'L' : p == 1 ? 'M' : 'R';
remaining String city = makeTown(piper);
remaining int anticipated = DeafRatsOfHamelin.countDeafRats(city);
System.out.println(String.format("Random take a look at %d : <span fashion='shade:inexperienced'>%s</span> has %d deaf rats", r, city, anticipated));
assertEquals(anticipated, DeafRatsOfHamelin.countDeafRats(city));
}
}
}
``````
RELATED ARTICLES