diff --git a/kata/7-kyu/i-before-e-except-after-c/README.md b/kata/7-kyu/i-before-e-except-after-c/README.md new file mode 100644 index 00000000..9747b2a2 --- /dev/null +++ b/kata/7-kyu/i-before-e-except-after-c/README.md @@ -0,0 +1,41 @@ +# [I before E except after C](https://www.codewars.com/kata/i-before-e-except-after-c "https://www.codewars.com/kata/6834f1a80e0a48c2ea3feeb0") + +## Intro + +There's a common mnemonic given to those learning English spelling which goes: + +> I before E except after C + +This suggests that when you have the letters `i` and `e` next to each other in a word, the `i` should come first, with the exception that if +preceding the two vowels is the letter `c` in which case the `e` should go first. + +For example, this rule would tell you that the word for a close companion would be `"friend"` and not `"freind"`. For the `c` case, this +means that when a package is mailed to you, you will `"receive"` it rather than `"recieve"` it. + +However, besides being incorrect for many cases (my weird foreign scientist neighbor taught me a few examples), it's not clear from this +rule what to do with more unusual cases with multiple `i`s and `e`s. + +For the purposes of this kata our rule will be: + +> For any contiguous sequence of `i` or `e` characters, all the `i`s should come before all the `e`s. If, however, the sequence is +> immediately preceded by a `c`, all the `e`s should come before all the `i`s. + +## Task + +Write a function which takes in a lowercase word and applies our version of the "I before E except after C" rule. + +#### Examples + +``` +"freind" --> "friend" +"friend" --> "friend" +"recieve" --> "receive" +``` + +You'll also need to account for the weirder cases that may not exist in real English words. + +``` +"eeiie" --> "iieee" +"cieee" --> "ceeei" +"eiicieeceie" --> "iieceeiceei" +``` \ No newline at end of file diff --git a/kata/7-kyu/i-before-e-except-after-c/main/Solution.java b/kata/7-kyu/i-before-e-except-after-c/main/Solution.java new file mode 100644 index 00000000..a05e712e --- /dev/null +++ b/kata/7-kyu/i-before-e-except-after-c/main/Solution.java @@ -0,0 +1,11 @@ +import static java.util.regex.Pattern.compile; + +interface Solution { + static String iBeforeE(String s) { + return compile("c?([ie]+)").matcher(s).replaceAll(mr -> { + String es = mr.group(1).replace("i", ""); + String is = mr.group(1).replace("e", ""); + return mr.group().charAt(0) == 'c' ? "c" + es + is : is + es; + }); + } +} \ No newline at end of file diff --git a/kata/7-kyu/i-before-e-except-after-c/test/ExampleTests.java b/kata/7-kyu/i-before-e-except-after-c/test/ExampleTests.java new file mode 100644 index 00000000..fbc8e5c7 --- /dev/null +++ b/kata/7-kyu/i-before-e-except-after-c/test/ExampleTests.java @@ -0,0 +1,22 @@ +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +class ExampleTests { + @ParameterizedTest + @CsvSource(textBlock = """ + freind, friend + friend, friend + recieve, receive + feireist, fieriest + sciencier, sceinceir + deities, dieties + eeiie, iieee + cieee, ceeei + eiicieeceie, iieceeiceei + """) + void sample(String word, String expected) { + assertEquals(expected, Solution.iBeforeE(word)); + } +} \ No newline at end of file diff --git a/kata/7-kyu/index.md b/kata/7-kyu/index.md index 705a24dc..89292cfb 100644 --- a/kata/7-kyu/index.md +++ b/kata/7-kyu/index.md @@ -273,6 +273,7 @@ - [How Many Streets?](how-many-streets "66fc9ca2e6d1d0e9cc2e4a4c") - [How many urinals are free?](how-many-urinals-are-free "5e2733f0e7432a000fb5ecc4") - [Hungarian Vowel Harmony (easy)](hungarian-vowel-harmony-easy "57fd696e26b06857eb0011e7") +- [I before E except after C](i-before-e-except-after-c "6834f1a80e0a48c2ea3feeb0") - [I Will Take the Biggest One and Nothing Else](i-will-take-the-biggest-one-and-nothing-else "631082840289bf000e95a334") - [Incrementer](incrementer "590e03aef55cab099a0002e8") - [Indexed capitalization](indexed-capitalization "59cfc09a86a6fdf6df0000f1")