#81 Format words into a sentence


Complete the method so that it formats the words into a single comma separated value. The last word should be separated by the word 'and' instead of a comma. The method takes in an array of strings and returns a single formatted string. Empty string values should be ignored. Empty arrays or null/nil values being passed into the method should result in an empty string being returned.


Kata.formatWords(new String[] {"ninja", "samurai", "ronin"}) => "ninja, samurai and ronin"

Kata.formatWords(new String[] {"ninja", "", "ronin"}) => "ninja and ronin"

Kata.formatWords(new String[] {}) => ""



import java.util.Arrays;
import java.util.stream.Collectors;

public class Kata {

	 public static String replaceLast(String str, String regex, String replacement) {
	        int regexIndexOf = str.lastIndexOf(regex);
	        if(regexIndexOf == -1){
	            return str;
	        }else{
	            return str.substring(0, regexIndexOf) + replacement + str.substring(regexIndexOf + regex.length());
	        }
	}

  public static String formatWords(String[] words) {
    // Do the things...
    if(words == null) return "";
    return replaceLast(Arrays.stream(words).filter(word -> !word.equals("")).collect(Collectors.joining(", ")),", "," and ");
  }

}

#80 Calculating Batting Average


In baseball, the batting average is a simple and most common way to measure a hitter's performace. Batting average is calculated by taking all the players hits and dividing it by their number of at_bats, and it is usually displayed as a 3 digit decimal (i.e. 0.300).


Given a yankees table with the following schema,


-player_id STRING


-player_name STRING


-primary_position STRING


-games INTEGER


-at_bats INTEGER


-hits INTEGER


return a table with player_name, games, and batting_average.


We want batting_average to be rounded to the nearest thousandth, since that is how baseball fans are used to seeing it. Format it as text and make sure it has 3 digits to the right of the decimal (pad with zeroes if neccesary).


Next, order our resulting table by batting_average, with the highest average in the first row.


Finally, since batting_average is a rate statistic, a small number of at_bats can change the average dramatically. To correct for this, exclude any player who doesn't have at least 100 at bats.


Expected Output Table


-player_name STRING


-games INTEGER


-batting_average STRING



#79 Human readable duration format


Your task in order to complete this Kata is to write a function which formats a duration, given as a number of seconds, in a human-friendly way.


The function must accept a non-negative integer. If it is zero, it just returns "now". Otherwise, the duration is expressed as a combination of years, days, hours, minutes and seconds.


It is much easier to understand with an example:


TimeFormatter.formatDuration(62)   //returns "1 minute and 2 seconds"

TimeFormatter.formatDuration(3662) //returns "1 hour, 1 minute and 2 seconds"

For the purpose of this Kata, a year is 365 days and a day is 24 hours.


Note that spaces are important.


Detailed rules

The resulting expression is made of components like 4 seconds, 1 year, etc. In general, a positive integer and one of the valid units of time, separated by a space. The unit of time is used in plural if the integer is greater than 1.


The components are separated by a comma and a space (", "). Except the last component, which is separated by " and ", just like it would be written in English.


A more significant units of time will occur before than a least significant one. Therefore, 1 second and 1 year is not correct, but 1 year and 1 second is.


Different components have different unit of times. So there is not repeated units like in 5 seconds and 1 second.


A component will not appear at all if its value happens to be zero. Hence, 1 minute and 0 seconds is not valid, but it should be just 1 minute.


A unit of time must be used "as much as possible". It means that the function should not return 61 seconds, but 1 minute and 1 second instead. Formally, the duration specified by of a component must not be greater than any valid more significant unit of time.




+ Recent posts