Sha256: f2e8af5974a9da46b6fc7956e324577ce1efbc73bcb6135e246de1149661e77e

Contents?: true

Size: 1.91 KB

Versions: 142

Compression:

Stored size: 1.91 KB

Contents

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

public class PigLatin {

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

    public static String translate(String sentence) {
        List<String> translatedWords = Arrays.asList(sentence.split(" "))
                .stream()
                .map(x -> translateWord(x))
                .collect(Collectors.toList());

        return String.join(" ", translatedWords);
    }

    private static 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 static boolean wordStartsWithVowelLike(String word) {
        return word.startsWith(YT) || word.startsWith(XR) || word.substring(0, 1).matches(VOWELS_REGEX);
    }

    private static boolean wordStartsWithPrefixes(String word, String... prefixes) {
        return Arrays.asList(prefixes).stream()
                .anyMatch(x -> word.startsWith(x));
    }

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

Version data entries

142 entries across 142 versions & 1 rubygems

Version Path
trackler-2.0.8.42 tracks/java/exercises/pig-latin/src/example/java/PigLatin.java
trackler-2.0.8.41 tracks/java/exercises/pig-latin/src/example/java/PigLatin.java
trackler-2.0.8.40 tracks/java/exercises/pig-latin/src/example/java/PigLatin.java
trackler-2.0.8.39 tracks/java/exercises/pig-latin/src/example/java/PigLatin.java
trackler-2.0.8.38 tracks/java/exercises/pig-latin/src/example/java/PigLatin.java
trackler-2.0.8.37 tracks/java/exercises/pig-latin/src/example/java/PigLatin.java
trackler-2.0.8.36 tracks/java/exercises/pig-latin/src/example/java/PigLatin.java
trackler-2.0.8.35 tracks/java/exercises/pig-latin/src/example/java/PigLatin.java
trackler-2.0.8.34 tracks/java/exercises/pig-latin/src/example/java/PigLatin.java
trackler-2.0.8.33 tracks/java/exercises/pig-latin/src/example/java/PigLatin.java
trackler-2.0.8.32 tracks/java/exercises/pig-latin/src/example/java/PigLatin.java
trackler-2.0.8.31 tracks/java/exercises/pig-latin/src/example/java/PigLatin.java
trackler-2.0.8.30 tracks/java/exercises/pig-latin/src/example/java/PigLatin.java
trackler-2.0.8.29 tracks/java/exercises/pig-latin/src/example/java/PigLatin.java
trackler-2.0.8.28 tracks/java/exercises/pig-latin/src/example/java/PigLatin.java
trackler-2.0.8.27 tracks/java/exercises/pig-latin/src/example/java/PigLatin.java
trackler-2.0.8.26 tracks/java/exercises/pig-latin/src/example/java/PigLatin.java
trackler-2.0.8.24 tracks/java/exercises/pig-latin/src/example/java/PigLatin.java
trackler-2.0.8.23 tracks/java/exercises/pig-latin/src/example/java/PigLatin.java
trackler-2.0.8.22 tracks/java/exercises/pig-latin/src/example/java/PigLatin.java