Преглед изворни кода

Pig-Latin Exercise Updates (#1249)

* Create version File for Pig-Latin Challenge

* Update PigLatinTranslator.java

Updated tests.

Could someone please double-check that this test class works as intended?

* Update PigLatin Tests

Build failed last time.

* Update PigLatinTranslator.java

Fixed reference solution so words with "y" after a consonant cluster are taken care of properly.

* Update PigLatinTranslatorTest.java

Fix test to match correct reference solution.

* Update PigLatinTranslator.java

Made method more efficient and fixed up formatting

* Update PigLatinTranslator.java

* Fix Character and String Comparison
Kyle P пре 8 година
родитељ
комит
d46a18f969

+ 16
- 0
exercises/pig-latin/.meta/src/reference/java/PigLatinTranslator.java Прегледај датотеку

@@ -26,6 +26,11 @@ class PigLatinTranslator {
26 26
             return word + AY;
27 27
         }
28 28
 
29
+        if (getLocationOfYAfterConsonantCluster(word) != -1) {
30
+            int yPos = getLocationOfYAfterConsonantCluster(word);
31
+            return word.substring(yPos) + word.substring(0, yPos) + AY;
32
+        }
33
+
29 34
         if (wordStartsWithPrefixes(word, THR, SCH)) {
30 35
             return word.substring(3) + word.substring(0, 3) + AY;
31 36
         }
@@ -41,6 +46,17 @@ class PigLatinTranslator {
41 46
         return word.substring(1) + word.toCharArray()[0] + AY;
42 47
     }
43 48
 
49
+    private static int getLocationOfYAfterConsonantCluster(String word) {
50
+        for (int i = 0; i < word.length() - 1; i++) {
51
+            if ((Character.toString(word.charAt(i)).matches(VOWELS_REGEX))) {
52
+                return -1;
53
+            } else if ((i != 0) && word.charAt(i) == 'y') {
54
+                return i;
55
+            }
56
+        }
57
+      return -1;
58
+    }
59
+
44 60
     private boolean wordStartsWithVowelLike(String word) {
45 61
         return word.startsWith(YT) || word.startsWith(XR) || word.substring(0, 1).matches(VOWELS_REGEX);
46 62
     }

+ 1
- 0
exercises/pig-latin/.meta/version Прегледај датотеку

@@ -0,0 +1 @@
1
+1.1.0

+ 15
- 1
exercises/pig-latin/src/test/java/PigLatinTranslatorTest.java Прегледај датотеку

@@ -23,12 +23,17 @@ public class PigLatinTranslatorTest {
23 23
                 {"object", "objectay"},
24 24
                 {"under", "underay"},
25 25
 
26
+                // Word beginning with vowel follwed by a 'qu'
27
+                {"equal", "equalay"},
28
+
26 29
                 // First letter and ay are moved to the end of words that start with consonants
27 30
                 {"pig", "igpay"},
28 31
                 {"koala", "oalakay"},
29
-                {"yellow", "ellowyay"},
30 32
                 {"xenon", "enonxay"},
31 33
 
34
+                // Word beginning with 'q' without a following 'u'
35
+                {"qat", "atqay"},
36
+
32 37
                 // Ch is treated like a single consonant
33 38
                 {"chair", "airchay"},
34 39
 
@@ -53,6 +58,15 @@ public class PigLatinTranslatorTest {
53 58
                 // Xr is treated like a single vowel
54 59
                 {"xray", "xrayay"},
55 60
 
61
+                // 'Y' is treated like a consonant at the beginning of a word
62
+                {"yellow", "ellowyay"},
63
+
64
+                // 'Y' is treated like a vowel at the end of a consonant cluster
65
+                {"rhythm", "ythmrhay"},
66
+
67
+                // 'Y' as second letter in a two letter word
68
+                {"my", "ymay"},
69
+
56 70
                 // Phrases are translated
57 71
                 {"quick fast run", "ickquay astfay unray"}
58 72
         });