Skip to content

Commit

Permalink
CaesarCipher: reduce amount of code, add test case, add YT link
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-punko committed Dec 26, 2024
1 parent d43a530 commit 0813f98
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
36 changes: 16 additions & 20 deletions src/main/java/by/andd3dfx/string/CaesarCipher.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,42 +36,38 @@
* Выходные данные:
* это яблоко красное
* </pre>
*
* @see <a href="https://youtu.be/pjQ9sYo5bVE">Video solution</a>
*/
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);
}
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/by/andd3dfx/string/CaesarCipherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("это яблоко красное");
}
}
}

0 comments on commit 0813f98

Please sign in to comment.