Skip to content

Commit

Permalink
Remove extra code from CaesarCipher solution. Swap lines in README
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-punko committed Dec 27, 2024
1 parent 0813f98 commit f2d09b9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ they contain enough code which describes implementation in a natural way.
| Ревью кода из интервью 7 (LIVE) | [Youtube](https://youtu.be/qvyJ0rxXcg0) | - |
| Как скачать видео с 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) |
| Прохождение теста подтверждения практического навыка "продвинутый" по Java на hh.ru | [Youtube](https://youtu.be/ce3g0nIJl24) | - |

## Materials & notes

Expand Down
17 changes: 7 additions & 10 deletions src/main/java/by/andd3dfx/string/CaesarCipher.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,20 @@ public class CaesarCipher {
private static final String ALPHABET = "абвгдеёжзийклмнопрстуфхцчшщъыьэюя";

public String encode(String text, int shift) {
return encodeWordWithSpacesSupport(text, shift);
return encodeInner(text, shift);
}

public String decode(String text, int shift) {
return encodeWordWithSpacesSupport(text, -shift);
return encodeInner(text, -shift);
}

private String encodeWordWithSpacesSupport(String text, int shift) {
var words = text.split(" ");
return Arrays.stream(words)
.map(word -> encodeWord(word, shift))
.collect(Collectors.joining(" "));
}

private String encodeWord(String word, int shift) {
private String encodeInner(String word, int shift) {
var chars = word.toCharArray();
for (var i = 0; i < chars.length; i++) {
if (chars[i] == ' ') {
continue;
}

var index = ALPHABET.indexOf(chars[i]) + shift;
while (index < 0) {
index += ALPHABET.length();
Expand Down

0 comments on commit f2d09b9

Please sign in to comment.