Skip to content

Commit 0813f98

Browse files
committed
CaesarCipher: reduce amount of code, add test case, add YT link
1 parent d43a530 commit 0813f98

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ they contain enough code which describes implementation in a natural way.
259259
| Как скачать видео с Boosty | [Youtube](https://youtu.be/b3ox1_xEx4U) | [Boosty](https://boosty.to/andd3dfx) |
260260
| Прохождение теста подтверждения практического навыка "средний" по Java на hh.ru | [Youtube](https://youtu.be/ja4nLzZSj3s) | - |
261261
| Прохождение теста подтверждения практического навыка "продвинутый" по Java на hh.ru | [Youtube](https://youtu.be/ce3g0nIJl24) | - |
262+
| Декодирование шифра Цезаря | [Youtube](https://youtu.be/pjQ9sYo5bVE) | [Code](src/main/java/by/andd3dfx/string/CaesarCipher.java) |
262263

263264
## Materials & notes
264265

src/main/java/by/andd3dfx/string/CaesarCipher.java

+16-20
Original file line numberDiff line numberDiff line change
@@ -36,42 +36,38 @@
3636
* Выходные данные:
3737
* это яблоко красное
3838
* </pre>
39+
*
40+
* @see <a href="https://youtu.be/pjQ9sYo5bVE">Video solution</a>
3941
*/
4042
public class CaesarCipher {
4143

4244
// Русский алфавит
4345
private static final String ALPHABET = "абвгдеёжзийклмнопрстуфхцчшщъыьэюя";
4446

4547
public String encode(String text, int shift) {
46-
var words = text.split(" ");
47-
return Arrays.stream(words)
48-
.map(word -> encodeWord(word, shift))
49-
.collect(Collectors.joining(" "));
48+
return encodeWordWithSpacesSupport(text, shift);
5049
}
5150

52-
private String encodeWord(String text, int shift) {
53-
var chars = text.toCharArray();
54-
for (var i = 0; i < chars.length; i++) {
55-
var targetIndex = ALPHABET.indexOf(chars[i]) + shift;
56-
targetIndex %= ALPHABET.length();
57-
chars[i] = ALPHABET.charAt(targetIndex);
58-
}
59-
return new String(chars);
51+
public String decode(String text, int shift) {
52+
return encodeWordWithSpacesSupport(text, -shift);
6053
}
6154

62-
public String decode(String encryptedText, int shift) {
63-
var words = encryptedText.split(" ");
55+
private String encodeWordWithSpacesSupport(String text, int shift) {
56+
var words = text.split(" ");
6457
return Arrays.stream(words)
65-
.map(word -> decodeWord(word, shift))
58+
.map(word -> encodeWord(word, shift))
6659
.collect(Collectors.joining(" "));
6760
}
6861

69-
private String decodeWord(String encryptedText, int shift) {
70-
var chars = encryptedText.toCharArray();
62+
private String encodeWord(String word, int shift) {
63+
var chars = word.toCharArray();
7164
for (var i = 0; i < chars.length; i++) {
72-
var targetIndex = ALPHABET.indexOf(chars[i]) - shift + ALPHABET.length();
73-
targetIndex %= ALPHABET.length();
74-
chars[i] = ALPHABET.charAt(targetIndex);
65+
var index = ALPHABET.indexOf(chars[i]) + shift;
66+
while (index < 0) {
67+
index += ALPHABET.length();
68+
}
69+
index %= ALPHABET.length();
70+
chars[i] = ALPHABET.charAt(index);
7571
}
7672
return new String(chars);
7773
}

src/test/java/by/andd3dfx/string/CaesarCipherTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ public void setUp() throws Exception {
1616

1717
@Test
1818
public void encode() {
19+
assertThat(caesarCipher.encode("андрей", 0)).isEqualTo("андрей");
1920
assertThat(caesarCipher.encode("абвгде", 1)).isEqualTo("бвгдеё");
2021
assertThat(caesarCipher.encode("это яблоко красное", 7)).isEqualTo("дщх ёзтхсх счжшфхл");
2122
}
2223

2324
@Test
2425
public void decode() {
26+
assertThat(caesarCipher.decode("андрей", 0)).isEqualTo("андрей");
2527
assertThat(caesarCipher.decode("бвгдеё", 1)).isEqualTo("абвгде");
2628
assertThat(caesarCipher.decode("дщх ёзтхсх счжшфхл", 7)).isEqualTo("это яблоко красное");
2729
}
28-
}
30+
}

0 commit comments

Comments
 (0)