Kaynağa Gözat

Added exercise pig-latin

Piet van Dongen 11 yıl önce
ebeveyn
işleme
0c99bbe385

+ 1
- 0
config.json Dosyayı Görüntüle

@@ -33,6 +33,7 @@
33 33
     "simple-cipher",
34 34
     "octal",
35 35
     "luhn",
36
+    "pig-latin",
36 37
     "pascals-triangle"
37 38
   ],
38 39
   "deprecated": [

+ 11
- 0
pig-latin/build.gradle Dosyayı Görüntüle

@@ -0,0 +1,11 @@
1
+apply plugin: "java"
2
+apply plugin: "eclipse"
3
+apply plugin: "idea"
4
+
5
+repositories {
6
+  mavenCentral()
7
+}
8
+
9
+dependencies {
10
+  testCompile "junit:junit:4.10"
11
+}

+ 59
- 0
pig-latin/example.java Dosyayı Görüntüle

@@ -0,0 +1,59 @@
1
+import java.util.Arrays;
2
+import java.util.List;
3
+import java.util.stream.Collectors;
4
+
5
+public class PigLatin {
6
+
7
+    public static final String AY = "ay";
8
+    public static final String THR = "thr";
9
+    public static final String SCH = "sch";
10
+    public static final String CH = "ch";
11
+    public static final String QU = "qu";
12
+    public static final String TH = "th";
13
+    public static final String QUAY = "quay";
14
+    public static final String XR = "xr";
15
+    public static final String YT = "yt";
16
+    public static final String VOWELS_REGEX = "[aeiou]";
17
+
18
+    public static String translate(String sentence) {
19
+        List<String> translatedWords = Arrays.asList(sentence.split(" "))
20
+                .stream()
21
+                .map(x -> translateWord(x))
22
+                .collect(Collectors.toList());
23
+
24
+        return String.join(" ", translatedWords);
25
+    }
26
+
27
+    private static String translateWord(String word) {
28
+        if (wordStartsWithVowelLike(word)) {
29
+            return word + AY;
30
+        }
31
+
32
+        if (wordStartsWithPrefixes(word, THR, SCH)) {
33
+            return word.substring(3) + word.substring(0, 3) + AY;
34
+        }
35
+
36
+        if (wordStartsWithPrefixes(word, CH, QU, TH)) {
37
+            return word.substring(2) + word.substring(0, 2) + AY;
38
+        }
39
+
40
+        if (wordStartsWithConsonantAndQu(word)) {
41
+            return word.substring(3) + word.toCharArray()[0] + QUAY;
42
+        }
43
+
44
+        return word.substring(1) + word.toCharArray()[0] + AY;
45
+    }
46
+
47
+    private static boolean wordStartsWithVowelLike(String word) {
48
+        return word.startsWith(YT) || word.startsWith(XR) || word.substring(0, 1).matches(VOWELS_REGEX);
49
+    }
50
+
51
+    private static boolean wordStartsWithPrefixes(String word, String... prefixes) {
52
+        return Arrays.asList(prefixes).stream()
53
+                .anyMatch(x -> word.startsWith(x));
54
+    }
55
+
56
+    private static boolean wordStartsWithConsonantAndQu(String word) {
57
+        return word.substring(1).startsWith(QU);
58
+    }
59
+}

+ 0
- 0
pig-latin/src/main/java/.keep Dosyayı Görüntüle


+ 2
- 0
pig-latin/src/main/java/PigLatin.java Dosyayı Görüntüle

@@ -0,0 +1,2 @@
1
+public class PigLatin {
2
+}

+ 70
- 0
pig-latin/src/test/java/PigLatinTest.java Dosyayı Görüntüle

@@ -0,0 +1,70 @@
1
+import org.junit.Test;
2
+import org.junit.runner.RunWith;
3
+import org.junit.runners.Parameterized;
4
+
5
+import java.util.Arrays;
6
+import java.util.Collection;
7
+
8
+import static org.junit.Assert.assertEquals;
9
+
10
+@RunWith(Parameterized.class)
11
+public class PigLatinTest {
12
+
13
+    private String input;
14
+    private String expectedOutput;
15
+
16
+    @Parameterized.Parameters
17
+    public static Collection<Object[]> data() {
18
+        return Arrays.asList(new Object[][]{
19
+                // Ay is added to words that start with vowels
20
+                {"apple", "appleay"},
21
+                {"ear", "earay"},
22
+                {"igloo", "iglooay"},
23
+                {"object", "objectay"},
24
+                {"under", "underay"},
25
+
26
+                // First letter and ay are moved to the end of words that start with consonants
27
+                {"pig", "igpay"},
28
+                {"koala", "oalakay"},
29
+                {"yellow", "ellowyay"},
30
+                {"xenon", "enonxay"},
31
+
32
+                // Ch is treated like a single consonant
33
+                {"chair", "airchay"},
34
+
35
+                // Qu is treated like a single consonant
36
+                {"queen", "eenquay"},
37
+
38
+                // Qu and a single preceding consonant are treated like a single consonant
39
+                {"square", "aresquay"},
40
+
41
+                // Th is treated like a single consonant
42
+                {"therapy", "erapythay"},
43
+
44
+                // Thr is treated like a single consonant
45
+                {"thrush", "ushthray"},
46
+
47
+                // Sch is treated like a single consonant
48
+                {"school", "oolschay"},
49
+
50
+                // Yt is treated like a single vowel
51
+                {"yttria", "yttriaay"},
52
+
53
+                // Xr is treated like a single vowel
54
+                {"xray", "xrayay"},
55
+
56
+                // Phrases are translated
57
+                {"quick fast run", "ickquay astfay unray"}
58
+        });
59
+    }
60
+
61
+    public PigLatinTest(String input, String expectedOutput) {
62
+        this.input = input;
63
+        this.expectedOutput = expectedOutput;
64
+    }
65
+
66
+    @Test
67
+    public void test() {
68
+        assertEquals(expectedOutput, PigLatin.translate(input));
69
+    }
70
+}