Sha256: 49e30fbbcdbd155b37633111b84b66c208159d92ee72909feb5182fd2a5ca8d4

Contents?: true

Size: 1.77 KB

Versions: 91

Compression:

Stored size: 1.77 KB

Contents

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

class PigLatinTranslator {

    private static final String AY = "ay";
    private static final String THR = "thr";
    private static final String SCH = "sch";
    private static final String CH = "ch";
    private static final String QU = "qu";
    private static final String TH = "th";
    private static final String QUAY = "quay";
    private static final String XR = "xr";
    private static final String YT = "yt";
    private static final String VOWELS_REGEX = "[aeiou]";

    String translate(String sentence) {
        return Arrays.stream(sentence.split(" "))
                .map(this::translateWord)
                .collect(Collectors.joining(" "));
    }

    private String translateWord(String word) {
        if (wordStartsWithVowelLike(word)) {
            return word + AY;
        }

        if (wordStartsWithPrefixes(word, THR, SCH)) {
            return word.substring(3) + word.substring(0, 3) + AY;
        }

        if (wordStartsWithPrefixes(word, CH, QU, TH)) {
            return word.substring(2) + word.substring(0, 2) + AY;
        }

        if (wordStartsWithConsonantAndQu(word)) {
            return word.substring(3) + word.toCharArray()[0] + QUAY;
        }

        return word.substring(1) + word.toCharArray()[0] + AY;
    }

    private boolean wordStartsWithVowelLike(String word) {
        return word.startsWith(YT) || word.startsWith(XR) || word.substring(0, 1).matches(VOWELS_REGEX);
    }

    private boolean wordStartsWithPrefixes(String word, String... prefixes) {
        return Arrays.stream(prefixes)
                .anyMatch(word::startsWith);
    }

    private boolean wordStartsWithConsonantAndQu(String word) {
        return word.substring(1).startsWith(QU);
    }
}

Version data entries

91 entries across 91 versions & 1 rubygems

Version Path
trackler-2.2.1.104 tracks/java/exercises/pig-latin/.meta/src/reference/java/PigLatinTranslator.java
trackler-2.2.1.103 tracks/java/exercises/pig-latin/.meta/src/reference/java/PigLatinTranslator.java
trackler-2.2.1.102 tracks/java/exercises/pig-latin/.meta/src/reference/java/PigLatinTranslator.java
trackler-2.2.1.101 tracks/java/exercises/pig-latin/.meta/src/reference/java/PigLatinTranslator.java
trackler-2.2.1.100 tracks/java/exercises/pig-latin/.meta/src/reference/java/PigLatinTranslator.java
trackler-2.2.1.99 tracks/java/exercises/pig-latin/.meta/src/reference/java/PigLatinTranslator.java
trackler-2.2.1.98 tracks/java/exercises/pig-latin/.meta/src/reference/java/PigLatinTranslator.java
trackler-2.2.1.97 tracks/java/exercises/pig-latin/.meta/src/reference/java/PigLatinTranslator.java
trackler-2.2.1.96 tracks/java/exercises/pig-latin/.meta/src/reference/java/PigLatinTranslator.java
trackler-2.2.1.95 tracks/java/exercises/pig-latin/.meta/src/reference/java/PigLatinTranslator.java
trackler-2.2.1.94 tracks/java/exercises/pig-latin/.meta/src/reference/java/PigLatinTranslator.java
trackler-2.2.1.93 tracks/java/exercises/pig-latin/.meta/src/reference/java/PigLatinTranslator.java
trackler-2.2.1.92 tracks/java/exercises/pig-latin/.meta/src/reference/java/PigLatinTranslator.java
trackler-2.2.1.91 tracks/java/exercises/pig-latin/.meta/src/reference/java/PigLatinTranslator.java
trackler-2.2.1.90 tracks/java/exercises/pig-latin/.meta/src/reference/java/PigLatinTranslator.java
trackler-2.2.1.89 tracks/java/exercises/pig-latin/.meta/src/reference/java/PigLatinTranslator.java
trackler-2.2.1.88 tracks/java/exercises/pig-latin/.meta/src/reference/java/PigLatinTranslator.java
trackler-2.2.1.87 tracks/java/exercises/pig-latin/.meta/src/reference/java/PigLatinTranslator.java
trackler-2.2.1.86 tracks/java/exercises/pig-latin/.meta/src/reference/java/PigLatinTranslator.java
trackler-2.2.1.85 tracks/java/exercises/pig-latin/.meta/src/reference/java/PigLatinTranslator.java