Skip to content

Commit d46a18f

Browse files
Kyle-PuFridaTveit
authored andcommitted
Pig-Latin Exercise Updates (exercism#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
1 parent 71a1fcb commit d46a18f

3 files changed

Lines changed: 32 additions & 1 deletion

File tree

‎exercises/pig-latin/.meta/src/reference/java/PigLatinTranslator.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ private String translateWord(String word) {
2626
return word + AY;
2727
}
2828

29+
if (getLocationOfYAfterConsonantCluster(word) != -1) {
30+
int yPos = getLocationOfYAfterConsonantCluster(word);
31+
return word.substring(yPos) + word.substring(0, yPos) + AY;
32+
}
33+
2934
if (wordStartsWithPrefixes(word, THR, SCH)) {
3035
return word.substring(3) + word.substring(0, 3) + AY;
3136
}
@@ -41,6 +46,17 @@ private String translateWord(String word) {
4146
return word.substring(1) + word.toCharArray()[0] + AY;
4247
}
4348

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

‎exercises/pig-latin/.meta/version‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.1.0

‎exercises/pig-latin/src/test/java/PigLatinTranslatorTest.java‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@ public static Collection<Object[]> data() {
2323
{"object", "objectay"},
2424
{"under", "underay"},
2525

26+
// Word beginning with vowel follwed by a 'qu'
27+
{"equal", "equalay"},
28+
2629
// First letter and ay are moved to the end of words that start with consonants
2730
{"pig", "igpay"},
2831
{"koala", "oalakay"},
29-
{"yellow", "ellowyay"},
3032
{"xenon", "enonxay"},
3133

34+
// Word beginning with 'q' without a following 'u'
35+
{"qat", "atqay"},
36+
3237
// Ch is treated like a single consonant
3338
{"chair", "airchay"},
3439

@@ -53,6 +58,15 @@ public static Collection<Object[]> data() {
5358
// Xr is treated like a single vowel
5459
{"xray", "xrayay"},
5560

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+
5670
// Phrases are translated
5771
{"quick fast run", "ickquay astfay unray"}
5872
});

0 commit comments

Comments
 (0)