The Deaf Rats of Hamelin Problem in Java

0
8
Adv1


Adv2

The problem

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

However a number of the rats are deaf and are going the improper means!

Job

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.change(" ","");
    for(int i=0;i<CompactTown.size();i+=2){   
        if(CompactTown.charAt(i) == 'O') DeafRats++;     
    }
    return DeafRats;
  }
}

Check circumstances to validate our resolution

import org.junit.jupiter.api.Check;
import static org.junit.jupiter.api.Assertions.*;

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

Further take a look at circumstances

import org.junit.Check;
import static org.junit.Assert.assertEquals;

public class SolutionTests {
  @Check
  public void ex1() {
    assertEquals(0, DeafRatsOfHamelin.countDeafRats("~O~O~O~O P"));
  }
  
  @Check
  public void ex2() {
    assertEquals(1, DeafRatsOfHamelin.countDeafRats("P O~ O~ ~O O~"));
  }
  
  @Check
  public void ex3() {
    assertEquals(2, DeafRatsOfHamelin.countDeafRats("~O~O~O~OP~O~OO~"));
  }

  @Check
  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"));
  }
  
  @Check
  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"));
  }

  @Check
  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
  non-public 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 must be going proper
      deaf += s1.change("R","").size();
      // Rats to proper of the Piper must be going left
      deaf += s2.change("L","").size();
      return deaf;
    }
  
  }
  
  non-public static String makeRatFacing(remaining char principally) {
    remaining double d = Math.random() * 3;
    if (principally == 'L') {
      // Largely dealing with left
      if (d < 2.5) return "O~";
      if (d < 2.8) return "~O";
    } else {
      // Largely dealing with proper
      if (d < 2.5) return "~O";
      if (d < 2.8) return "O~";
    }
    return "  ";
  }
  
  non-public 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;
  }
  
  @Check
  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 type='shade:inexperienced'>%s</span> has %d deaf rats", r, city, anticipated));      
      assertEquals(anticipated, DeafRatsOfHamelin.countDeafRats(city));
    }
  }
}
Adv3