Skip to content

Commit da01eab

Browse files
committed
1 parent 9ac9c22 commit da01eab

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

kata/7-kyu/i-before-e-except-after-c/README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@ There's a common mnemonic given to those learning English spelling which goes:
66

77
> I before E except after C
88
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 preceding the two vowels is the letter `c` in which case the `e` should go first.
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.
1011

11-
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.
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.
1214

13-
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.
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.
1417

1518
For the purposes of this kata our rule will be:
1619

17-
> 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 preceeded by a `c`, all the `e`s should come before all the `i`s.
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.
1822
1923
## Task
2024

@@ -34,4 +38,4 @@ You'll also need to account for the weirder cases that may not exist in real Eng
3438
"eeiie" --> "iieee"
3539
"cieee" --> "ceeei"
3640
"eiicieeceie" --> "iieceeiceei"
37-
```
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+
}

0 commit comments

Comments
 (0)