Skip to content

Commit 02d488e

Browse files
Updating Squeaky Clean (#2664)
* Updating Squeaky Clean Reformating exercise to not use unicode, control characters and greek letters Updating docs and tests, and meta reference * Adding extra exercise to convert leetspeak into normal text * Applying suggestions * Update exercises/concept/squeaky-clean/src/test/java/SqueakyCleanTest.java Co-authored-by: Sander Ploegsma <[email protected]> * Update exercises/concept/squeaky-clean/.docs/instructions.md Co-authored-by: Sander Ploegsma <[email protected]> * Add myself as contributor * Add myself as contributor --------- Co-authored-by: Sander Ploegsma <[email protected]>
1 parent 24ab215 commit 02d488e

File tree

7 files changed

+72
-65
lines changed

7 files changed

+72
-65
lines changed

concepts/chars/introduction.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ The Java `char` type represents the smallest addressable components of text.
44
Multiple `char`s can comprise a string such as `"word"` or `char`s can be processed independently.
55
Their literals have single quotes e.g. `'A'`.
66

7-
Java `char`s support Unicode encoding so in addition to the Latin character set pretty much all the writing systems in use worldwide can be represented, e.g. the Greek letter `'β'`.
8-
97
There are many builtin library methods to inspect and manipulate `char`s.
108
These can be found as static methods of the `java.lang.Character` class.
119

exercises/concept/squeaky-clean/.docs/hints.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,23 @@
99
- See [this method][iswhitespace] for detecting spaces. Remember it is a static method.
1010
- `char` literals are enclosed in single quotes.
1111

12-
## 2. Replace control characters with the upper case string "CTRL"
13-
14-
- See [this method][iscontrol] to check if a character is a control character.
15-
16-
## 3. Convert kebab-case to camel-case
12+
## 2. Convert kebab-case to camel-case
1713

1814
- See [this method][toupper] to convert a character to upper case.
1915

20-
## 4. Omit characters that are not letters
16+
## 3. Convert leetspeak to normal text
2117

22-
- See [this method][isLetter] to check if a character is a letter.
18+
- See [this method][isdigit] for detecting numbers.
2319

24-
## 5. Omit Greek lower case letters
20+
## 4. Omit characters that are not letters
2521

26-
- `char`s support the default equality and comparison operators.
22+
- See [this method][isletter] to check if a character is a letter.
2723

2824
[chars-docs]: https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/Character.html
2925
[chars-tutorial]: https://docs.oracle.com/javase/tutorial/java/data/characters.html
3026
[char-at]: https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/String.html#charAt(int)
3127
[string-builder]: https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/StringBuilder.html
3228
[iswhitespace]: https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/Character.html#isWhitespace(char)
33-
[iscontrol]: https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/Character.html#isISOControl(char)
3429
[toupper]: https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/Character.html#toUpperCase(char)
35-
[isLetter]: https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/Character.html#isLetter(char)
30+
[isletter]: https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/Character.html#isLetter(char)
31+
[isdigit]: https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/Character.html#isDigit(char)

exercises/concept/squeaky-clean/.docs/instructions.md

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,32 @@ SqueakyClean.clean("my Id");
1717
// => "my___Id"
1818
```
1919

20-
## 2. Replace control characters with the upper case string "CTRL"
20+
## 2. Convert kebab-case to camelCase
2121

22-
Modify the (_static_) `SqueakyClean.clean()` method to replace control characters with the upper case string `"CTRL"`.
22+
Modify the (_static_) `SqueakyClean.clean()` method to convert kebab-case to camelCase.
2323

2424
```java
25-
SqueakyClean.clean("my\0Id");
26-
// => "myCTRLId",
25+
SqueakyClean.clean("a-bc");
26+
// => "aBc"
2727
```
2828

29-
## 3. Convert kebab-case to camelCase
29+
## 3. Convert leetspeak to normal text
3030

31-
Modify the (_static_) `SqueakyClean.clean()` method to convert kebab-case to camelCase.
31+
Modify the (_static_) `SqueakyClean.clean()` method to convert [leetspeak][leet-speak] to normal text.
32+
For simplicity we will only be using `4`, `3`, `0`, `1` and `7` from the table.
3233

3334
```java
34-
SqueakyClean.clean("à-ḃç");
35-
// => "àḂç"
35+
SqueakyClean.clean("H3ll0 W0rld");
36+
// => "Hello_World"
3637
```
3738

3839
## 4. Omit characters that are not letters
3940

4041
Modify the (_static_) `SqueakyClean.clean()` method to omit any characters that are not letters.
4142

4243
```java
43-
SqueakyClean.clean("a1😀2😀3😀b");
44+
SqueakyClean.clean("a$#.b");
4445
// => "ab"
4546
```
4647

47-
## 5. Omit Greek lower case letters
48-
49-
Modify the (_static_) `SqueakyClean.clean()` method to omit any Greek letters in the range 'α' to 'ω'.
50-
51-
```java
52-
SqueakyClean.clean("MyΟβιεγτFinder");
53-
// => "MyΟFinder"
54-
```
48+
[leet-speak]: https://en.wikipedia.org/wiki/Leet

exercises/concept/squeaky-clean/.docs/introduction.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ The Java `char` type represents the smallest addressable components of text.
66
Multiple `char`s can comprise a string such as `"word"` or `char`s can be processed independently.
77
Their literals have single quotes e.g. `'A'`.
88

9-
Java `char`s support Unicode encoding so in addition to the Latin character set pretty much all the writing systems in use worldwide can be represented, e.g. the Greek letter `'β'`.
10-
119
There are many builtin library methods to inspect and manipulate `char`s.
1210
These can be found as static methods of the `java.lang.Character` class.
1311

exercises/concept/squeaky-clean/.meta/config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
"authors": [
33
"ystromm"
44
],
5+
"contributors": [
6+
"sanderploegsma",
7+
"manumafe98"
8+
],
59
"files": {
610
"solution": [
711
"src/main/java/SqueakyClean.java"
Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
11
class SqueakyClean {
2+
3+
private static char replaceDigit(char digit) {
4+
if (digit == '3') {
5+
return 'e';
6+
}
7+
8+
if (digit == '4') {
9+
return 'a';
10+
}
11+
12+
if (digit == '0') {
13+
return 'o';
14+
}
15+
16+
if (digit == '1') {
17+
return 'l';
18+
}
19+
return 't';
20+
}
21+
222
static String clean(String identifier) {
323
final StringBuilder cleanIdentifier = new StringBuilder();
424
boolean kebab = false;
525
for (int i = 0; i < identifier.length(); i++) {
626
final char ch = identifier.charAt(i);
727
if (Character.isSpaceChar(ch)) {
828
cleanIdentifier.append("_");
9-
} else if (Character.isISOControl(ch)) {
10-
cleanIdentifier.append("CTRL");
29+
} else if (Character.isDigit(ch)) {
30+
cleanIdentifier.append(SqueakyClean.replaceDigit(ch));
1131
} else if (ch == '-') {
1232
kebab = true;
13-
} else if (isLetter(ch)) {
33+
} else if (Character.isLetter(ch)) {
1434
cleanIdentifier.append(
1535
kebab ? Character.toUpperCase(ch) : ch
1636
);
@@ -19,7 +39,4 @@ static String clean(String identifier) {
1939
}
2040
return cleanIdentifier.toString();
2141
}
22-
private static boolean isLetter(char ch) {
23-
return Character.isLetter(ch) && !(ch >= 'α' && ch <= 'ω');
24-
}
2542
}

exercises/concept/squeaky-clean/src/test/java/SqueakyCleanTest.java

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void single_letter() {
2424
@Tag("task:1")
2525
@DisplayName("The clean method returns the same string when invoked on a string of three letters")
2626
public void string() {
27-
assertThat(SqueakyClean.clean("àḃç")).isEqualTo("àḃç");
27+
assertThat(SqueakyClean.clean("abc")).isEqualTo("abc");
2828
}
2929

3030
@Test
@@ -43,50 +43,50 @@ public void leading_and_trailing_spaces() {
4343

4444
@Test
4545
@Tag("task:2")
46-
@DisplayName("The clean method replaces control characters with CTRL")
47-
public void ctrl() {
48-
assertThat(SqueakyClean.clean("my\0\r\u007FId")).isEqualTo("myCTRLCTRLCTRLId");
46+
@DisplayName("The clean method converts kebab to camel case after removing a dash")
47+
public void kebab_to_camel_case() {
48+
assertThat(SqueakyClean.clean("a-bc")).isEqualTo("aBc");
4949
}
5050

5151
@Test
52-
@Tag("task:4")
53-
@DisplayName("The clean method returns an empty string when invoked on a string with no letters")
54-
public void string_with_no_letters() {
55-
assertThat(SqueakyClean.clean("\uD83D\uDE00\uD83D\uDE00\uD83D\uDE00")).isEmpty();
52+
@Tag("task:2")
53+
@DisplayName("The clean method returns a string in camel case after removing a dash and replaces a whitespace")
54+
public void kebab_to_camel_case_and_number() {
55+
assertThat(SqueakyClean.clean("a-C ")).isEqualTo("aC_");
5656
}
5757

5858
@Test
59-
@Tag("task:3")
60-
@DisplayName("The clean method converts kebab to camel case after removing a dash")
61-
public void kebab_to_camel_case() {
62-
assertThat(SqueakyClean.clean("à-ḃç")).isEqualTo("àḂç");
59+
@Tag("task:2")
60+
@DisplayName("The clean method returns a string in camel case and replaces leading and trailing whitespaces")
61+
public void kebab_to_camel_case_and_spaces() {
62+
assertThat(SqueakyClean.clean(" hello-world ")).isEqualTo("_helloWorld_");
6363
}
6464

6565
@Test
6666
@Tag("task:3")
67-
@DisplayName("The clean method returns a string in camel case after removing a dash and a number")
68-
public void kebab_to_camel_case_no_letter() {
69-
assertThat(SqueakyClean.clean("a-1C")).isEqualTo("aC");
67+
@DisplayName("The clean method converts leetspeak to normal text after replacing numbers with chars")
68+
public void leetspeak_to_normal_text() {
69+
assertThat(SqueakyClean.clean("H3ll0 W0rld")).isEqualTo("Hello_World");
7070
}
7171

7272
@Test
73-
@Tag("task:4")
74-
@DisplayName("The clean method removes all characters that are not letters")
75-
public void keep_only_letters() {
76-
assertThat(SqueakyClean.clean("a1\uD83D\uDE002\uD83D\uDE003\uD83D\uDE00b")).isEqualTo("ab");
73+
@Tag("task:3")
74+
@DisplayName("The clean method converts leetspeak to normal text with spaces and special characters")
75+
public void leetspeak_to_normal_text_with_spaces_and_special_characters() {
76+
assertThat(SqueakyClean.clean("¡1337sp34k is fun!")).isEqualTo("leetspeak_is_fun");
7777
}
7878

7979
@Test
80-
@Tag("task:5")
81-
@DisplayName("The clean method removes all lowercase greek letters")
82-
public void omit_lower_case_greek_letters() {
83-
assertThat(SqueakyClean.clean("MyΟβιεγτFinder")).isEqualTo("MyΟFinder");
80+
@Tag("task:4")
81+
@DisplayName("The clean method removes all characters that are not letters")
82+
public void special_characters() {
83+
assertThat(SqueakyClean.clean("a$#.b")).isEqualTo("ab");
8484
}
8585

8686
@Test
87-
@Tag("task:5")
88-
@DisplayName("The clean method returns the correct result after performing a few cleaning operations")
89-
public void combine_conversions() {
90-
assertThat(SqueakyClean.clean("9 -abcĐ\uD83D\uDE00ω\0")).isEqualTo("_AbcĐCTRL");
87+
@Tag("task:4")
88+
@DisplayName("The clean method removes all characters that are not letters and replaces spaces")
89+
public void special_characters_and_spaces() {
90+
assertThat(SqueakyClean.clean("¡hello world!. ")).isEqualTo("hello_world_");
9191
}
9292
}

0 commit comments

Comments
 (0)