Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions kata/7-kyu/i-before-e-except-after-c/README.md
Original file line number Diff line number Diff line change
@@ -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"
```
11 changes: 11 additions & 0 deletions kata/7-kyu/i-before-e-except-after-c/main/Solution.java
Original file line number Diff line number Diff line change
@@ -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;
});
}
}
22 changes: 22 additions & 0 deletions kata/7-kyu/i-before-e-except-after-c/test/ExampleTests.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
1 change: 1 addition & 0 deletions kata/7-kyu/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down