Skip to content

Commit 1566e0e

Browse files
* docs: kata description * feat: kata/i-before-e-except-after-c --------- Co-authored-by: ParanoidUser <[email protected]>
1 parent 89fe7e1 commit 1566e0e

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# [I before E except after C](https://www.codewars.com/kata/i-before-e-except-after-c "https://www.codewars.com/kata/6834f1a80e0a48c2ea3feeb0")
2+
3+
## Intro
4+
5+
There's a common mnemonic given to those learning English spelling which goes:
6+
7+
> I before E except after C
8+
9+
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
10+
preceding the two vowels is the letter `c` in which case the `e` should go first.
11+
12+
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
13+
means that when a package is mailed to you, you will `"receive"` it rather than `"recieve"` it.
14+
15+
However, besides being incorrect for many cases (my weird foreign scientist neighbor taught me a few examples), it's not clear from this
16+
rule what to do with more unusual cases with multiple `i`s and `e`s.
17+
18+
For the purposes of this kata our rule will be:
19+
20+
> 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
21+
> immediately preceded by a `c`, all the `e`s should come before all the `i`s.
22+
23+
## Task
24+
25+
Write a function which takes in a lowercase word and applies our version of the "I before E except after C" rule.
26+
27+
#### Examples
28+
29+
```
30+
"freind" --> "friend"
31+
"friend" --> "friend"
32+
"recieve" --> "receive"
33+
```
34+
35+
You'll also need to account for the weirder cases that may not exist in real English words.
36+
37+
```
38+
"eeiie" --> "iieee"
39+
"cieee" --> "ceeei"
40+
"eiicieeceie" --> "iieceeiceei"
41+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import static java.util.regex.Pattern.compile;
2+
3+
interface Solution {
4+
static String iBeforeE(String s) {
5+
return compile("c?([ie]+)").matcher(s).replaceAll(mr -> {
6+
String es = mr.group(1).replace("i", "");
7+
String is = mr.group(1).replace("e", "");
8+
return mr.group().charAt(0) == 'c' ? "c" + es + is : is + es;
9+
});
10+
}
11+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.CsvSource;
5+
6+
class ExampleTests {
7+
@ParameterizedTest
8+
@CsvSource(textBlock = """
9+
freind, friend
10+
friend, friend
11+
recieve, receive
12+
feireist, fieriest
13+
sciencier, sceinceir
14+
deities, dieties
15+
eeiie, iieee
16+
cieee, ceeei
17+
eiicieeceie, iieceeiceei
18+
""")
19+
void sample(String word, String expected) {
20+
assertEquals(expected, Solution.iBeforeE(word));
21+
}
22+
}

kata/7-kyu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@
273273
- [How Many Streets?](how-many-streets "66fc9ca2e6d1d0e9cc2e4a4c")
274274
- [How many urinals are free?](how-many-urinals-are-free "5e2733f0e7432a000fb5ecc4")
275275
- [Hungarian Vowel Harmony (easy)](hungarian-vowel-harmony-easy "57fd696e26b06857eb0011e7")
276+
- [I before E except after C](i-before-e-except-after-c "6834f1a80e0a48c2ea3feeb0")
276277
- [I Will Take the Biggest One and Nothing Else](i-will-take-the-biggest-one-and-nothing-else "631082840289bf000e95a334")
277278
- [Incrementer](incrementer "590e03aef55cab099a0002e8")
278279
- [Indexed capitalization](indexed-capitalization "59cfc09a86a6fdf6df0000f1")

0 commit comments

Comments
 (0)