Java Katas 👷🏼♂️👷🏼♀️
Sum of the first nth terms of a Series
Your task is to write a function which returns the sum of following series upto nth term(parameter).
Series: 1 + 1/4 + 1/7 + 1/10 + 1/13 + 1/16 +...
Rules
You need to round the answer to 2 decimal places and return it as String.
If the given value is 0 then it should return 0.00
You will only be given Natural Numbers as arguments.
My approach
public class NthSeries {
public static String seriesSum(int n) {
double total = 0.0;
double denominator = 1.0;
for(int i=0; i<n;i++) {
total += (1.0/denominator);
denominator += 3.0;
}
return String.format("%.2f", total);
}
}
A method using a more functional programming approach
import java.util.stream.IntStream;
public class NthSeries {
public static String seriesSum(int n) {
return String.format("%.2f", IntStream.range(0, n).mapToDouble(num -> 1.0 / (1 + num * 3)).sum());
}
}
Multiples of 3 or 5
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in.
Note: If the number is a multiple of both 3 and 5, only count it once.
My initial approach...
public class Solution {
public int solution(int number) {
int total= 0;
for(int i= 0; i<number; i++) {
if((i%3==0)||(i%5==0)) total+= i;
}
return total;
}
}
The more functional approach
import java.util.stream.IntStream;
public class Solution {
public int solution(int number) {
return IntStream.range(0, number)
.filter(n -> (n % 3 == 0) || (n % 5 == 0))
.sum();
}
}
Detect Pangram
My approach..
import java.util.Arrays;
import java.util.ArrayList;
public class PangramChecker {
public boolean check(String sentence){
ArrayList<Character> alphabet = new ArrayList<>(
Arrays.asList('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z')
);
for(int i = 0; i < sentence.length(); i++) {
alphabet.remove((Character)sentence.toLowerCase().charAt(i));
if(alphabet.isEmpty()) break;
}
return alphabet.isEmpty();
}
}
Others...
public class PangramChecker {
public boolean check(String sentence){
for (char c = 'a'; c<='z'; c++)
if (!sentence.toLowerCase().contains("" + c))
return false;
return true;
}
}
Or...
public class PangramChecker {
public boolean check(String sentence){
return sentence
.chars()
.map(Character::toLowerCase)
.filter(Character::isAlphabetic)
.distinct()
.count() == 26;
}
}
Or...
class PangramChecker {
boolean check(final String sentence) {
return sentence.chars()
.filter(Character::isLetter)
.map(Character::toLowerCase)
.distinct()
.count() == 26;
}
}
Create Phone Numbers
Turn 1234567890 into (123) 456-7890
My approach...
public class Kata {
public static String createPhoneNumber(int[] numbers) {
return "("+numbers[0]+numbers[1]+numbers[2]+") "+numbers[3]+numbers[4]+numbers[5]+"-"+numbers[6]+numbers[7]+numbers[8]+numbers[9];
}
}
Others...
import java.util.stream.IntStream;
public class Kata {
public static String createPhoneNumber(int[] numbers) {
return String.format("(%d%d%d) %d%d%d-%d%d%d%d", IntStream.of(numbers).boxed().toArray());
}
}
Spinning Words
Given a sentence, reverse every word that is 5 letter or over.
My approach...
public class SpinWords {
public String spinWords(String sentence) {
String[] words = sentence.split(" ");
StringBuilder outputString = new StringBuilder();
for(int i=0; i<words.length; i++)
{
outputString.append(spinWord(words[i]));
if(i!= words.length-1) outputString.append(" ");
}
return outputString.toString();
}
private String spinWord(String word)
{
StringBuilder reverseWord = new StringBuilder();
if(word.length()<5) return word;
for(int i=word.length()-1; i>=0; i--)
{
reverseWord.append(word.charAt(i));
}
return reverseWord.toString();
}
}
Others...
import java.util.stream.*;
import java.util.Arrays;
public class SpinWords {
public String spinWords(String sentence) {
return Arrays.stream(sentence.split(" "))
.map(i -> i.length() > 4 ? new StringBuilder(i).reverse().toString() : i)
.collect(Collectors.joining(" "));
}
}
Or...
import java.util.Arrays;
public class SpinWords {
public String spinWords(String sentence) {
String[] words = sentence.split(" ");
for (int i=0; i<words.length; i++) {
if (words[i].length() >= 5) {
words[i] = new StringBuilder(words[i]).reverse().toString();
}
}
return String.join(" ",words);
}
}
Two Fighters, one winner
public class Kata {
public static String declareWinner(Fighter fighter1, Fighter fighter2, String firstAttacker) {
Fighter attacker = fighter1.name.equals(firstAttacker) ? fighter1 : fighter2;
Fighter defender;
do {
defender = (attacker == fighter1) ? fighter2 : fighter1;
defender.health -= attacker.damagePerAttack;
attacker = (attacker == fighter1) ? fighter2 : fighter1;
} while(defender.health>0);
return (fighter1.health <= 0) ? fighter2.name : fighter1.name;
}
}
Dubstep
My approach...
import java.util.Arrays;
import java.util.List;
import java.util.stream.*;
public class Dubstep {
public static String SongDecoder (String song)
{
return String.join(" ",
Arrays.asList(song.split("WUB"))
.stream().filter( item -> !item.isEmpty() )
.collect(Collectors.toList()));
}
}
Others...
Using a Regex replaceAll and a trim.
public class Dubstep {
public static String SongDecoder (String song)
{
return song.replaceAll("(WUB)+", " ").trim();
}
}