Ad
Code
Diff
  • /* 
     * 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
     */
    public class Kata {
      
      public static boolean[] populateBathroom(boolean[] urinals, int numOfPeople) {
        int spacing = (urinals.length - 1) / (numOfPeople - 1);
        
        // Gave function ability to use random boolean array as input
        for (int i = 0; i < urinals.length; i++){
          urinals[i] = i % spacing == 0;
        }
        
        return urinals;
      }
      
      // Generates the array based on two integer inputs
      public static boolean[] populateBathroom(int urinals, int people) {
        int spacing = (urinals - 1) / (people - 1);
        
        boolean[] toRet = new boolean[urinals];
        
        for (int i = 0; i < people; i++){
          toRet[i * spacing] = true;
        }
        
        return toRet;
      }
    }
    • /*
    • * 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
    • */
    • public class Kata {
    • // 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
    • public static boolean[] populateBathroom(boolean[] urinals, int numOfPeople) {
    • for (int i = 0; i < numOfPeople; i++) {
    • urinals[i * (urinals.length - 1) / (numOfPeople - 1)] = true;
    • int spacing = (urinals.length - 1) / (numOfPeople - 1);
    • // Gave function ability to use random boolean array as input
    • for (int i = 0; i < urinals.length; i++){
    • urinals[i] = i % spacing == 0;
    • }
    • return urinals;
    • }
    • // Generates the array based on two integer inputs
    • public static boolean[] populateBathroom(int urinals, int people) {
    • int spacing = (urinals - 1) / (people - 1);
    • boolean[] toRet = new boolean[urinals];
    • for (int i = 0; i < people; i++){
    • toRet[i * spacing] = true;
    • }
    • return toRet;
    • }
    • }

Removed unnecesary if-else statement

Code
Diff
  • public class Kata {
      public static boolean detectPalindrome(String word) {
        return word.equals(new StringBuilder(word).reverse().toString());
      }
    }
    • public class Kata {
    • public static boolean detectPalindrome(String userWord) {
    • // Detect whether this string is a palindrome
    • String backwardsWord = new StringBuilder(userWord).reverse().toString();
    • if (!userWord.equals(backwardsWord)) {
    • return false;
    • }
    • return true;
    • public static boolean detectPalindrome(String word) {
    • return word.equals(new StringBuilder(word).reverse().toString());
    • }
    • }