diff --git a/README.md b/README.md index 72bac14..5031f30 100644 --- a/README.md +++ b/README.md @@ -259,6 +259,7 @@ they contain enough code which describes implementation in a natural way. | Как скачать видео с Boosty | [Youtube](https://youtu.be/b3ox1_xEx4U) | [Boosty](https://boosty.to/andd3dfx) | | Прохождение теста подтверждения практического навыка "средний" по Java на hh.ru | [Youtube](https://youtu.be/ja4nLzZSj3s) | - | | Прохождение теста подтверждения практического навыка "продвинутый" по Java на hh.ru | [Youtube](https://youtu.be/ce3g0nIJl24) | - | +| Декодирование шифра Цезаря | [Youtube](https://youtu.be/pjQ9sYo5bVE) | [Code](src/main/java/by/andd3dfx/string/CaesarCipher.java) | ## Materials & notes diff --git a/src/main/java/by/andd3dfx/string/CaesarCipher.java b/src/main/java/by/andd3dfx/string/CaesarCipher.java index 6ec36f8..6e0551a 100644 --- a/src/main/java/by/andd3dfx/string/CaesarCipher.java +++ b/src/main/java/by/andd3dfx/string/CaesarCipher.java @@ -36,6 +36,8 @@ * Выходные данные: * это яблоко красное * + * + * @see Video solution */ public class CaesarCipher { @@ -43,35 +45,29 @@ public class CaesarCipher { private static final String ALPHABET = "абвгдеёжзийклмнопрстуфхцчшщъыьэюя"; public String encode(String text, int shift) { - var words = text.split(" "); - return Arrays.stream(words) - .map(word -> encodeWord(word, shift)) - .collect(Collectors.joining(" ")); + return encodeWordWithSpacesSupport(text, shift); } - private String encodeWord(String text, int shift) { - var chars = text.toCharArray(); - for (var i = 0; i < chars.length; i++) { - var targetIndex = ALPHABET.indexOf(chars[i]) + shift; - targetIndex %= ALPHABET.length(); - chars[i] = ALPHABET.charAt(targetIndex); - } - return new String(chars); + public String decode(String text, int shift) { + return encodeWordWithSpacesSupport(text, -shift); } - public String decode(String encryptedText, int shift) { - var words = encryptedText.split(" "); + private String encodeWordWithSpacesSupport(String text, int shift) { + var words = text.split(" "); return Arrays.stream(words) - .map(word -> decodeWord(word, shift)) + .map(word -> encodeWord(word, shift)) .collect(Collectors.joining(" ")); } - private String decodeWord(String encryptedText, int shift) { - var chars = encryptedText.toCharArray(); + private String encodeWord(String word, int shift) { + var chars = word.toCharArray(); for (var i = 0; i < chars.length; i++) { - var targetIndex = ALPHABET.indexOf(chars[i]) - shift + ALPHABET.length(); - targetIndex %= ALPHABET.length(); - chars[i] = ALPHABET.charAt(targetIndex); + var index = ALPHABET.indexOf(chars[i]) + shift; + while (index < 0) { + index += ALPHABET.length(); + } + index %= ALPHABET.length(); + chars[i] = ALPHABET.charAt(index); } return new String(chars); } diff --git a/src/test/java/by/andd3dfx/string/CaesarCipherTest.java b/src/test/java/by/andd3dfx/string/CaesarCipherTest.java index 64e6e6d..cf5bae5 100644 --- a/src/test/java/by/andd3dfx/string/CaesarCipherTest.java +++ b/src/test/java/by/andd3dfx/string/CaesarCipherTest.java @@ -16,13 +16,15 @@ public void setUp() throws Exception { @Test public void encode() { + assertThat(caesarCipher.encode("андрей", 0)).isEqualTo("андрей"); assertThat(caesarCipher.encode("абвгде", 1)).isEqualTo("бвгдеё"); assertThat(caesarCipher.encode("это яблоко красное", 7)).isEqualTo("дщх ёзтхсх счжшфхл"); } @Test public void decode() { + assertThat(caesarCipher.decode("андрей", 0)).isEqualTo("андрей"); assertThat(caesarCipher.decode("бвгдеё", 1)).isEqualTo("абвгде"); assertThat(caesarCipher.decode("дщх ёзтхсх счжшфхл", 7)).isEqualTo("это яблоко красное"); } -} \ No newline at end of file +}