public class Kata { public static boolean[] populateBathroom(boolean[] urinals, int numOfPeople) { // Implement the urinal problem, described as "given x number of people walking // into a bathroom that contains y number of urinals, describe the optimate // placement of people so that none is standing next to the other" // In this case, the urinals are represented by a boolean array with // true representing a person at a urinal, and false meaning the urinal is empty if (urinals.length < numOfPeople) { throw new RuntimeException("Cursed input data."); } int peopleRemain = numOfPeople; int peoplePlacePointer = 0; boolean[] result = urinals; while(peopleRemain > 0) { if (peopleRemain == 1) { // only one person is not at the urinal // we'll place him to far corner of the toilet :) result[urinals.length - 1] = true; peopleRemain--; } else { // if more than one person is not at the urinal // place first of them at the urinal with number peoplePlacePointer result[peoplePlacePointer] = true; // then increments the peoplePlacePoiner by value // which depends on empty urinals count from rigth side of peoplePlacePointer and remain people count // the point is to distribute resulting empty urinals so that almost equals spaces will be between people peoplePlacePointer += 1 + (urinals.length - peoplePlacePointer) / peopleRemain; // one less left peopleRemain--; } } return result; } }
- public class Kata {
- public static boolean[] populateBathroom(boolean[] urinals, int numOfPeople) {
- // Implement the urinal problem, described as "given x number of people walking
- // into a bathroom that contains y number of urinals, describe the optimate
- // placement of people so that none is standing next to the other"
- // In this case, the urinals are represented by a boolean array with
- // true representing a person at a urinal, and false meaning the urinal is empty
return new boolean[] {true, false, true};- if (urinals.length < numOfPeople) {
- throw new RuntimeException("Cursed input data.");
- }
- int peopleRemain = numOfPeople;
- int peoplePlacePointer = 0;
- boolean[] result = urinals;
- while(peopleRemain > 0) {
- if (peopleRemain == 1) {
- // only one person is not at the urinal
- // we'll place him to far corner of the toilet :)
- result[urinals.length - 1] = true;
- peopleRemain--;
- } else {
- // if more than one person is not at the urinal
- // place first of them at the urinal with number peoplePlacePointer
- result[peoplePlacePointer] = true;
- // then increments the peoplePlacePoiner by value
- // which depends on empty urinals count from rigth side of peoplePlacePointer and remain people count
- // the point is to distribute resulting empty urinals so that almost equals spaces will be between people
- peoplePlacePointer += 1 + (urinals.length - peoplePlacePointer) / peopleRemain;
- // one less left
- peopleRemain--;
- }
- }
- return result;
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertArrayEquals; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test void testSomething() { assertArrayEquals(Kata.populateBathroom(new boolean[3], 2), new boolean[] {true, false, true}); assertArrayEquals(Kata.populateBathroom(new boolean[4], 2), new boolean[] {true, false, false, true}); assertArrayEquals(Kata.populateBathroom(new boolean[5], 2), new boolean[] {true, false, false, false, true}); assertArrayEquals(Kata.populateBathroom(new boolean[5], 3), new boolean[] {true, false, true, false, true}); assertArrayEquals(Kata.populateBathroom(new boolean[7], 3), new boolean[] {true, false, false, true, false, false, true}); assertArrayEquals(Kata.populateBathroom(new boolean[7], 4), new boolean[] {true, false, true, false, true, false, true}); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertArrayEquals;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
- @Test
- void testSomething() {
- assertArrayEquals(Kata.populateBathroom(new boolean[3], 2), new boolean[] {true, false, true});
- assertArrayEquals(Kata.populateBathroom(new boolean[4], 2), new boolean[] {true, false, false, true});
- assertArrayEquals(Kata.populateBathroom(new boolean[5], 2), new boolean[] {true, false, false, false, true});
- assertArrayEquals(Kata.populateBathroom(new boolean[5], 3), new boolean[] {true, false, true, false, true});
- assertArrayEquals(Kata.populateBathroom(new boolean[7], 3), new boolean[] {true, false, false, true, false, false, true});
- assertArrayEquals(Kata.populateBathroom(new boolean[7], 4), new boolean[] {true, false, true, false, true, false, true});
- }
- }