Strings
Ciphers
Fundamentals
Cryptography
Algorithms
import java.util.List; import java.util.stream.Collectors; public class Kata { // <<-CONSTANTS->> private static final int UpperCase_LOWER_LIMIT = 65; private static final int UpperCase_UPPER_LIMIT = 90; private static final int LowerCase_LOWER_LIMIT = 97; private static final int LowerCase_UPPER_LIMIT = 122; private static final int INTERMEDIATE_OFFSET = LowerCase_LOWER_LIMIT - UpperCase_UPPER_LIMIT - 1; // <<-METHODS->> private static int getLastDigit(final int digit) { String str = String.valueOf(digit); str = str.substring(str.length() - 1, str.length()); return Integer.valueOf(str); } private static List<Integer> getEncryptationOffsets(String key) { return key.chars() .map(n -> getLastDigit(n) + key.length()) .boxed() .toList(); } private static List<Integer> getPhraseChars(String phrase) { return phrase.chars() .boxed() .collect(Collectors.toList()); } private static String parseChars(List<Integer> traduction) { return traduction.stream() .map(n -> (char) n.intValue()) .map(c -> Character.toString(c)) .collect(Collectors.joining()); } public static String encode(String phrase, String key) { if (phrase == null || phrase.isEmpty()) return ""; if (key == null || key.isEmpty()) return phrase; List<Integer> offsets = getEncryptationOffsets(key); List<Integer> phraseChars = getPhraseChars(phrase); List<Integer> traduction = new java.util.ArrayList<>(); for (int i = 0; i < phrase.length(); i++) { int keyIndex = i % offsets.size(); int current = phraseChars.get(i); int result = current + offsets.get(keyIndex); if (current <= UpperCase_UPPER_LIMIT && result >= LowerCase_LOWER_LIMIT) { int offset = result - UpperCase_UPPER_LIMIT; result = LowerCase_LOWER_LIMIT + offset - 1; } if (result > UpperCase_UPPER_LIMIT && result < LowerCase_LOWER_LIMIT) { result += INTERMEDIATE_OFFSET; } else if (result > LowerCase_UPPER_LIMIT) { result = result - LowerCase_UPPER_LIMIT + UpperCase_LOWER_LIMIT - 1; } traduction.add(result); } return parseChars(traduction); } public static String decode(String phrase, String key) { if (phrase == null || phrase.isEmpty()) return ""; if (key == null || key.isEmpty()) return phrase; List<Integer> offsets = getEncryptationOffsets(key); List<Integer> phraseChars = getPhraseChars(phrase); List<Integer> traduction = new java.util.ArrayList<>(); for (int i = 0; i < phrase.length(); i++) { int keyIndex = i % offsets.size(); int current = phraseChars.get(i); int result = current - offsets.get(keyIndex); if (current >= LowerCase_LOWER_LIMIT && result <= UpperCase_UPPER_LIMIT) { int offset = LowerCase_LOWER_LIMIT - result; result = UpperCase_UPPER_LIMIT - offset + 1; } if (result < LowerCase_LOWER_LIMIT && result > UpperCase_UPPER_LIMIT) { result -= INTERMEDIATE_OFFSET; } else if (result < UpperCase_LOWER_LIMIT) { int offset = UpperCase_LOWER_LIMIT - result; result = LowerCase_UPPER_LIMIT - offset + 1; } traduction.add(result); } return parseChars(traduction); } }
- import java.util.List;
- import java.util.stream.Collectors;
- public class Kata {
// <<-CONSTANTS->>- // <<-CONSTANTS->>
- private static final int UpperCase_LOWER_LIMIT = 65;
- private static final int UpperCase_UPPER_LIMIT = 90;
- private static final int LowerCase_LOWER_LIMIT = 97;
- private static final int LowerCase_UPPER_LIMIT = 122;
- private static final int INTERMEDIATE_OFFSET = LowerCase_LOWER_LIMIT - UpperCase_UPPER_LIMIT - 1;
- // <<-METHODS->>
- private static int getLastDigit(final int digit) {
- String str = String.valueOf(digit);
- str = str.substring(str.length() - 1, str.length());
- return Integer.valueOf(str);
- }
- private static List<Integer> getEncryptationOffsets(String key) {
- return key.chars()
- .map(n -> getLastDigit(n) + key.length())
- .boxed()
- .toList();
- }
- private static List<Integer> getPhraseChars(String phrase) {
- return phrase.chars()
- .boxed()
- .collect(Collectors.toList());
- }
private static String parseChars(List<Integer> translation) {return translation.stream()- private static String parseChars(List<Integer> traduction) {
- return traduction.stream()
- .map(n -> (char) n.intValue())
- .map(c -> Character.toString(c))
- .collect(Collectors.joining());
- }
- public static String encode(String phrase, String key) {
if (phrase == null || phrase.isEmpty() || key == null) return "";if (key.isEmpty()) return phrase;- if (phrase == null || phrase.isEmpty()) return "";
- if (key == null || key.isEmpty()) return phrase;
- List<Integer> offsets = getEncryptationOffsets(key);
- List<Integer> phraseChars = getPhraseChars(phrase);
List<Integer> translation = new java.util.ArrayList<>();- List<Integer> traduction = new java.util.ArrayList<>();
int keyIndex = 0;- for (int i = 0; i < phrase.length(); i++) {
keyIndex = i % offsets.size();- int keyIndex = i % offsets.size();
- int current = phraseChars.get(i);
- int result = current + offsets.get(keyIndex);
- if (current <= UpperCase_UPPER_LIMIT && result >= LowerCase_LOWER_LIMIT) {
- int offset = result - UpperCase_UPPER_LIMIT;
- result = LowerCase_LOWER_LIMIT + offset - 1;
- }
- if (result > UpperCase_UPPER_LIMIT && result < LowerCase_LOWER_LIMIT) {
- result += INTERMEDIATE_OFFSET;
- } else if (result > LowerCase_UPPER_LIMIT) {
- result = result - LowerCase_UPPER_LIMIT + UpperCase_LOWER_LIMIT - 1;
- }
translation.add(result);keyIndex++;- traduction.add(result);
- }
return parseChars(translation);- return parseChars(traduction);
- }
- public static String decode(String phrase, String key) {
if (phrase == null || phrase.isEmpty() || key == null) return "";if (key.isEmpty()) return phrase;- if (phrase == null || phrase.isEmpty()) return "";
- if (key == null || key.isEmpty()) return phrase;
- List<Integer> offsets = getEncryptationOffsets(key);
- List<Integer> phraseChars = getPhraseChars(phrase);
List<Integer> translation = new java.util.ArrayList<>();- List<Integer> traduction = new java.util.ArrayList<>();
int keyIndex = 0;- for (int i = 0; i < phrase.length(); i++) {
keyIndex = i % offsets.size();- int keyIndex = i % offsets.size();
- int current = phraseChars.get(i);
- int result = current - offsets.get(keyIndex);
- if (current >= LowerCase_LOWER_LIMIT && result <= UpperCase_UPPER_LIMIT) {
- int offset = LowerCase_LOWER_LIMIT - result;
- result = UpperCase_UPPER_LIMIT - offset + 1;
- }
- if (result < LowerCase_LOWER_LIMIT && result > UpperCase_UPPER_LIMIT) {
- result -= INTERMEDIATE_OFFSET;
- } else if (result < UpperCase_LOWER_LIMIT) {
- int offset = UpperCase_LOWER_LIMIT - result;
- result = LowerCase_UPPER_LIMIT - offset + 1;
- }
translation.add(result);keyIndex++;- traduction.add(result);
- }
return parseChars(translation);- return parseChars(traduction);
- }
- }
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; import java.util.List; import java.util.Random; import java.util.stream.Collectors; class SolutionTest { @Test void basicTest() { assertEquals("moqik", Kata.encode("abcde", "abcde")); assertEquals("abcde", Kata.decode("moqik", "abcde")); assertEquals("KMOQS", Kata.encode("ABCDE", "ABCDE")); assertEquals("ABCDE", Kata.decode("KMOQS", "ABCDE")); assertEquals("GIFUKTG", Kata.encode("DEARGOD", "FGH")); assertEquals("DEARGOD", Kata.decode("GIFUKTG", "FGH")); assertEquals("ikhwmvi", Kata.encode("deargod", "fgh")); assertEquals("deargod", Kata.decode("ikhwmvi", "fgh")); assertEquals("IQlOirL", Kata.encode("ALaBamA", "hoME")); assertEquals("ALaBamA", Kata.decode("IQlOirL", "hoME")); } @Test void specialCasesTest() { assertEquals("", Kata.encode("", "")); assertEquals("TernaryLove", Kata.encode("TernaryLove", "")); assertEquals("", Kata.encode(null, "abcde")); assertEquals("", Kata.encode("", null)); } @Test void fullTestSimpleOverflow() { assertEquals("CEEpB", Kata.encode("turbo", "TUXON")); assertEquals("turbo", Kata.decode("CEEpB", "TUXON")); assertEquals("skstzCvzwj", Kata.encode("helloworld", "afgh")); assertEquals("helloworld", Kata.decode("skstzCvzwj", "afgh")); assertEquals("IKM", Kata.encode("xyz", "ijklmn")); assertEquals("xyz", Kata.decode("IKM", "ijklmn")); assertEquals("qUlWuZXovQ", Kata.encode("gOdMoRNinG", "uSA")); assertEquals("gOdMoRNinG", Kata.decode("qUlWuZXovQ", "uSA")); assertEquals("BOywIJdrzafbLMKuCnVqNt", Kata.encode("wElovETerNaRyExpsaNdIj", "dAvID")); assertEquals("wElovETerNaRyExpsaNdIj", Kata.decode("BOywIJdrzafbLMKuCnVqNt", "dAvID")); } @Test void fullTestIntermediateOverflow() { assertEquals("rhCRDjxXEVyfu", Kata.encode("hOmEsWeEtHoMe", "nOtSoSwEeT")); assertEquals("hOmEsWeEtHoMe", Kata.decode("rhCRDjxXEVyfu", "nOtSoSwEeT")); assertEquals("cyvNMxgiVt", Kata.encode("WonDErWaLl", "oASiS")); assertEquals("WonDErWaLl", Kata.decode("cyvNMxgiVt", "oASiS")); assertEquals("lyynmpA", Kata.encode("Zumbido", "wow")); assertEquals("Zumbido", Kata.decode("lyynmpA", "wow")); } private Random random = new Random(); String generateString(int lowerBound, int upperBound) { return java.util.stream.IntStream.rangeClosed(random.nextInt(lowerBound + 1), random.nextInt(upperBound + 1)) .mapToObj(i -> random.nextInt(2) == 0 ? random.nextInt(65, 91) : random.nextInt(97, 123)) .collect(StringBuilder::new, (sb, code) -> sb.append((char) code.intValue()), StringBuilder::append) .toString(); } String generatePhrase(int lowerBound, int upperBound) { return generateString(lowerBound, upperBound); } String generateKey(int lowerBound, int upperBound) { return generateString(lowerBound, upperBound); } @RepeatedTest(1500) void randomEasyModeTest() { String phrase = generatePhrase(0, 100); String key = generateKey(0, 100); assertEquals(Kata.encode(phrase, key), encode(phrase, key)); assertEquals(Kata.decode(phrase, key), decode(phrase, key)); } @RepeatedTest(1000) void randomHardModeTest() { String phrase = generatePhrase(1000, 15000); String key = generateKey(1000, 15000); assertEquals(Kata.encode(phrase, key), encode(phrase, key)); assertEquals(Kata.decode(phrase, key), decode(phrase, key)); } @RepeatedTest(50) void randomGodModeTest() { String phrase = generatePhrase(89000, 90000); String key = generateKey(79000, 80000); assertEquals(Kata.encode(phrase, key), encode(phrase, key)); assertEquals(Kata.decode(phrase, key), decode(phrase, key)); } // [[ A N S W E R ]] // <<-CONSTANTS->> private static final int UpperCase_LOWER_LIMIT = 65; private static final int UpperCase_UPPER_LIMIT = 90; private static final int LowerCase_LOWER_LIMIT = 97; private static final int LowerCase_UPPER_LIMIT = 122; private static final int INTERMEDIATE_OFFSET = LowerCase_LOWER_LIMIT - UpperCase_UPPER_LIMIT - 1; // <<-METHODS->> private static int getLastDigit(final int digit) { String str = String.valueOf(digit); str = str.substring(str.length() - 1, str.length()); return Integer.valueOf(str); } private static List<Integer> getEncryptationOffsets(String key) { return key.chars() .map(n -> getLastDigit(n) + key.length()) .boxed() .toList(); } private static List<Integer> getPhraseChars(String phrase) { return phrase.chars() .boxed() .collect(Collectors.toList()); } private static String parseChars(List<Integer> translation) { return translation.stream() .map(n -> (char) n.intValue()) .map(c -> Character.toString(c)) .collect(Collectors.joining()); } public static String encode(String phrase, String key) { if (phrase == null || phrase.isEmpty()) return ""; if (key == null || key.isEmpty()) return phrase; List<Integer> offsets = getEncryptationOffsets(key); List<Integer> phraseChars = getPhraseChars(phrase); List<Integer> traduction = new java.util.ArrayList<>(); for (int i = 0; i < phrase.length(); i++) { int keyIndex = i % offsets.size(); int current = phraseChars.get(i); int result = current + offsets.get(keyIndex); if (current <= UpperCase_UPPER_LIMIT && result >= LowerCase_LOWER_LIMIT) { int offset = result - UpperCase_UPPER_LIMIT; result = LowerCase_LOWER_LIMIT + offset - 1; } if (result > UpperCase_UPPER_LIMIT && result < LowerCase_LOWER_LIMIT) { result += INTERMEDIATE_OFFSET; } else if (result > LowerCase_UPPER_LIMIT) { result = result - LowerCase_UPPER_LIMIT + UpperCase_LOWER_LIMIT - 1; } traduction.add(result); } return parseChars(traduction); } public static String decode(String phrase, String key) { if (phrase == null || phrase.isEmpty()) return ""; if (key == null || key.isEmpty()) return phrase; List<Integer> offsets = getEncryptationOffsets(key); List<Integer> phraseChars = getPhraseChars(phrase); List<Integer> traduction = new java.util.ArrayList<>(); for (int i = 0; i < phrase.length(); i++) { int keyIndex = i % offsets.size(); int current = phraseChars.get(i); int result = current - offsets.get(keyIndex); if (current >= LowerCase_LOWER_LIMIT && result <= UpperCase_UPPER_LIMIT) { int offset = LowerCase_LOWER_LIMIT - result; result = UpperCase_UPPER_LIMIT - offset + 1; } if (result < LowerCase_LOWER_LIMIT && result > UpperCase_UPPER_LIMIT) { result -= INTERMEDIATE_OFFSET; } else if (result < UpperCase_LOWER_LIMIT) { int offset = UpperCase_LOWER_LIMIT - result; result = LowerCase_UPPER_LIMIT - offset + 1; } traduction.add(result); } return parseChars(traduction); } }
- import static org.junit.jupiter.api.Assertions.*;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Test;
- import java.util.List;
- import java.util.Random;
- import java.util.stream.Collectors;
- class SolutionTest {
- @Test
- void basicTest() {
- assertEquals("moqik", Kata.encode("abcde", "abcde"));
- assertEquals("abcde", Kata.decode("moqik", "abcde"));
- assertEquals("KMOQS", Kata.encode("ABCDE", "ABCDE"));
- assertEquals("ABCDE", Kata.decode("KMOQS", "ABCDE"));
- assertEquals("GIFUKTG", Kata.encode("DEARGOD", "FGH"));
- assertEquals("DEARGOD", Kata.decode("GIFUKTG", "FGH"));
- assertEquals("ikhwmvi", Kata.encode("deargod", "fgh"));
- assertEquals("deargod", Kata.decode("ikhwmvi", "fgh"));
- assertEquals("IQlOirL", Kata.encode("ALaBamA", "hoME"));
- assertEquals("ALaBamA", Kata.decode("IQlOirL", "hoME"));
- }
- @Test
- void specialCasesTest() {
- assertEquals("", Kata.encode("", ""));
- assertEquals("TernaryLove", Kata.encode("TernaryLove", ""));
- assertEquals("", Kata.encode(null, "abcde"));
- assertEquals("", Kata.encode("", null));
- }
- @Test
- void fullTestSimpleOverflow() {
- assertEquals("CEEpB", Kata.encode("turbo", "TUXON"));
- assertEquals("turbo", Kata.decode("CEEpB", "TUXON"));
- assertEquals("skstzCvzwj", Kata.encode("helloworld", "afgh"));
- assertEquals("helloworld", Kata.decode("skstzCvzwj", "afgh"));
- assertEquals("IKM", Kata.encode("xyz", "ijklmn"));
- assertEquals("xyz", Kata.decode("IKM", "ijklmn"));
- assertEquals("qUlWuZXovQ", Kata.encode("gOdMoRNinG", "uSA"));
- assertEquals("gOdMoRNinG", Kata.decode("qUlWuZXovQ", "uSA"));
- assertEquals("BOywIJdrzafbLMKuCnVqNt", Kata.encode("wElovETerNaRyExpsaNdIj", "dAvID"));
- assertEquals("wElovETerNaRyExpsaNdIj", Kata.decode("BOywIJdrzafbLMKuCnVqNt", "dAvID"));
- }
- @Test
- void fullTestIntermediateOverflow() {
- assertEquals("rhCRDjxXEVyfu", Kata.encode("hOmEsWeEtHoMe", "nOtSoSwEeT"));
- assertEquals("hOmEsWeEtHoMe", Kata.decode("rhCRDjxXEVyfu", "nOtSoSwEeT"));
- assertEquals("cyvNMxgiVt", Kata.encode("WonDErWaLl", "oASiS"));
- assertEquals("WonDErWaLl", Kata.decode("cyvNMxgiVt", "oASiS"));
- assertEquals("lyynmpA", Kata.encode("Zumbido", "wow"));
- assertEquals("Zumbido", Kata.decode("lyynmpA", "wow"));
- }
String generateString(int bound) {Random random = new Random();byte[] buffer = new byte[random.nextInt(bound + 1)];random.nextBytes(buffer);return new String(buffer, java.nio.charset.Charset.forName("UTF-8"));}- private Random random = new Random();
- String generateString(int lowerBound, int upperBound) {
- return java.util.stream.IntStream.rangeClosed(random.nextInt(lowerBound + 1), random.nextInt(upperBound + 1))
- .mapToObj(i -> random.nextInt(2) == 0 ? random.nextInt(65, 91) : random.nextInt(97, 123))
- .collect(StringBuilder::new, (sb, code) -> sb.append((char) code.intValue()), StringBuilder::append)
- .toString();
- }
String generatePhrase(int bound) {return generateString(bound);- String generatePhrase(int lowerBound, int upperBound) {
- return generateString(lowerBound, upperBound);
- }
String generateKey(int bound) {return generateString(bound);- String generateKey(int lowerBound, int upperBound) {
- return generateString(lowerBound, upperBound);
- }
- @RepeatedTest(1500)
void randomTestEasyMode() {String phrase = generatePhrase(1500);String key = generateKey(1500);- void randomEasyModeTest() {
- String phrase = generatePhrase(0, 100);
- String key = generateKey(0, 100);
- assertEquals(Kata.encode(phrase, key), encode(phrase, key));
- assertEquals(Kata.decode(phrase, key), decode(phrase, key));
- }
- @RepeatedTest(1000)
void randomTestHardMode() {String phrase = generatePhrase(15000);String key = generateKey(15000);- void randomHardModeTest() {
- String phrase = generatePhrase(1000, 15000);
- String key = generateKey(1000, 15000);
- assertEquals(Kata.encode(phrase, key), encode(phrase, key));
- assertEquals(Kata.decode(phrase, key), decode(phrase, key));
- }
@RepeatedTest(101)void randomTestGodMode() {String phrase = generatePhrase(150000);String key = generateKey(150000);- @RepeatedTest(50)
- void randomGodModeTest() {
- String phrase = generatePhrase(89000, 90000);
- String key = generateKey(79000, 80000);
- assertEquals(Kata.encode(phrase, key), encode(phrase, key));
- assertEquals(Kata.decode(phrase, key), decode(phrase, key));
- }
- // [[ A N S W E R ]]
- // <<-CONSTANTS->>
- private static final int UpperCase_LOWER_LIMIT = 65;
- private static final int UpperCase_UPPER_LIMIT = 90;
- private static final int LowerCase_LOWER_LIMIT = 97;
- private static final int LowerCase_UPPER_LIMIT = 122;
- private static final int INTERMEDIATE_OFFSET = LowerCase_LOWER_LIMIT - UpperCase_UPPER_LIMIT - 1;
- // <<-METHODS->>
- private static int getLastDigit(final int digit) {
- String str = String.valueOf(digit);
- str = str.substring(str.length() - 1, str.length());
- return Integer.valueOf(str);
- }
- private static List<Integer> getEncryptationOffsets(String key) {
- return key.chars()
- .map(n -> getLastDigit(n) + key.length())
- .boxed()
- .toList();
- }
- private static List<Integer> getPhraseChars(String phrase) {
- return phrase.chars()
- .boxed()
- .collect(Collectors.toList());
- }
- private static String parseChars(List<Integer> translation) {
- return translation.stream()
- .map(n -> (char) n.intValue())
- .map(c -> Character.toString(c))
- .collect(Collectors.joining());
- }
- public static String encode(String phrase, String key) {
if (phrase == null || phrase.isEmpty() || key == null) return "";if (key.isEmpty()) return phrase;- if (phrase == null || phrase.isEmpty()) return "";
- if (key == null || key.isEmpty()) return phrase;
- List<Integer> offsets = getEncryptationOffsets(key);
- List<Integer> phraseChars = getPhraseChars(phrase);
List<Integer> translation = new java.util.ArrayList<>();- List<Integer> traduction = new java.util.ArrayList<>();
int keyIndex = 0;- for (int i = 0; i < phrase.length(); i++) {
keyIndex = i % offsets.size();- int keyIndex = i % offsets.size();
- int current = phraseChars.get(i);
- int result = current + offsets.get(keyIndex);
- if (current <= UpperCase_UPPER_LIMIT && result >= LowerCase_LOWER_LIMIT) {
- int offset = result - UpperCase_UPPER_LIMIT;
- result = LowerCase_LOWER_LIMIT + offset - 1;
- }
- if (result > UpperCase_UPPER_LIMIT && result < LowerCase_LOWER_LIMIT) {
- result += INTERMEDIATE_OFFSET;
- } else if (result > LowerCase_UPPER_LIMIT) {
- result = result - LowerCase_UPPER_LIMIT + UpperCase_LOWER_LIMIT - 1;
- }
translation.add(result);keyIndex++;- traduction.add(result);
- }
return parseChars(translation);- return parseChars(traduction);
- }
- public static String decode(String phrase, String key) {
if (phrase == null || phrase.isEmpty() || key == null) return "";if (key.isEmpty()) return phrase;- if (phrase == null || phrase.isEmpty()) return "";
- if (key == null || key.isEmpty()) return phrase;
- List<Integer> offsets = getEncryptationOffsets(key);
- List<Integer> phraseChars = getPhraseChars(phrase);
List<Integer> translation = new java.util.ArrayList<>();- List<Integer> traduction = new java.util.ArrayList<>();
int keyIndex = 0;- for (int i = 0; i < phrase.length(); i++) {
keyIndex = i % offsets.size();- int keyIndex = i % offsets.size();
- int current = phraseChars.get(i);
- int result = current - offsets.get(keyIndex);
- if (current >= LowerCase_LOWER_LIMIT && result <= UpperCase_UPPER_LIMIT) {
- int offset = LowerCase_LOWER_LIMIT - result;
- result = UpperCase_UPPER_LIMIT - offset + 1;
- }
- if (result < LowerCase_LOWER_LIMIT && result > UpperCase_UPPER_LIMIT) {
- result -= INTERMEDIATE_OFFSET;
- } else if (result < UpperCase_LOWER_LIMIT) {
- int offset = UpperCase_LOWER_LIMIT - result;
- result = LowerCase_UPPER_LIMIT - offset + 1;
- }
translation.add(result);keyIndex++;- traduction.add(result);
- }
return parseChars(translation);- return parseChars(traduction);
- }
- }
Strings
Ciphers
Fundamentals
Cryptography
Algorithms
import java.util.List; import java.util.stream.Collectors; public class Kata { // <<-CONSTANTS->> private static final int UpperCase_LOWER_LIMIT = 65; private static final int UpperCase_UPPER_LIMIT = 90; private static final int LowerCase_LOWER_LIMIT = 97; private static final int LowerCase_UPPER_LIMIT = 122; private static final int INTERMEDIATE_OFFSET = LowerCase_LOWER_LIMIT - UpperCase_UPPER_LIMIT - 1; // <<-METHODS->> private static int getLastDigit(final int digit) { String str = String.valueOf(digit); str = str.substring(str.length() - 1, str.length()); return Integer.valueOf(str); } private static List<Integer> getEncryptationOffsets(String key) { return key.chars() .map(n -> getLastDigit(n) + key.length()) .boxed() .toList(); } private static List<Integer> getPhraseChars(String phrase) { return phrase.chars() .boxed() .collect(Collectors.toList()); } private static String parseChars(List<Integer> traduction) { return traduction.stream() .map(n -> (char) n.intValue()) .map(c -> Character.toString(c)) .collect(Collectors.joining()); } public static String encode(String phrase, String key) { if (phrase == null || phrase.isEmpty() || key == null) return ""; if (key.isEmpty()) return phrase; List<Integer> offsets = getEncryptationOffsets(key); List<Integer> phraseChars = getPhraseChars(phrase); List<Integer> traduction = new java.util.ArrayList<>(); int keyIndex = 0; for (int i = 0; i < phrase.length(); i++) { keyIndex = i % offsets.size(); int current = phraseChars.get(i); int result = current + offsets.get(keyIndex); if (current <= UpperCase_UPPER_LIMIT && result >= LowerCase_LOWER_LIMIT) { int offset = result - UpperCase_UPPER_LIMIT; result = LowerCase_LOWER_LIMIT + offset - 1; } if (result > UpperCase_UPPER_LIMIT && result < LowerCase_LOWER_LIMIT) { result += INTERMEDIATE_OFFSET; } else if (result > LowerCase_UPPER_LIMIT) { result = result - LowerCase_UPPER_LIMIT + UpperCase_LOWER_LIMIT - 1; } traduction.add(result); keyIndex++; } return parseChars(traduction); } public static String decode(String phrase, String key) { if (phrase == null || phrase.isEmpty() || key == null) return ""; if (key.isEmpty()) return phrase; List<Integer> offsets = getEncryptationOffsets(key); List<Integer> phraseChars = getPhraseChars(phrase); List<Integer> traduction = new java.util.ArrayList<>(); int keyIndex = 0; for (int i = 0; i < phrase.length(); i++) { keyIndex = i % offsets.size(); int current = phraseChars.get(i); int result = current - offsets.get(keyIndex); if (current >= LowerCase_LOWER_LIMIT && result <= UpperCase_UPPER_LIMIT) { int offset = LowerCase_LOWER_LIMIT - result; result = UpperCase_UPPER_LIMIT - offset + 1; } if (result < LowerCase_LOWER_LIMIT && result > UpperCase_UPPER_LIMIT) { result -= INTERMEDIATE_OFFSET; } else if (result < UpperCase_LOWER_LIMIT) { int offset = UpperCase_LOWER_LIMIT - result; result = LowerCase_UPPER_LIMIT - offset + 1; } traduction.add(result); keyIndex++; } return parseChars(traduction); } }
- import java.util.List;
- import java.util.stream.Collectors;
- public class Kata {
- // <<-CONSTANTS->>
- private static final int UpperCase_LOWER_LIMIT = 65;
- private static final int UpperCase_UPPER_LIMIT = 90;
- private static final int LowerCase_LOWER_LIMIT = 97;
- private static final int LowerCase_UPPER_LIMIT = 122;
- private static final int INTERMEDIATE_OFFSET = LowerCase_LOWER_LIMIT - UpperCase_UPPER_LIMIT - 1;
- // <<-METHODS->>
- private static int getLastDigit(final int digit) {
- String str = String.valueOf(digit);
- str = str.substring(str.length() - 1, str.length());
- return Integer.valueOf(str);
- }
- private static List<Integer> getEncryptationOffsets(String key) {
- return key.chars()
- .map(n -> getLastDigit(n) + key.length())
- .boxed()
- .toList();
- }
- private static List<Integer> getPhraseChars(String phrase) {
- return phrase.chars()
- .boxed()
- .collect(Collectors.toList());
- }
- private static String parseChars(List<Integer> traduction) {
- return traduction.stream()
- .map(n -> (char) n.intValue())
- .map(c -> Character.toString(c))
- .collect(Collectors.joining());
- }
- public static String encode(String phrase, String key) {
- if (phrase == null || phrase.isEmpty() || key == null) return "";
- if (key.isEmpty()) return phrase;
- List<Integer> offsets = getEncryptationOffsets(key);
- List<Integer> phraseChars = getPhraseChars(phrase);
- List<Integer> traduction = new java.util.ArrayList<>();
- int keyIndex = 0;
- for (int i = 0; i < phrase.length(); i++) {
if (keyIndex == offsets.size())keyIndex = 0;- keyIndex = i % offsets.size();
- int current = phraseChars.get(i);
- int result = current + offsets.get(keyIndex);
- if (current <= UpperCase_UPPER_LIMIT && result >= LowerCase_LOWER_LIMIT) {
- int offset = result - UpperCase_UPPER_LIMIT;
- result = LowerCase_LOWER_LIMIT + offset - 1;
- }
- if (result > UpperCase_UPPER_LIMIT && result < LowerCase_LOWER_LIMIT) {
- result += INTERMEDIATE_OFFSET;
- } else if (result > LowerCase_UPPER_LIMIT) {
- result = result - LowerCase_UPPER_LIMIT + UpperCase_LOWER_LIMIT - 1;
- }
- traduction.add(result);
- keyIndex++;
- }
- return parseChars(traduction);
- }
- public static String decode(String phrase, String key) {
- if (phrase == null || phrase.isEmpty() || key == null) return "";
- if (key.isEmpty()) return phrase;
- List<Integer> offsets = getEncryptationOffsets(key);
- List<Integer> phraseChars = getPhraseChars(phrase);
- List<Integer> traduction = new java.util.ArrayList<>();
- int keyIndex = 0;
- for (int i = 0; i < phrase.length(); i++) {
if (keyIndex == offsets.size())keyIndex = 0;- keyIndex = i % offsets.size();
- int current = phraseChars.get(i);
- int result = current - offsets.get(keyIndex);
- if (current >= LowerCase_LOWER_LIMIT && result <= UpperCase_UPPER_LIMIT) {
- int offset = LowerCase_LOWER_LIMIT - result;
- result = UpperCase_UPPER_LIMIT - offset + 1;
- }
- if (result < LowerCase_LOWER_LIMIT && result > UpperCase_UPPER_LIMIT) {
- result -= INTERMEDIATE_OFFSET;
- } else if (result < UpperCase_LOWER_LIMIT) {
- int offset = UpperCase_LOWER_LIMIT - result;
- result = LowerCase_UPPER_LIMIT - offset + 1;
- }
- traduction.add(result);
- keyIndex++;
- }
- return parseChars(traduction);
- }
- }
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; import java.util.List; import java.util.Random; import java.util.stream.Collectors; class SolutionTest { @Test void basicTest() { assertEquals("moqik", Kata.encode("abcde", "abcde")); assertEquals("abcde", Kata.decode("moqik", "abcde")); assertEquals("KMOQS", Kata.encode("ABCDE", "ABCDE")); assertEquals("ABCDE", Kata.decode("KMOQS", "ABCDE")); assertEquals("GIFUKTG", Kata.encode("DEARGOD", "FGH")); assertEquals("DEARGOD", Kata.decode("GIFUKTG", "FGH")); assertEquals("ikhwmvi", Kata.encode("deargod", "fgh")); assertEquals("deargod", Kata.decode("ikhwmvi", "fgh")); assertEquals("IQlOirL", Kata.encode("ALaBamA", "hoME")); assertEquals("ALaBamA", Kata.decode("IQlOirL", "hoME")); } @Test void specialCasesTest() { assertEquals("", Kata.encode("", "")); assertEquals("TernaryLove", Kata.encode("TernaryLove", "")); assertEquals("", Kata.encode(null, "abcde")); assertEquals("", Kata.encode("", null)); } @Test void fullTestSimpleOverflow() { assertEquals("CEEpB", Kata.encode("turbo", "TUXON")); assertEquals("turbo", Kata.decode("CEEpB", "TUXON")); assertEquals("skstzCvzwj", Kata.encode("helloworld", "afgh")); assertEquals("helloworld", Kata.decode("skstzCvzwj", "afgh")); assertEquals("IKM", Kata.encode("xyz", "ijklmn")); assertEquals("xyz", Kata.decode("IKM", "ijklmn")); assertEquals("qUlWuZXovQ", Kata.encode("gOdMoRNinG", "uSA")); assertEquals("gOdMoRNinG", Kata.decode("qUlWuZXovQ", "uSA")); assertEquals("BOywIJdrzafbLMKuCnVqNt", Kata.encode("wElovETerNaRyExpsaNdIj", "dAvID")); assertEquals("wElovETerNaRyExpsaNdIj", Kata.decode("BOywIJdrzafbLMKuCnVqNt", "dAvID")); } @Test void fullTestIntermediateOverflow() { assertEquals("rhCRDjxXEVyfu", Kata.encode("hOmEsWeEtHoMe", "nOtSoSwEeT")); assertEquals("hOmEsWeEtHoMe", Kata.decode("rhCRDjxXEVyfu", "nOtSoSwEeT")); assertEquals("cyvNMxgiVt", Kata.encode("WonDErWaLl", "oASiS")); assertEquals("WonDErWaLl", Kata.decode("cyvNMxgiVt", "oASiS")); assertEquals("lyynmpA", Kata.encode("Zumbido", "wow")); assertEquals("Zumbido", Kata.decode("lyynmpA", "wow")); } String generateString(int bound) { Random random = new Random(); byte[] buffer = new byte[random.nextInt(bound + 1)]; random.nextBytes(buffer); return new String(buffer, java.nio.charset.Charset.forName("UTF-8")); } String generatePhrase(int bound) { return generateString(bound); } String generateKey(int bound) { return generateString(bound); } @RepeatedTest(1500) void randomTestEasyMode() { String phrase = generatePhrase(1500); String key = generateKey(1500); assertEquals(Kata.encode(phrase, key), encode(phrase, key)); assertEquals(Kata.decode(phrase, key), decode(phrase, key)); } @RepeatedTest(1000) void randomTestHardMode() { String phrase = generatePhrase(15000); String key = generateKey(15000); assertEquals(Kata.encode(phrase, key), encode(phrase, key)); assertEquals(Kata.decode(phrase, key), decode(phrase, key)); } @RepeatedTest(101) void randomTestGodMode() { String phrase = generatePhrase(150000); String key = generateKey(150000); assertEquals(Kata.encode(phrase, key), encode(phrase, key)); assertEquals(Kata.decode(phrase, key), decode(phrase, key)); } // [[ A N S W E R ]] // <<-CONSTANTS->> private static final int UpperCase_LOWER_LIMIT = 65; private static final int UpperCase_UPPER_LIMIT = 90; private static final int LowerCase_LOWER_LIMIT = 97; private static final int LowerCase_UPPER_LIMIT = 122; private static final int INTERMEDIATE_OFFSET = LowerCase_LOWER_LIMIT - UpperCase_UPPER_LIMIT - 1; // <<-METHODS->> private static int getLastDigit(final int digit) { String str = String.valueOf(digit); str = str.substring(str.length() - 1, str.length()); return Integer.valueOf(str); } private static List<Integer> getEncryptationOffsets(String key) { return key.chars() .map(n -> getLastDigit(n) + key.length()) .boxed() .toList(); } private static List<Integer> getPhraseChars(String phrase) { return phrase.chars() .boxed() .collect(Collectors.toList()); } private static String parseChars(List<Integer> traduction) { return traduction.stream() .map(n -> (char) n.intValue()) .map(c -> Character.toString(c)) .collect(Collectors.joining()); } public static String encode(String phrase, String key) { if (phrase == null || phrase.isEmpty() || key == null) return ""; if (key.isEmpty()) return phrase; List<Integer> offsets = getEncryptationOffsets(key); List<Integer> phraseChars = getPhraseChars(phrase); List<Integer> traduction = new java.util.ArrayList<>(); int keyIndex = 0; for (int i = 0; i < phrase.length(); i++) { keyIndex = i % offsets.size(); int current = phraseChars.get(i); int result = current + offsets.get(keyIndex); if (current <= UpperCase_UPPER_LIMIT && result >= LowerCase_LOWER_LIMIT) { int offset = result - UpperCase_UPPER_LIMIT; result = LowerCase_LOWER_LIMIT + offset - 1; } if (result > UpperCase_UPPER_LIMIT && result < LowerCase_LOWER_LIMIT) { result += INTERMEDIATE_OFFSET; } else if (result > LowerCase_UPPER_LIMIT) { result = result - LowerCase_UPPER_LIMIT + UpperCase_LOWER_LIMIT - 1; } traduction.add(result); keyIndex++; } return parseChars(traduction); } public static String decode(String phrase, String key) { if (phrase == null || phrase.isEmpty() || key == null) return ""; if (key.isEmpty()) return phrase; List<Integer> offsets = getEncryptationOffsets(key); List<Integer> phraseChars = getPhraseChars(phrase); List<Integer> traduction = new java.util.ArrayList<>(); int keyIndex = 0; for (int i = 0; i < phrase.length(); i++) { keyIndex = i % offsets.size(); int current = phraseChars.get(i); int result = current - offsets.get(keyIndex); if (current >= LowerCase_LOWER_LIMIT && result <= UpperCase_UPPER_LIMIT) { int offset = LowerCase_LOWER_LIMIT - result; result = UpperCase_UPPER_LIMIT - offset + 1; } if (result < LowerCase_LOWER_LIMIT && result > UpperCase_UPPER_LIMIT) { result -= INTERMEDIATE_OFFSET; } else if (result < UpperCase_LOWER_LIMIT) { int offset = UpperCase_LOWER_LIMIT - result; result = LowerCase_UPPER_LIMIT - offset + 1; } traduction.add(result); keyIndex++; } return parseChars(traduction); } }
- import static org.junit.jupiter.api.Assertions.*;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Test;
- import java.util.List;
- import java.util.Random;
- import java.util.stream.Collectors;
- class SolutionTest {
- @Test
- void basicTest() {
- assertEquals("moqik", Kata.encode("abcde", "abcde"));
- assertEquals("abcde", Kata.decode("moqik", "abcde"));
- assertEquals("KMOQS", Kata.encode("ABCDE", "ABCDE"));
- assertEquals("ABCDE", Kata.decode("KMOQS", "ABCDE"));
- assertEquals("GIFUKTG", Kata.encode("DEARGOD", "FGH"));
- assertEquals("DEARGOD", Kata.decode("GIFUKTG", "FGH"));
- assertEquals("ikhwmvi", Kata.encode("deargod", "fgh"));
- assertEquals("deargod", Kata.decode("ikhwmvi", "fgh"));
- assertEquals("IQlOirL", Kata.encode("ALaBamA", "hoME"));
- assertEquals("ALaBamA", Kata.decode("IQlOirL", "hoME"));
- }
- @Test
- void specialCasesTest() {
- assertEquals("", Kata.encode("", ""));
- assertEquals("TernaryLove", Kata.encode("TernaryLove", ""));
- assertEquals("", Kata.encode(null, "abcde"));
- assertEquals("", Kata.encode("", null));
- }
- @Test
- void fullTestSimpleOverflow() {
- assertEquals("CEEpB", Kata.encode("turbo", "TUXON"));
- assertEquals("turbo", Kata.decode("CEEpB", "TUXON"));
- assertEquals("skstzCvzwj", Kata.encode("helloworld", "afgh"));
- assertEquals("helloworld", Kata.decode("skstzCvzwj", "afgh"));
- assertEquals("IKM", Kata.encode("xyz", "ijklmn"));
- assertEquals("xyz", Kata.decode("IKM", "ijklmn"));
- assertEquals("qUlWuZXovQ", Kata.encode("gOdMoRNinG", "uSA"));
- assertEquals("gOdMoRNinG", Kata.decode("qUlWuZXovQ", "uSA"));
- assertEquals("BOywIJdrzafbLMKuCnVqNt", Kata.encode("wElovETerNaRyExpsaNdIj", "dAvID"));
- assertEquals("wElovETerNaRyExpsaNdIj", Kata.decode("BOywIJdrzafbLMKuCnVqNt", "dAvID"));
- }
- @Test
- void fullTestIntermediateOverflow() {
- assertEquals("rhCRDjxXEVyfu", Kata.encode("hOmEsWeEtHoMe", "nOtSoSwEeT"));
- assertEquals("hOmEsWeEtHoMe", Kata.decode("rhCRDjxXEVyfu", "nOtSoSwEeT"));
- assertEquals("cyvNMxgiVt", Kata.encode("WonDErWaLl", "oASiS"));
- assertEquals("WonDErWaLl", Kata.decode("cyvNMxgiVt", "oASiS"));
- assertEquals("lyynmpA", Kata.encode("Zumbido", "wow"));
- assertEquals("Zumbido", Kata.decode("lyynmpA", "wow"));
- }
- String generateString(int bound) {
- Random random = new Random();
- byte[] buffer = new byte[random.nextInt(bound + 1)];
- random.nextBytes(buffer);
- return new String(buffer, java.nio.charset.Charset.forName("UTF-8"));
- }
- String generatePhrase(int bound) {
- return generateString(bound);
- }
- String generateKey(int bound) {
- return generateString(bound);
- }
- @RepeatedTest(1500)
- void randomTestEasyMode() {
- String phrase = generatePhrase(1500);
- String key = generateKey(1500);
- assertEquals(Kata.encode(phrase, key), encode(phrase, key));
- assertEquals(Kata.decode(phrase, key), decode(phrase, key));
- }
@RepeatedTest(100)- @RepeatedTest(1000)
- void randomTestHardMode() {
- String phrase = generatePhrase(15000);
- String key = generateKey(15000);
- assertEquals(Kata.encode(phrase, key), encode(phrase, key));
- assertEquals(Kata.decode(phrase, key), decode(phrase, key));
- }
@RepeatedTest(10)- @RepeatedTest(101)
- void randomTestGodMode() {
- String phrase = generatePhrase(150000);
- String key = generateKey(150000);
- assertEquals(Kata.encode(phrase, key), encode(phrase, key));
- assertEquals(Kata.decode(phrase, key), decode(phrase, key));
- }
- // [[ A N S W E R ]]
- // <<-CONSTANTS->>
- private static final int UpperCase_LOWER_LIMIT = 65;
- private static final int UpperCase_UPPER_LIMIT = 90;
- private static final int LowerCase_LOWER_LIMIT = 97;
- private static final int LowerCase_UPPER_LIMIT = 122;
- private static final int INTERMEDIATE_OFFSET = LowerCase_LOWER_LIMIT - UpperCase_UPPER_LIMIT - 1;
- // <<-METHODS->>
- private static int getLastDigit(final int digit) {
- String str = String.valueOf(digit);
- str = str.substring(str.length() - 1, str.length());
- return Integer.valueOf(str);
- }
- private static List<Integer> getEncryptationOffsets(String key) {
- return key.chars()
- .map(n -> getLastDigit(n) + key.length())
- .boxed()
- .toList();
- }
- private static List<Integer> getPhraseChars(String phrase) {
- return phrase.chars()
- .boxed()
- .collect(Collectors.toList());
- }
- private static String parseChars(List<Integer> traduction) {
- return traduction.stream()
- .map(n -> (char) n.intValue())
- .map(c -> Character.toString(c))
- .collect(Collectors.joining());
- }
- public static String encode(String phrase, String key) {
- if (phrase == null || phrase.isEmpty() || key == null) return "";
- if (key.isEmpty()) return phrase;
- List<Integer> offsets = getEncryptationOffsets(key);
- List<Integer> phraseChars = getPhraseChars(phrase);
- List<Integer> traduction = new java.util.ArrayList<>();
- int keyIndex = 0;
- for (int i = 0; i < phrase.length(); i++) {
if (keyIndex == offsets.size())keyIndex = 0;- keyIndex = i % offsets.size();
- int current = phraseChars.get(i);
- int result = current + offsets.get(keyIndex);
- if (current <= UpperCase_UPPER_LIMIT && result >= LowerCase_LOWER_LIMIT) {
- int offset = result - UpperCase_UPPER_LIMIT;
- result = LowerCase_LOWER_LIMIT + offset - 1;
- }
- if (result > UpperCase_UPPER_LIMIT && result < LowerCase_LOWER_LIMIT) {
- result += INTERMEDIATE_OFFSET;
- } else if (result > LowerCase_UPPER_LIMIT) {
- result = result - LowerCase_UPPER_LIMIT + UpperCase_LOWER_LIMIT - 1;
- }
- traduction.add(result);
- keyIndex++;
- }
- return parseChars(traduction);
- }
- public static String decode(String phrase, String key) {
- if (phrase == null || phrase.isEmpty() || key == null) return "";
- if (key.isEmpty()) return phrase;
- List<Integer> offsets = getEncryptationOffsets(key);
- List<Integer> phraseChars = getPhraseChars(phrase);
- List<Integer> traduction = new java.util.ArrayList<>();
- int keyIndex = 0;
- for (int i = 0; i < phrase.length(); i++) {
if (keyIndex == offsets.size())keyIndex = 0;- keyIndex = i % offsets.size();
- int current = phraseChars.get(i);
- int result = current - offsets.get(keyIndex);
- if (current >= LowerCase_LOWER_LIMIT && result <= UpperCase_UPPER_LIMIT) {
- int offset = LowerCase_LOWER_LIMIT - result;
- result = UpperCase_UPPER_LIMIT - offset + 1;
- }
- if (result < LowerCase_LOWER_LIMIT && result > UpperCase_UPPER_LIMIT) {
- result -= INTERMEDIATE_OFFSET;
- } else if (result < UpperCase_LOWER_LIMIT) {
- int offset = UpperCase_LOWER_LIMIT - result;
- result = LowerCase_UPPER_LIMIT - offset + 1;
- }
- traduction.add(result);
- keyIndex++;
- }
- return parseChars(traduction);
- }
- }
Strings
Ciphers
Fundamentals
Cryptography
Algorithms
import java.util.List; import java.util.stream.Collectors; public class Kata { // <<-CONSTANTS->> private static final int UpperCase_LOWER_LIMIT = 65; private static final int UpperCase_UPPER_LIMIT = 90; private static final int LowerCase_LOWER_LIMIT = 97; private static final int LowerCase_UPPER_LIMIT = 122; private static final int INTERMEDIATE_OFFSET = LowerCase_LOWER_LIMIT - UpperCase_UPPER_LIMIT - 1; // <<-METHODS->> private static int getLastDigit(final int digit) { String str = String.valueOf(digit); str = str.substring(str.length() - 1, str.length()); return Integer.valueOf(str); } private static List<Integer> getEncryptationOffsets(String key) { return key.chars() .map(n -> getLastDigit(n) + key.length()) .boxed() .toList(); } private static List<Integer> getPhraseChars(String phrase) { return phrase.chars() .boxed() .collect(Collectors.toList()); } private static String parseChars(List<Integer> traduction) { return traduction.stream() .map(n -> (char) n.intValue()) .map(c -> Character.toString(c)) .collect(Collectors.joining()); } public static String encode(String phrase, String key) { if (phrase == null || phrase.isEmpty() || key == null) return ""; if (key.isEmpty()) return phrase; List<Integer> offsets = getEncryptationOffsets(key); List<Integer> phraseChars = getPhraseChars(phrase); List<Integer> traduction = new java.util.ArrayList<>(); int keyIndex = 0; for (int i = 0; i < phrase.length(); i++) { if (keyIndex == offsets.size()) keyIndex = 0; int current = phraseChars.get(i); int result = current + offsets.get(keyIndex); if (current <= UpperCase_UPPER_LIMIT && result >= LowerCase_LOWER_LIMIT) { int offset = result - UpperCase_UPPER_LIMIT; result = LowerCase_LOWER_LIMIT + offset - 1; } if (result > UpperCase_UPPER_LIMIT && result < LowerCase_LOWER_LIMIT) { result += INTERMEDIATE_OFFSET; } else if (result > LowerCase_UPPER_LIMIT) { result = result - LowerCase_UPPER_LIMIT + UpperCase_LOWER_LIMIT - 1; } traduction.add(result); keyIndex++; } return parseChars(traduction); } public static String decode(String phrase, String key) { if (phrase == null || phrase.isEmpty() || key == null) return ""; if (key.isEmpty()) return phrase; List<Integer> offsets = getEncryptationOffsets(key); List<Integer> phraseChars = getPhraseChars(phrase); List<Integer> traduction = new java.util.ArrayList<>(); int keyIndex = 0; for (int i = 0; i < phrase.length(); i++) { if (keyIndex == offsets.size()) keyIndex = 0; int current = phraseChars.get(i); int result = current - offsets.get(keyIndex); if (current >= LowerCase_LOWER_LIMIT && result <= UpperCase_UPPER_LIMIT) { int offset = LowerCase_LOWER_LIMIT - result; result = UpperCase_UPPER_LIMIT - offset + 1; } if (result < LowerCase_LOWER_LIMIT && result > UpperCase_UPPER_LIMIT) { result -= INTERMEDIATE_OFFSET; } else if (result < UpperCase_LOWER_LIMIT) { int offset = UpperCase_LOWER_LIMIT - result; result = LowerCase_UPPER_LIMIT - offset + 1; } traduction.add(result); keyIndex++; } return parseChars(traduction); } }
- import java.util.List;
- import java.util.stream.Collectors;
- public class Kata {
- // <<-CONSTANTS->>
- private static final int UpperCase_LOWER_LIMIT = 65;
- private static final int UpperCase_UPPER_LIMIT = 90;
- private static final int LowerCase_LOWER_LIMIT = 97;
- private static final int LowerCase_UPPER_LIMIT = 122;
- private static final int INTERMEDIATE_OFFSET = LowerCase_LOWER_LIMIT - UpperCase_UPPER_LIMIT - 1;
- // <<-METHODS->>
- private static int getLastDigit(final int digit) {
- String str = String.valueOf(digit);
- str = str.substring(str.length() - 1, str.length());
- return Integer.valueOf(str);
- }
- private static List<Integer> getEncryptationOffsets(String key) {
- return key.chars()
- .map(n -> getLastDigit(n) + key.length())
- .boxed()
- .toList();
- }
- private static List<Integer> getPhraseChars(String phrase) {
- return phrase.chars()
- .boxed()
- .collect(Collectors.toList());
- }
- private static String parseChars(List<Integer> traduction) {
- return traduction.stream()
- .map(n -> (char) n.intValue())
- .map(c -> Character.toString(c))
- .collect(Collectors.joining());
- }
- public static String encode(String phrase, String key) {
- if (phrase == null || phrase.isEmpty() || key == null) return "";
- if (key.isEmpty()) return phrase;
- List<Integer> offsets = getEncryptationOffsets(key);
- List<Integer> phraseChars = getPhraseChars(phrase);
- List<Integer> traduction = new java.util.ArrayList<>();
- int keyIndex = 0;
- for (int i = 0; i < phrase.length(); i++) {
- if (keyIndex == offsets.size())
- keyIndex = 0;
- int current = phraseChars.get(i);
- int result = current + offsets.get(keyIndex);
- if (current <= UpperCase_UPPER_LIMIT && result >= LowerCase_LOWER_LIMIT) {
- int offset = result - UpperCase_UPPER_LIMIT;
- result = LowerCase_LOWER_LIMIT + offset - 1;
- }
- if (result > UpperCase_UPPER_LIMIT && result < LowerCase_LOWER_LIMIT) {
- result += INTERMEDIATE_OFFSET;
- } else if (result > LowerCase_UPPER_LIMIT) {
- result = result - LowerCase_UPPER_LIMIT + UpperCase_LOWER_LIMIT - 1;
- }
- traduction.add(result);
- keyIndex++;
- }
- return parseChars(traduction);
- }
- public static String decode(String phrase, String key) {
- if (phrase == null || phrase.isEmpty() || key == null) return "";
- if (key.isEmpty()) return phrase;
- List<Integer> offsets = getEncryptationOffsets(key);
- List<Integer> phraseChars = getPhraseChars(phrase);
- List<Integer> traduction = new java.util.ArrayList<>();
- int keyIndex = 0;
- for (int i = 0; i < phrase.length(); i++) {
- if (keyIndex == offsets.size())
- keyIndex = 0;
- int current = phraseChars.get(i);
- int result = current - offsets.get(keyIndex);
- if (current >= LowerCase_LOWER_LIMIT && result <= UpperCase_UPPER_LIMIT) {
- int offset = LowerCase_LOWER_LIMIT - result;
- result = UpperCase_UPPER_LIMIT - offset + 1;
- }
- if (result < LowerCase_LOWER_LIMIT && result > UpperCase_UPPER_LIMIT) {
- result -= INTERMEDIATE_OFFSET;
- } else if (result < UpperCase_LOWER_LIMIT) {
- int offset = UpperCase_LOWER_LIMIT - result;
- result = LowerCase_UPPER_LIMIT - offset + 1;
- }
- traduction.add(result);
- keyIndex++;
- }
- return parseChars(traduction);
- }
- }
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; import java.util.List; import java.util.Random; import java.util.stream.Collectors; class SolutionTest { @Test void basicTest() { assertEquals("moqik", Kata.encode("abcde", "abcde")); assertEquals("abcde", Kata.decode("moqik", "abcde")); assertEquals("KMOQS", Kata.encode("ABCDE", "ABCDE")); assertEquals("ABCDE", Kata.decode("KMOQS", "ABCDE")); assertEquals("GIFUKTG", Kata.encode("DEARGOD", "FGH")); assertEquals("DEARGOD", Kata.decode("GIFUKTG", "FGH")); assertEquals("ikhwmvi", Kata.encode("deargod", "fgh")); assertEquals("deargod", Kata.decode("ikhwmvi", "fgh")); assertEquals("IQlOirL", Kata.encode("ALaBamA", "hoME")); assertEquals("ALaBamA", Kata.decode("IQlOirL", "hoME")); } @Test void specialCasesTest() { assertEquals("", Kata.encode("", "")); assertEquals("TernaryLove", Kata.encode("TernaryLove", "")); assertEquals("", Kata.encode(null, "abcde")); assertEquals("", Kata.encode("", null)); } @Test void fullTestSimpleOverflow() { assertEquals("CEEpB", Kata.encode("turbo", "TUXON")); assertEquals("turbo", Kata.decode("CEEpB", "TUXON")); assertEquals("skstzCvzwj", Kata.encode("helloworld", "afgh")); assertEquals("helloworld", Kata.decode("skstzCvzwj", "afgh")); assertEquals("IKM", Kata.encode("xyz", "ijklmn")); assertEquals("xyz", Kata.decode("IKM", "ijklmn")); assertEquals("qUlWuZXovQ", Kata.encode("gOdMoRNinG", "uSA")); assertEquals("gOdMoRNinG", Kata.decode("qUlWuZXovQ", "uSA")); assertEquals("BOywIJdrzafbLMKuCnVqNt", Kata.encode("wElovETerNaRyExpsaNdIj", "dAvID")); assertEquals("wElovETerNaRyExpsaNdIj", Kata.decode("BOywIJdrzafbLMKuCnVqNt", "dAvID")); } @Test void fullTestIntermediateOverflow() { assertEquals("rhCRDjxXEVyfu", Kata.encode("hOmEsWeEtHoMe", "nOtSoSwEeT")); assertEquals("hOmEsWeEtHoMe", Kata.decode("rhCRDjxXEVyfu", "nOtSoSwEeT")); assertEquals("cyvNMxgiVt", Kata.encode("WonDErWaLl", "oASiS")); assertEquals("WonDErWaLl", Kata.decode("cyvNMxgiVt", "oASiS")); assertEquals("lyynmpA", Kata.encode("Zumbido", "wow")); assertEquals("Zumbido", Kata.decode("lyynmpA", "wow")); } String generateString(int bound) { Random random = new Random(); byte[] buffer = new byte[random.nextInt(bound + 1)]; random.nextBytes(buffer); return new String(buffer, java.nio.charset.Charset.forName("UTF-8")); } String generatePhrase(int bound) { return generateString(bound); } String generateKey(int bound) { return generateString(bound); } @RepeatedTest(1500) void randomTestEasyMode() { String phrase = generatePhrase(1500); String key = generateKey(1500); assertEquals(Kata.encode(phrase, key), encode(phrase, key)); assertEquals(Kata.decode(phrase, key), decode(phrase, key)); } @RepeatedTest(100) void randomTestHardMode() { String phrase = generatePhrase(15000); String key = generateKey(15000); assertEquals(Kata.encode(phrase, key), encode(phrase, key)); assertEquals(Kata.decode(phrase, key), decode(phrase, key)); } @RepeatedTest(10) void randomTestGodMode() { String phrase = generatePhrase(150000); String key = generateKey(150000); assertEquals(Kata.encode(phrase, key), encode(phrase, key)); assertEquals(Kata.decode(phrase, key), decode(phrase, key)); } // [[ A N S W E R ]] // <<-CONSTANTS->> private static final int UpperCase_LOWER_LIMIT = 65; private static final int UpperCase_UPPER_LIMIT = 90; private static final int LowerCase_LOWER_LIMIT = 97; private static final int LowerCase_UPPER_LIMIT = 122; private static final int INTERMEDIATE_OFFSET = LowerCase_LOWER_LIMIT - UpperCase_UPPER_LIMIT - 1; // <<-METHODS->> private static int getLastDigit(final int digit) { String str = String.valueOf(digit); str = str.substring(str.length() - 1, str.length()); return Integer.valueOf(str); } private static List<Integer> getEncryptationOffsets(String key) { return key.chars() .map(n -> getLastDigit(n) + key.length()) .boxed() .toList(); } private static List<Integer> getPhraseChars(String phrase) { return phrase.chars() .boxed() .collect(Collectors.toList()); } private static String parseChars(List<Integer> traduction) { return traduction.stream() .map(n -> (char) n.intValue()) .map(c -> Character.toString(c)) .collect(Collectors.joining()); } public static String encode(String phrase, String key) { if (phrase == null || phrase.isEmpty() || key == null) return ""; if (key.isEmpty()) return phrase; List<Integer> offsets = getEncryptationOffsets(key); List<Integer> phraseChars = getPhraseChars(phrase); List<Integer> traduction = new java.util.ArrayList<>(); int keyIndex = 0; for (int i = 0; i < phrase.length(); i++) { if (keyIndex == offsets.size()) keyIndex = 0; int current = phraseChars.get(i); int result = current + offsets.get(keyIndex); if (current <= UpperCase_UPPER_LIMIT && result >= LowerCase_LOWER_LIMIT) { int offset = result - UpperCase_UPPER_LIMIT; result = LowerCase_LOWER_LIMIT + offset - 1; } if (result > UpperCase_UPPER_LIMIT && result < LowerCase_LOWER_LIMIT) { result += INTERMEDIATE_OFFSET; } else if (result > LowerCase_UPPER_LIMIT) { result = result - LowerCase_UPPER_LIMIT + UpperCase_LOWER_LIMIT - 1; } traduction.add(result); keyIndex++; } return parseChars(traduction); } public static String decode(String phrase, String key) { if (phrase == null || phrase.isEmpty() || key == null) return ""; if (key.isEmpty()) return phrase; List<Integer> offsets = getEncryptationOffsets(key); List<Integer> phraseChars = getPhraseChars(phrase); List<Integer> traduction = new java.util.ArrayList<>(); int keyIndex = 0; for (int i = 0; i < phrase.length(); i++) { if (keyIndex == offsets.size()) keyIndex = 0; int current = phraseChars.get(i); int result = current - offsets.get(keyIndex); if (current >= LowerCase_LOWER_LIMIT && result <= UpperCase_UPPER_LIMIT) { int offset = LowerCase_LOWER_LIMIT - result; result = UpperCase_UPPER_LIMIT - offset + 1; } if (result < LowerCase_LOWER_LIMIT && result > UpperCase_UPPER_LIMIT) { result -= INTERMEDIATE_OFFSET; } else if (result < UpperCase_LOWER_LIMIT) { int offset = UpperCase_LOWER_LIMIT - result; result = LowerCase_UPPER_LIMIT - offset + 1; } traduction.add(result); keyIndex++; } return parseChars(traduction); } }
- import static org.junit.jupiter.api.Assertions.*;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Test;
- import java.util.List;
- import java.util.Random;
- import java.util.stream.Collectors;
- class SolutionTest {
- @Test
- void basicTest() {
- assertEquals("moqik", Kata.encode("abcde", "abcde"));
- assertEquals("abcde", Kata.decode("moqik", "abcde"));
- assertEquals("KMOQS", Kata.encode("ABCDE", "ABCDE"));
- assertEquals("ABCDE", Kata.decode("KMOQS", "ABCDE"));
- assertEquals("GIFUKTG", Kata.encode("DEARGOD", "FGH"));
- assertEquals("DEARGOD", Kata.decode("GIFUKTG", "FGH"));
- assertEquals("ikhwmvi", Kata.encode("deargod", "fgh"));
- assertEquals("deargod", Kata.decode("ikhwmvi", "fgh"));
- assertEquals("IQlOirL", Kata.encode("ALaBamA", "hoME"));
- assertEquals("ALaBamA", Kata.decode("IQlOirL", "hoME"));
- }
- @Test
- void specialCasesTest() {
- assertEquals("", Kata.encode("", ""));
- assertEquals("TernaryLove", Kata.encode("TernaryLove", ""));
- assertEquals("", Kata.encode(null, "abcde"));
- assertEquals("", Kata.encode("", null));
- }
- @Test
- void fullTestSimpleOverflow() {
- assertEquals("CEEpB", Kata.encode("turbo", "TUXON"));
- assertEquals("turbo", Kata.decode("CEEpB", "TUXON"));
- assertEquals("skstzCvzwj", Kata.encode("helloworld", "afgh"));
- assertEquals("helloworld", Kata.decode("skstzCvzwj", "afgh"));
- assertEquals("IKM", Kata.encode("xyz", "ijklmn"));
- assertEquals("xyz", Kata.decode("IKM", "ijklmn"));
- assertEquals("qUlWuZXovQ", Kata.encode("gOdMoRNinG", "uSA"));
- assertEquals("gOdMoRNinG", Kata.decode("qUlWuZXovQ", "uSA"));
- assertEquals("BOywIJdrzafbLMKuCnVqNt", Kata.encode("wElovETerNaRyExpsaNdIj", "dAvID"));
- assertEquals("wElovETerNaRyExpsaNdIj", Kata.decode("BOywIJdrzafbLMKuCnVqNt", "dAvID"));
- }
- @Test
- void fullTestIntermediateOverflow() {
- assertEquals("rhCRDjxXEVyfu", Kata.encode("hOmEsWeEtHoMe", "nOtSoSwEeT"));
- assertEquals("hOmEsWeEtHoMe", Kata.decode("rhCRDjxXEVyfu", "nOtSoSwEeT"));
- assertEquals("cyvNMxgiVt", Kata.encode("WonDErWaLl", "oASiS"));
- assertEquals("WonDErWaLl", Kata.decode("cyvNMxgiVt", "oASiS"));
- assertEquals("lyynmpA", Kata.encode("Zumbido", "wow"));
- assertEquals("Zumbido", Kata.decode("lyynmpA", "wow"));
- }
- String generateString(int bound) {
- Random random = new Random();
- byte[] buffer = new byte[random.nextInt(bound + 1)];
- random.nextBytes(buffer);
- return new String(buffer, java.nio.charset.Charset.forName("UTF-8"));
- }
- String generatePhrase(int bound) {
- return generateString(bound);
- }
- String generateKey(int bound) {
- return generateString(bound);
- }
@RepeatedTest(200)- @RepeatedTest(1500)
- void randomTestEasyMode() {
String phrase = generatePhrase(500);String key = generateKey(500);- String phrase = generatePhrase(1500);
- String key = generateKey(1500);
- assertEquals(Kata.encode(phrase, key), encode(phrase, key));
- assertEquals(Kata.decode(phrase, key), decode(phrase, key));
- }
- @RepeatedTest(100)
- void randomTestHardMode() {
- String phrase = generatePhrase(15000);
- String key = generateKey(15000);
- assertEquals(Kata.encode(phrase, key), encode(phrase, key));
- assertEquals(Kata.decode(phrase, key), decode(phrase, key));
- }
- @RepeatedTest(10)
- void randomTestGodMode() {
- String phrase = generatePhrase(150000);
- String key = generateKey(150000);
- assertEquals(Kata.encode(phrase, key), encode(phrase, key));
- assertEquals(Kata.decode(phrase, key), decode(phrase, key));
- }
- // [[ A N S W E R ]]
- // <<-CONSTANTS->>
- private static final int UpperCase_LOWER_LIMIT = 65;
- private static final int UpperCase_UPPER_LIMIT = 90;
- private static final int LowerCase_LOWER_LIMIT = 97;
- private static final int LowerCase_UPPER_LIMIT = 122;
- private static final int INTERMEDIATE_OFFSET = LowerCase_LOWER_LIMIT - UpperCase_UPPER_LIMIT - 1;
- // <<-METHODS->>
- private static int getLastDigit(final int digit) {
- String str = String.valueOf(digit);
- str = str.substring(str.length() - 1, str.length());
- return Integer.valueOf(str);
- }
- private static List<Integer> getEncryptationOffsets(String key) {
- return key.chars()
- .map(n -> getLastDigit(n) + key.length())
- .boxed()
- .toList();
- }
- private static List<Integer> getPhraseChars(String phrase) {
- return phrase.chars()
- .boxed()
- .collect(Collectors.toList());
- }
- private static String parseChars(List<Integer> traduction) {
- return traduction.stream()
- .map(n -> (char) n.intValue())
- .map(c -> Character.toString(c))
- .collect(Collectors.joining());
- }
- public static String encode(String phrase, String key) {
- if (phrase == null || phrase.isEmpty() || key == null) return "";
- if (key.isEmpty()) return phrase;
- List<Integer> offsets = getEncryptationOffsets(key);
- List<Integer> phraseChars = getPhraseChars(phrase);
- List<Integer> traduction = new java.util.ArrayList<>();
- int keyIndex = 0;
- for (int i = 0; i < phrase.length(); i++) {
- if (keyIndex == offsets.size())
- keyIndex = 0;
- int current = phraseChars.get(i);
- int result = current + offsets.get(keyIndex);
- if (current <= UpperCase_UPPER_LIMIT && result >= LowerCase_LOWER_LIMIT) {
- int offset = result - UpperCase_UPPER_LIMIT;
- result = LowerCase_LOWER_LIMIT + offset - 1;
- }
- if (result > UpperCase_UPPER_LIMIT && result < LowerCase_LOWER_LIMIT) {
- result += INTERMEDIATE_OFFSET;
- } else if (result > LowerCase_UPPER_LIMIT) {
- result = result - LowerCase_UPPER_LIMIT + UpperCase_LOWER_LIMIT - 1;
- }
- traduction.add(result);
- keyIndex++;
- }
- return parseChars(traduction);
- }
- public static String decode(String phrase, String key) {
- if (phrase == null || phrase.isEmpty() || key == null) return "";
- if (key.isEmpty()) return phrase;
- List<Integer> offsets = getEncryptationOffsets(key);
- List<Integer> phraseChars = getPhraseChars(phrase);
- List<Integer> traduction = new java.util.ArrayList<>();
- int keyIndex = 0;
- for (int i = 0; i < phrase.length(); i++) {
- if (keyIndex == offsets.size())
- keyIndex = 0;
- int current = phraseChars.get(i);
- int result = current - offsets.get(keyIndex);
- if (current >= LowerCase_LOWER_LIMIT && result <= UpperCase_UPPER_LIMIT) {
- int offset = LowerCase_LOWER_LIMIT - result;
- result = UpperCase_UPPER_LIMIT - offset + 1;
- }
- if (result < LowerCase_LOWER_LIMIT && result > UpperCase_UPPER_LIMIT) {
- result -= INTERMEDIATE_OFFSET;
- } else if (result < UpperCase_LOWER_LIMIT) {
- int offset = UpperCase_LOWER_LIMIT - result;
- result = LowerCase_UPPER_LIMIT - offset + 1;
- }
- traduction.add(result);
- keyIndex++;
- }
- return parseChars(traduction);
- }
- }
Strings
Ciphers
Fundamentals
Cryptography
Algorithms
import java.util.List; import java.util.stream.Collectors; public class Kata { // <<-CONSTANTS->> private static final int UpperCase_LOWER_LIMIT = 65; private static final int UpperCase_UPPER_LIMIT = 90; private static final int LowerCase_LOWER_LIMIT = 97; private static final int LowerCase_UPPER_LIMIT = 122; private static final int INTERMEDIATE_OFFSET = LowerCase_LOWER_LIMIT - UpperCase_UPPER_LIMIT - 1; // <<-METHODS->> private static int getLastDigit(final int digit) { String str = String.valueOf(digit); str = str.substring(str.length() - 1, str.length()); return Integer.valueOf(str); } private static List<Integer> getEncryptationOffsets(String key) { return key.chars() .map(n -> getLastDigit(n) + key.length()) .boxed() .toList(); } private static List<Integer> getPhraseChars(String phrase) { return phrase.chars() .boxed() .collect(Collectors.toList()); } private static String parseChars(List<Integer> traduction) { return traduction.stream() .map(n -> (char) n.intValue()) .map(c -> Character.toString(c)) .collect(Collectors.joining()); } public static String encode(String phrase, String key) { List<Integer> offsets = getEncryptationOffsets(key); List<Integer> phraseChars = getPhraseChars(phrase); List<Integer> traduction = new java.util.ArrayList<>(); int keyIndex = 0; for (int i = 0; i < phrase.length(); i++) { if (keyIndex == offsets.size()) keyIndex = 0; int current = phraseChars.get(i); int result = current + offsets.get(keyIndex); if (current <= UpperCase_UPPER_LIMIT && result >= LowerCase_LOWER_LIMIT) { int offset = result - UpperCase_UPPER_LIMIT; result = LowerCase_LOWER_LIMIT + offset - 1; } if (result > UpperCase_UPPER_LIMIT && result < LowerCase_LOWER_LIMIT) { result += INTERMEDIATE_OFFSET; } else if (result > LowerCase_UPPER_LIMIT) { result = result - LowerCase_UPPER_LIMIT + UpperCase_LOWER_LIMIT - 1; } traduction.add(result); keyIndex++; } return parseChars(traduction); } public static String decode(String phrase, String key) { List<Integer> offsets = getEncryptationOffsets(key); List<Integer> phraseChars = getPhraseChars(phrase); List<Integer> traduction = new java.util.ArrayList<>(); int keyIndex = 0; for (int i = 0; i < phrase.length(); i++) { if (keyIndex == offsets.size()) keyIndex = 0; int current = phraseChars.get(i); int result = current - offsets.get(keyIndex); if (current >= LowerCase_LOWER_LIMIT && result <= UpperCase_UPPER_LIMIT) { int offset = LowerCase_LOWER_LIMIT - result; result = UpperCase_UPPER_LIMIT - offset + 1; } if (result < LowerCase_LOWER_LIMIT && result > UpperCase_UPPER_LIMIT) { result -= INTERMEDIATE_OFFSET; } else if (result < UpperCase_LOWER_LIMIT) { int offset = UpperCase_LOWER_LIMIT - result; result = LowerCase_UPPER_LIMIT - offset + 1; } traduction.add(result); keyIndex++; } return parseChars(traduction); } }
- import java.util.List;
- import java.util.stream.Collectors;
- public class Kata {
private static final int UPPERCASE_LOWERLIMIT = 65;private static final int UPPERCASE_UPPERLIMIT = 90;private static final int LOWERCASE_LOWERLIMIT = 97;private static final int LOWERCASE_UPPERLIMIT = 122;private static final int INTERMEDIATE_OFFSET = LOWERCASE_LOWERLIMIT-UPPERCASE_UPPERLIMIT-1;public static void main(String[] args) {System.out.println(decode("rhCRDjxXEVyfu", "nOtSoSwEeT"));}// <<-METHODS->>private static int getLastDigit(int n) {String str = String.valueOf(n);str = str.substring(str.length() - 1, str.length());- // <<-CONSTANTS->>
- private static final int UpperCase_LOWER_LIMIT = 65;
- private static final int UpperCase_UPPER_LIMIT = 90;
- private static final int LowerCase_LOWER_LIMIT = 97;
- private static final int LowerCase_UPPER_LIMIT = 122;
- private static final int INTERMEDIATE_OFFSET = LowerCase_LOWER_LIMIT - UpperCase_UPPER_LIMIT - 1;
- // <<-METHODS->>
- private static int getLastDigit(final int digit) {
- String str = String.valueOf(digit);
- str = str.substring(str.length() - 1, str.length());
- return Integer.valueOf(str);
- }
public static String encode(String phrase, String key) {List<Integer> adds = key.chars().map(n -> getLastDigit(n) + key.length()).boxed().toList();List<Integer> phraseChars = phrase.chars()- private static List<Integer> getEncryptationOffsets(String key) {
- return key.chars()
- .map(n -> getLastDigit(n) + key.length())
- .boxed()
- .toList();
- }
- private static List<Integer> getPhraseChars(String phrase) {
- return phrase.chars()
- .boxed()
- .collect(Collectors.toList());
List<Integer> traduction = new java.util.ArrayList<>();int j = 0;- }
- private static String parseChars(List<Integer> traduction) {
- return traduction.stream()
- .map(n -> (char) n.intValue())
- .map(c -> Character.toString(c))
- .collect(Collectors.joining());
- }
- public static String encode(String phrase, String key) {
- List<Integer> offsets = getEncryptationOffsets(key);
- List<Integer> phraseChars = getPhraseChars(phrase);
- List<Integer> traduction = new java.util.ArrayList<>();
- int keyIndex = 0;
- for (int i = 0; i < phrase.length(); i++) {
if (j == adds.size()) j = 0;int actualChar=phraseChars.get(i);int result=adds.get(j)+actualChar;if(actualChar<=UPPERCASE_UPPERLIMIT&&result>=LOWERCASE_LOWERLIMIT) {int offset=result-UPPERCASE_UPPERLIMIT;result=LOWERCASE_LOWERLIMIT+offset-1;}if(result>UPPERCASE_UPPERLIMIT&&result<LOWERCASE_LOWERLIMIT) {result+=INTERMEDIATE_OFFSET;- if (keyIndex == offsets.size())
- keyIndex = 0;
- int current = phraseChars.get(i);
- int result = current + offsets.get(keyIndex);
- if (current <= UpperCase_UPPER_LIMIT && result >= LowerCase_LOWER_LIMIT) {
- int offset = result - UpperCase_UPPER_LIMIT;
- result = LowerCase_LOWER_LIMIT + offset - 1;
- }
else if(result>LOWERCASE_UPPERLIMIT) {result=result-LOWERCASE_UPPERLIMIT+UPPERCASE_LOWERLIMIT-1;- if (result > UpperCase_UPPER_LIMIT && result < LowerCase_LOWER_LIMIT) {
- result += INTERMEDIATE_OFFSET;
- } else if (result > LowerCase_UPPER_LIMIT) {
- result = result - LowerCase_UPPER_LIMIT + UpperCase_LOWER_LIMIT - 1;
- }
- traduction.add(result);
j++;- keyIndex++;
- }
return traduction.stream().map(n -> (char) n.intValue()).map(ch -> Character.toString(ch)).collect(Collectors.joining());- return parseChars(traduction);
- }
/*-----------------------------------------------------------------------------------*/public static String decode(String phrase, String key) {List<Integer> adds = key.chars().map(n -> getLastDigit(n) + key.length()).boxed().toList();List<Integer> phraseChars = phrase.chars().boxed().collect(Collectors.toList());- public static String decode(String phrase, String key) {
- List<Integer> offsets = getEncryptationOffsets(key);
- List<Integer> phraseChars = getPhraseChars(phrase);
- List<Integer> traduction = new java.util.ArrayList<>();
- int keyIndex = 0;
- for (int i = 0; i < phrase.length(); i++) {
- if (keyIndex == offsets.size())
- keyIndex = 0;
List<Integer> traduction = new java.util.ArrayList<>();int j = 0;for (int i = 0; i < phrase.length(); i++) {if (j == adds.size()) j = 0;int actualChar=phraseChars.get(i);int resulSum=actualChar-adds.get(j);if(actualChar>=LOWERCASE_LOWERLIMIT&&resulSum<=UPPERCASE_UPPERLIMIT) {int offset=LOWERCASE_LOWERLIMIT-resulSum;resulSum=UPPERCASE_UPPERLIMIT-offset+1;}if(resulSum<LOWERCASE_LOWERLIMIT&&resulSum>UPPERCASE_UPPERLIMIT) {resulSum-=INTERMEDIATE_OFFSET;}else if(resulSum<UPPERCASE_LOWERLIMIT) {int offset=UPPERCASE_LOWERLIMIT-resulSum;resulSum=LOWERCASE_UPPERLIMIT-offset+1;}traduction.add(resulSum);j++;- int current = phraseChars.get(i);
- int result = current - offsets.get(keyIndex);
- if (current >= LowerCase_LOWER_LIMIT && result <= UpperCase_UPPER_LIMIT) {
- int offset = LowerCase_LOWER_LIMIT - result;
- result = UpperCase_UPPER_LIMIT - offset + 1;
- }
- if (result < LowerCase_LOWER_LIMIT && result > UpperCase_UPPER_LIMIT) {
- result -= INTERMEDIATE_OFFSET;
- } else if (result < UpperCase_LOWER_LIMIT) {
- int offset = UpperCase_LOWER_LIMIT - result;
- result = LowerCase_UPPER_LIMIT - offset + 1;
- }
- traduction.add(result);
- keyIndex++;
- }
return traduction.stream().map(n -> (char) n.intValue()).map(ch -> Character.toString(ch)).collect(Collectors.joining());}- return parseChars(traduction);
- }
- }
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; import java.util.List; import java.util.Random; import java.util.stream.Collectors; class SolutionTest { @Test void basicTest() { assertEquals("moqik", Kata.encode("abcde", "abcde")); assertEquals("abcde", Kata.decode("moqik", "abcde")); assertEquals("KMOQS", Kata.encode("ABCDE", "ABCDE")); assertEquals("ABCDE", Kata.decode("KMOQS", "ABCDE")); assertEquals("GIFUKTG", Kata.encode("DEARGOD", "FGH")); assertEquals("DEARGOD", Kata.decode("GIFUKTG", "FGH")); assertEquals("ikhwmvi", Kata.encode("deargod", "fgh")); assertEquals("deargod", Kata.decode("ikhwmvi", "fgh")); assertEquals("IQlOirL", Kata.encode("ALaBamA", "hoME")); assertEquals("ALaBamA", Kata.decode("IQlOirL", "hoME")); } @Test void specialCasesTest() { assertEquals("", Kata.encode("", "")); assertEquals("TernaryLove", Kata.encode("TernaryLove", "")); assertEquals("", Kata.encode(null, "abcde")); assertEquals("", Kata.encode("", null)); } @Test void fullTestSimpleOverflow() { assertEquals("CEEpB", Kata.encode("turbo", "TUXON")); assertEquals("turbo", Kata.decode("CEEpB", "TUXON")); assertEquals("skstzCvzwj", Kata.encode("helloworld", "afgh")); assertEquals("helloworld", Kata.decode("skstzCvzwj", "afgh")); assertEquals("IKM", Kata.encode("xyz", "ijklmn")); assertEquals("xyz", Kata.decode("IKM", "ijklmn")); assertEquals("qUlWuZXovQ", Kata.encode("gOdMoRNinG", "uSA")); assertEquals("gOdMoRNinG", Kata.decode("qUlWuZXovQ", "uSA")); assertEquals("BOywIJdrzafbLMKuCnVqNt", Kata.encode("wElovETerNaRyExpsaNdIj", "dAvID")); assertEquals("wElovETerNaRyExpsaNdIj", Kata.decode("BOywIJdrzafbLMKuCnVqNt", "dAvID")); } @Test void fullTestIntermediateOverflow() { assertEquals("rhCRDjxXEVyfu", Kata.encode("hOmEsWeEtHoMe", "nOtSoSwEeT")); assertEquals("hOmEsWeEtHoMe", Kata.decode("rhCRDjxXEVyfu", "nOtSoSwEeT")); assertEquals("cyvNMxgiVt", Kata.encode("WonDErWaLl", "oASiS")); assertEquals("WonDErWaLl", Kata.decode("cyvNMxgiVt", "oASiS")); assertEquals("lyynmpA", Kata.encode("Zumbido", "wow")); assertEquals("Zumbido", Kata.decode("lyynmpA", "wow")); } String generateString(int bound) { Random random = new Random(); byte[] buffer = new byte[random.nextInt(bound + 1)]; random.nextBytes(buffer); return new String(buffer, java.nio.charset.Charset.forName("UTF-8")); } String generatePhrase(int bound) { return generateString(bound); } String generateKey(int bound) { return generateString(bound); } @RepeatedTest(200) void randomTestEasyMode() { String phrase = generatePhrase(500); String key = generateKey(500); assertEquals(Kata.encode(phrase, key), encode(phrase, key)); assertEquals(Kata.decode(phrase, key), decode(phrase, key)); } @RepeatedTest(100) void randomTestHardMode() { String phrase = generatePhrase(15000); String key = generateKey(15000); assertEquals(Kata.encode(phrase, key), encode(phrase, key)); assertEquals(Kata.decode(phrase, key), decode(phrase, key)); } @RepeatedTest(10) void randomTestGodMode() { String phrase = generatePhrase(150000); String key = generateKey(150000); assertEquals(Kata.encode(phrase, key), encode(phrase, key)); assertEquals(Kata.decode(phrase, key), decode(phrase, key)); } // [[ A N S W E R ]] // <<-CONSTANTS->> private static final int UpperCase_LOWER_LIMIT = 65; private static final int UpperCase_UPPER_LIMIT = 90; private static final int LowerCase_LOWER_LIMIT = 97; private static final int LowerCase_UPPER_LIMIT = 122; private static final int INTERMEDIATE_OFFSET = LowerCase_LOWER_LIMIT - UpperCase_UPPER_LIMIT - 1; // <<-METHODS->> private static int getLastDigit(final int digit) { String str = String.valueOf(digit); str = str.substring(str.length() - 1, str.length()); return Integer.valueOf(str); } private static List<Integer> getEncryptationOffsets(String key) { return key.chars() .map(n -> getLastDigit(n) + key.length()) .boxed() .toList(); } private static List<Integer> getPhraseChars(String phrase) { return phrase.chars() .boxed() .collect(Collectors.toList()); } private static String parseChars(List<Integer> traduction) { return traduction.stream() .map(n -> (char) n.intValue()) .map(c -> Character.toString(c)) .collect(Collectors.joining()); } public static String encode(String phrase, String key) { List<Integer> offsets = getEncryptationOffsets(key); List<Integer> phraseChars = getPhraseChars(phrase); List<Integer> traduction = new java.util.ArrayList<>(); int keyIndex = 0; for (int i = 0; i < phrase.length(); i++) { if (keyIndex == offsets.size()) keyIndex = 0; int current = phraseChars.get(i); int result = current + offsets.get(keyIndex); if (current <= UpperCase_UPPER_LIMIT && result >= LowerCase_LOWER_LIMIT) { int offset = result - UpperCase_UPPER_LIMIT; result = LowerCase_LOWER_LIMIT + offset - 1; } if (result > UpperCase_UPPER_LIMIT && result < LowerCase_LOWER_LIMIT) { result += INTERMEDIATE_OFFSET; } else if (result > LowerCase_UPPER_LIMIT) { result = result - LowerCase_UPPER_LIMIT + UpperCase_LOWER_LIMIT - 1; } traduction.add(result); keyIndex++; } return parseChars(traduction); } public static String decode(String phrase, String key) { List<Integer> offsets = getEncryptationOffsets(key); List<Integer> phraseChars = getPhraseChars(phrase); List<Integer> traduction = new java.util.ArrayList<>(); int keyIndex = 0; for (int i = 0; i < phrase.length(); i++) { if (keyIndex == offsets.size()) keyIndex = 0; int current = phraseChars.get(i); int result = current - offsets.get(keyIndex); if (current >= LowerCase_LOWER_LIMIT && result <= UpperCase_UPPER_LIMIT) { int offset = LowerCase_LOWER_LIMIT - result; result = UpperCase_UPPER_LIMIT - offset + 1; } if (result < LowerCase_LOWER_LIMIT && result > UpperCase_UPPER_LIMIT) { result -= INTERMEDIATE_OFFSET; } else if (result < UpperCase_LOWER_LIMIT) { int offset = UpperCase_LOWER_LIMIT - result; result = LowerCase_UPPER_LIMIT - offset + 1; } traduction.add(result); keyIndex++; } return parseChars(traduction); } }
- import static org.junit.jupiter.api.Assertions.*;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Test;
- import java.util.List;
- import java.util.Random;
- import java.util.stream.Collectors;
- class SolutionTest {
- @Test
- void basicTest() {
- assertEquals("moqik", Kata.encode("abcde", "abcde"));
- assertEquals("abcde", Kata.decode("moqik", "abcde"));
- assertEquals("KMOQS", Kata.encode("ABCDE", "ABCDE"));
- assertEquals("ABCDE", Kata.decode("KMOQS", "ABCDE"));
- assertEquals("GIFUKTG", Kata.encode("DEARGOD", "FGH"));
- assertEquals("DEARGOD", Kata.decode("GIFUKTG", "FGH"));
- assertEquals("ikhwmvi", Kata.encode("deargod", "fgh"));
- assertEquals("deargod", Kata.decode("ikhwmvi", "fgh"));
- assertEquals("IQlOirL", Kata.encode("ALaBamA", "hoME"));
- assertEquals("ALaBamA", Kata.decode("IQlOirL", "hoME"));
- }
- @Test
- void specialCasesTest() {
- assertEquals("", Kata.encode("", ""));
- assertEquals("TernaryLove", Kata.encode("TernaryLove", ""));
- assertEquals("", Kata.encode(null, "abcde"));
- assertEquals("", Kata.encode("", null));
- }
- @Test
- void fullTestSimpleOverflow() {
- assertEquals("CEEpB", Kata.encode("turbo", "TUXON"));
- assertEquals("turbo", Kata.decode("CEEpB", "TUXON"));
- assertEquals("skstzCvzwj", Kata.encode("helloworld", "afgh"));
- assertEquals("helloworld", Kata.decode("skstzCvzwj", "afgh"));
- assertEquals("IKM", Kata.encode("xyz", "ijklmn"));
- assertEquals("xyz", Kata.decode("IKM", "ijklmn"));
- assertEquals("qUlWuZXovQ", Kata.encode("gOdMoRNinG", "uSA"));
- assertEquals("gOdMoRNinG", Kata.decode("qUlWuZXovQ", "uSA"));
- assertEquals("BOywIJdrzafbLMKuCnVqNt", Kata.encode("wElovETerNaRyExpsaNdIj", "dAvID"));
- assertEquals("wElovETerNaRyExpsaNdIj", Kata.decode("BOywIJdrzafbLMKuCnVqNt", "dAvID"));
- }
- @Test
- void fullTestIntermediateOverflow() {
- assertEquals("rhCRDjxXEVyfu", Kata.encode("hOmEsWeEtHoMe", "nOtSoSwEeT"));
- assertEquals("hOmEsWeEtHoMe", Kata.decode("rhCRDjxXEVyfu", "nOtSoSwEeT"));
- assertEquals("cyvNMxgiVt", Kata.encode("WonDErWaLl", "oASiS"));
- assertEquals("WonDErWaLl", Kata.decode("cyvNMxgiVt", "oASiS"));
- assertEquals("lyynmpA", Kata.encode("Zumbido", "wow"));
- assertEquals("Zumbido", Kata.decode("lyynmpA", "wow"));
- }
String generateString() {- String generateString(int bound) {
- Random random = new Random();
byte[] buffer = new byte[random.nextInt(1501)];- byte[] buffer = new byte[random.nextInt(bound + 1)];
- random.nextBytes(buffer);
- return new String(buffer, java.nio.charset.Charset.forName("UTF-8"));
- }
String generatePhrase() {return generateString();- String generatePhrase(int bound) {
- return generateString(bound);
- }
String generateKey() {return generateString();- String generateKey(int bound) {
- return generateString(bound);
- }
- @RepeatedTest(200)
- void randomTestEasyMode() {
- String phrase = generatePhrase(500);
- String key = generateKey(500);
- assertEquals(Kata.encode(phrase, key), encode(phrase, key));
- assertEquals(Kata.decode(phrase, key), decode(phrase, key));
- }
- @RepeatedTest(100)
void randomTest() {String phrase = generatePhrase();String key = generateKey();//assertEquals(Kata.encode(phrase, key), encode(phrase, key));//assertEquals(Kata.decode(phrase, key), decode(phrase, key));assertTrue(true);- void randomTestHardMode() {
- String phrase = generatePhrase(15000);
- String key = generateKey(15000);
- assertEquals(Kata.encode(phrase, key), encode(phrase, key));
- assertEquals(Kata.decode(phrase, key), decode(phrase, key));
- }
- @RepeatedTest(10)
- void randomTestGodMode() {
- String phrase = generatePhrase(150000);
- String key = generateKey(150000);
- assertEquals(Kata.encode(phrase, key), encode(phrase, key));
- assertEquals(Kata.decode(phrase, key), decode(phrase, key));
- }
- // [[ A N S W E R ]]
- // <<-CONSTANTS->>
- private static final int UpperCase_LOWER_LIMIT = 65;
- private static final int UpperCase_UPPER_LIMIT = 90;
- private static final int LowerCase_LOWER_LIMIT = 97;
- private static final int LowerCase_UPPER_LIMIT = 122;
- private static final int INTERMEDIATE_OFFSET = LowerCase_LOWER_LIMIT - UpperCase_UPPER_LIMIT - 1;
- // <<-METHODS->>
- private static int getLastDigit(final int digit) {
StringBuilder sb = new StringBuilder(digit);return sb.charAt(sb.toString().length() - 1);- String str = String.valueOf(digit);
- str = str.substring(str.length() - 1, str.length());
- return Integer.valueOf(str);
- }
- private static List<Integer> getEncryptationOffsets(String key) {
- return key.chars()
- .map(n -> getLastDigit(n) + key.length())
- .boxed()
- .toList();
- }
- private static List<Integer> getPhraseChars(String phrase) {
- return phrase.chars()
- .boxed()
- .collect(Collectors.toList());
- }
- private static String parseChars(List<Integer> traduction) {
- return traduction.stream()
- .map(n -> (char) n.intValue())
- .map(c -> Character.toString(c))
- .collect(Collectors.joining());
- }
- public static String encode(String phrase, String key) {
- List<Integer> offsets = getEncryptationOffsets(key);
- List<Integer> phraseChars = getPhraseChars(phrase);
- List<Integer> traduction = new java.util.ArrayList<>();
- int keyIndex = 0;
- for (int i = 0; i < phrase.length(); i++) {
- if (keyIndex == offsets.size())
- keyIndex = 0;
- int current = phraseChars.get(i);
- int result = current + offsets.get(keyIndex);
- if (current <= UpperCase_UPPER_LIMIT && result >= LowerCase_LOWER_LIMIT) {
- int offset = result - UpperCase_UPPER_LIMIT;
- result = LowerCase_LOWER_LIMIT + offset - 1;
- }
- if (result > UpperCase_UPPER_LIMIT && result < LowerCase_LOWER_LIMIT) {
- result += INTERMEDIATE_OFFSET;
- } else if (result > LowerCase_UPPER_LIMIT) {
- result = result - LowerCase_UPPER_LIMIT + UpperCase_LOWER_LIMIT - 1;
- }
- traduction.add(result);
- keyIndex++;
- }
- return parseChars(traduction);
- }
- public static String decode(String phrase, String key) {
- List<Integer> offsets = getEncryptationOffsets(key);
- List<Integer> phraseChars = getPhraseChars(phrase);
- List<Integer> traduction = new java.util.ArrayList<>();
- int keyIndex = 0;
- for (int i = 0; i < phrase.length(); i++) {
- if (keyIndex == offsets.size())
- keyIndex = 0;
- int current = phraseChars.get(i);
- int result = current - offsets.get(keyIndex);
- if (current >= LowerCase_LOWER_LIMIT && result <= UpperCase_UPPER_LIMIT) {
- int offset = LowerCase_LOWER_LIMIT - result;
- result = UpperCase_UPPER_LIMIT - offset + 1;
- }
- if (result < LowerCase_LOWER_LIMIT && result > UpperCase_UPPER_LIMIT) {
- result -= INTERMEDIATE_OFFSET;
- } else if (result < UpperCase_LOWER_LIMIT) {
- int offset = UpperCase_LOWER_LIMIT - result;
- result = LowerCase_UPPER_LIMIT - offset + 1;
- }
- traduction.add(result);
- keyIndex++;
- }
- return parseChars(traduction);
- }
- }
Strings
Ciphers
Fundamentals
Cryptography
Algorithms
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; import java.util.List; import java.util.Random; import java.util.stream.Collectors; class SolutionTest { @Test void basicTest() { assertEquals("moqik", Kata.encode("abcde", "abcde")); assertEquals("abcde", Kata.decode("moqik", "abcde")); assertEquals("KMOQS", Kata.encode("ABCDE", "ABCDE")); assertEquals("ABCDE", Kata.decode("KMOQS", "ABCDE")); assertEquals("GIFUKTG", Kata.encode("DEARGOD", "FGH")); assertEquals("DEARGOD", Kata.decode("GIFUKTG", "FGH")); assertEquals("ikhwmvi", Kata.encode("deargod", "fgh")); assertEquals("deargod", Kata.decode("ikhwmvi", "fgh")); assertEquals("IQlOirL", Kata.encode("ALaBamA", "hoME")); assertEquals("ALaBamA", Kata.decode("IQlOirL", "hoME")); } @Test void fullTestSimpleOverflow() { assertEquals("CEEpB", Kata.encode("turbo", "TUXON")); assertEquals("turbo", Kata.decode("CEEpB", "TUXON")); assertEquals("skstzCvzwj", Kata.encode("helloworld", "afgh")); assertEquals("helloworld", Kata.decode("skstzCvzwj", "afgh")); assertEquals("IKM", Kata.encode("xyz", "ijklmn")); assertEquals("xyz", Kata.decode("IKM", "ijklmn")); assertEquals("qUlWuZXovQ", Kata.encode("gOdMoRNinG", "uSA")); assertEquals("gOdMoRNinG", Kata.decode("qUlWuZXovQ", "uSA")); assertEquals("BOywIJdrzafbLMKuCnVqNt", Kata.encode("wElovETerNaRyExpsaNdIj", "dAvID")); assertEquals("wElovETerNaRyExpsaNdIj", Kata.decode("BOywIJdrzafbLMKuCnVqNt", "dAvID")); } @Test void fullTestIntermediateOverflow() { assertEquals("rhCRDjxXEVyfu", Kata.encode("hOmEsWeEtHoMe", "nOtSoSwEeT")); assertEquals("hOmEsWeEtHoMe", Kata.decode("rhCRDjxXEVyfu", "nOtSoSwEeT")); assertEquals("cyvNMxgiVt", Kata.encode("WonDErWaLl", "oASiS")); assertEquals("WonDErWaLl", Kata.decode("cyvNMxgiVt", "oASiS")); assertEquals("lyynmpA", Kata.encode("Zumbido", "wow")); assertEquals("Zumbido", Kata.decode("lyynmpA", "wow")); } String generateString() { Random random = new Random(); byte[] buffer = new byte[random.nextInt(1501)]; random.nextBytes(buffer); return new String(buffer, java.nio.charset.Charset.forName("UTF-8")); } String generatePhrase() { return generateString(); } String generateKey() { return generateString(); } @RepeatedTest(100) void randomTest() { String phrase = generatePhrase(); String key = generateKey(); //assertEquals(Kata.encode(phrase, key), encode(phrase, key)); //assertEquals(Kata.decode(phrase, key), decode(phrase, key)); assertTrue(true); } // [[ A N S W E R ]] // <<-CONSTANTS->> private static final int UpperCase_LOWER_LIMIT = 65; private static final int UpperCase_UPPER_LIMIT = 90; private static final int LowerCase_LOWER_LIMIT = 97; private static final int LowerCase_UPPER_LIMIT = 122; private static final int INTERMEDIATE_OFFSET = LowerCase_LOWER_LIMIT - UpperCase_UPPER_LIMIT - 1; // <<-METHODS->> private static int getLastDigit(final int digit) { StringBuilder sb = new StringBuilder(digit); return sb.charAt(sb.toString().length() - 1); } private static List<Integer> getEncryptationOffsets(String key) { return key.chars() .map(n -> getLastDigit(n) + key.length()) .boxed() .toList(); } private static List<Integer> getPhraseChars(String phrase) { return phrase.chars() .boxed() .collect(Collectors.toList()); } private static String parseChars(List<Integer> traduction) { return traduction.stream() .map(n -> (char) n.intValue()) .map(c -> Character.toString(c)) .collect(Collectors.joining()); } public static String encode(String phrase, String key) { List<Integer> offsets = getEncryptationOffsets(key); List<Integer> phraseChars = getPhraseChars(phrase); List<Integer> traduction = new java.util.ArrayList<>(); int keyIndex = 0; for (int i = 0; i < phrase.length(); i++) { if (keyIndex == offsets.size()) keyIndex = 0; int current = phraseChars.get(i); int result = current + offsets.get(keyIndex); if (current <= UpperCase_UPPER_LIMIT && result >= LowerCase_LOWER_LIMIT) { int offset = result - UpperCase_UPPER_LIMIT; result = LowerCase_LOWER_LIMIT + offset - 1; } if (result > UpperCase_UPPER_LIMIT && result < LowerCase_LOWER_LIMIT) { result += INTERMEDIATE_OFFSET; } else if (result > LowerCase_UPPER_LIMIT) { result = result - LowerCase_UPPER_LIMIT + UpperCase_LOWER_LIMIT - 1; } traduction.add(result); keyIndex++; } return parseChars(traduction); } public static String decode(String phrase, String key) { List<Integer> offsets = getEncryptationOffsets(key); List<Integer> phraseChars = getPhraseChars(phrase); List<Integer> traduction = new java.util.ArrayList<>(); int keyIndex = 0; for (int i = 0; i < phrase.length(); i++) { if (keyIndex == offsets.size()) keyIndex = 0; int current = phraseChars.get(i); int result = current - offsets.get(keyIndex); if (current >= LowerCase_LOWER_LIMIT && result <= UpperCase_UPPER_LIMIT) { int offset = LowerCase_LOWER_LIMIT - result; result = UpperCase_UPPER_LIMIT - offset + 1; } if (result < LowerCase_LOWER_LIMIT && result > UpperCase_UPPER_LIMIT) { result -= INTERMEDIATE_OFFSET; } else if (result < UpperCase_LOWER_LIMIT) { int offset = UpperCase_LOWER_LIMIT - result; result = LowerCase_UPPER_LIMIT - offset + 1; } traduction.add(result); keyIndex++; } return parseChars(traduction); } }
- import static org.junit.jupiter.api.Assertions.*;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Test;
- import java.util.List;
- import java.util.Random;
- import java.util.stream.Collectors;
- class SolutionTest {
- @Test
- void basicTest() {
- assertEquals("moqik", Kata.encode("abcde", "abcde"));
- assertEquals("abcde", Kata.decode("moqik", "abcde"));
- assertEquals("KMOQS", Kata.encode("ABCDE", "ABCDE"));
- assertEquals("ABCDE", Kata.decode("KMOQS", "ABCDE"));
- assertEquals("GIFUKTG", Kata.encode("DEARGOD", "FGH"));
- assertEquals("DEARGOD", Kata.decode("GIFUKTG", "FGH"));
- assertEquals("ikhwmvi", Kata.encode("deargod", "fgh"));
- assertEquals("deargod", Kata.decode("ikhwmvi", "fgh"));
- assertEquals("IQlOirL", Kata.encode("ALaBamA", "hoME"));
- assertEquals("ALaBamA", Kata.decode("IQlOirL", "hoME"));
- }
- @Test
- void fullTestSimpleOverflow() {
- assertEquals("CEEpB", Kata.encode("turbo", "TUXON"));
- assertEquals("turbo", Kata.decode("CEEpB", "TUXON"));
- assertEquals("skstzCvzwj", Kata.encode("helloworld", "afgh"));
- assertEquals("helloworld", Kata.decode("skstzCvzwj", "afgh"));
- assertEquals("IKM", Kata.encode("xyz", "ijklmn"));
- assertEquals("xyz", Kata.decode("IKM", "ijklmn"));
- assertEquals("qUlWuZXovQ", Kata.encode("gOdMoRNinG", "uSA"));
- assertEquals("gOdMoRNinG", Kata.decode("qUlWuZXovQ", "uSA"));
- assertEquals("BOywIJdrzafbLMKuCnVqNt", Kata.encode("wElovETerNaRyExpsaNdIj", "dAvID"));
- assertEquals("wElovETerNaRyExpsaNdIj", Kata.decode("BOywIJdrzafbLMKuCnVqNt", "dAvID"));
- }
- @Test
- void fullTestIntermediateOverflow() {
- assertEquals("rhCRDjxXEVyfu", Kata.encode("hOmEsWeEtHoMe", "nOtSoSwEeT"));
- assertEquals("hOmEsWeEtHoMe", Kata.decode("rhCRDjxXEVyfu", "nOtSoSwEeT"));
- assertEquals("cyvNMxgiVt", Kata.encode("WonDErWaLl", "oASiS"));
- assertEquals("WonDErWaLl", Kata.decode("cyvNMxgiVt", "oASiS"));
- assertEquals("lyynmpA", Kata.encode("Zumbido", "wow"));
- assertEquals("Zumbido", Kata.decode("lyynmpA", "wow"));
- }
private String encode(String phrase, String key) {return "";}private String decode(String phrase, String key) {return "";}- String generateString() {
- Random random = new Random();
- byte[] buffer = new byte[random.nextInt(1501)];
- random.nextBytes(buffer);
- return new String(buffer, java.nio.charset.Charset.forName("UTF-8"));
- }
- String generatePhrase() {
- return generateString();
- }
- String generateKey() {
- return generateString();
- }
- @RepeatedTest(100)
- void randomTest() {
//String phrase = generatePhrase();//String key = generateKey();- String phrase = generatePhrase();
- String key = generateKey();
- //assertEquals(Kata.encode(phrase, key), encode(phrase, key));
- //assertEquals(Kata.decode(phrase, key), decode(phrase, key));
assertTrue(true);- assertTrue(true);
- }
- // [[ A N S W E R ]]
- // <<-CONSTANTS->>
- private static final int UpperCase_LOWER_LIMIT = 65;
- private static final int UpperCase_UPPER_LIMIT = 90;
- private static final int LowerCase_LOWER_LIMIT = 97;
- private static final int LowerCase_UPPER_LIMIT = 122;
- private static final int INTERMEDIATE_OFFSET = LowerCase_LOWER_LIMIT - UpperCase_UPPER_LIMIT - 1;
- // <<-METHODS->>
- private static int getLastDigit(final int digit) {
- StringBuilder sb = new StringBuilder(digit);
- return sb.charAt(sb.toString().length() - 1);
- }
- private static List<Integer> getEncryptationOffsets(String key) {
- return key.chars()
- .map(n -> getLastDigit(n) + key.length())
- .boxed()
- .toList();
- }
- private static List<Integer> getPhraseChars(String phrase) {
- return phrase.chars()
- .boxed()
- .collect(Collectors.toList());
- }
- private static String parseChars(List<Integer> traduction) {
- return traduction.stream()
- .map(n -> (char) n.intValue())
- .map(c -> Character.toString(c))
- .collect(Collectors.joining());
- }
- public static String encode(String phrase, String key) {
- List<Integer> offsets = getEncryptationOffsets(key);
- List<Integer> phraseChars = getPhraseChars(phrase);
- List<Integer> traduction = new java.util.ArrayList<>();
- int keyIndex = 0;
- for (int i = 0; i < phrase.length(); i++) {
- if (keyIndex == offsets.size())
- keyIndex = 0;
- int current = phraseChars.get(i);
- int result = current + offsets.get(keyIndex);
- if (current <= UpperCase_UPPER_LIMIT && result >= LowerCase_LOWER_LIMIT) {
- int offset = result - UpperCase_UPPER_LIMIT;
- result = LowerCase_LOWER_LIMIT + offset - 1;
- }
- if (result > UpperCase_UPPER_LIMIT && result < LowerCase_LOWER_LIMIT) {
- result += INTERMEDIATE_OFFSET;
- } else if (result > LowerCase_UPPER_LIMIT) {
- result = result - LowerCase_UPPER_LIMIT + UpperCase_LOWER_LIMIT - 1;
- }
- traduction.add(result);
- keyIndex++;
- }
- return parseChars(traduction);
- }
- public static String decode(String phrase, String key) {
- List<Integer> offsets = getEncryptationOffsets(key);
- List<Integer> phraseChars = getPhraseChars(phrase);
- List<Integer> traduction = new java.util.ArrayList<>();
- int keyIndex = 0;
- for (int i = 0; i < phrase.length(); i++) {
- if (keyIndex == offsets.size())
- keyIndex = 0;
- int current = phraseChars.get(i);
- int result = current - offsets.get(keyIndex);
- if (current >= LowerCase_LOWER_LIMIT && result <= UpperCase_UPPER_LIMIT) {
- int offset = LowerCase_LOWER_LIMIT - result;
- result = UpperCase_UPPER_LIMIT - offset + 1;
- }
- if (result < LowerCase_LOWER_LIMIT && result > UpperCase_UPPER_LIMIT) {
- result -= INTERMEDIATE_OFFSET;
- } else if (result < UpperCase_LOWER_LIMIT) {
- int offset = UpperCase_LOWER_LIMIT - result;
- result = LowerCase_UPPER_LIMIT - offset + 1;
- }
- traduction.add(result);
- keyIndex++;
- }
- return parseChars(traduction);
- }
- }
Strings
Ciphers
Fundamentals
Cryptography
Algorithms
import java.util.List; import java.util.stream.Collectors; public class Kata { // <<-METHODS->> private static int getLastDigit(int n) { String str = String.valueOf(n); str = str.substring(str.length() - 1, str.length()); return Integer.valueOf(str); } public static String encode(String phrase, String key) { List<Integer> adds = key.chars() .map(n -> getLastDigit(n) + key.length()) .boxed() .toList(); List<Integer> phraseChars = phrase.chars() .boxed() .collect(Collectors.toList()); List<Integer> traduction = new java.util.ArrayList<>(); int j = 0; for (int i = 0; i < phrase.length(); i++) { if (j == adds.size()) j = 0; traduction.add(adds.get(j) + phraseChars.get(i)); j++; } return traduction.stream() .map(n -> (char) n.intValue()) .map(ch -> Character.toString(ch)) .collect(Collectors.joining()); } public static String decode(String phrase, String key) { return ""; } }
- import java.util.List;
- import java.util.stream.Collectors;
- public class Kata {
- // <<-METHODS->>
- private static int getLastDigit(int n) {
- String str = String.valueOf(n);
- str = str.substring(str.length() - 1, str.length());
- return Integer.valueOf(str);
- }
private static String encode(String phrase, String key) {- public static String encode(String phrase, String key) {
- List<Integer> adds = key.chars()
- .map(n -> getLastDigit(n) + key.length())
- .boxed()
- .toList();
StringBuilder sb = new StringBuilder();List<Integer> pAscii = phrase.chars()- List<Integer> phraseChars = phrase.chars()
- .boxed()
- .collect(Collectors.toList());
- List<Integer> traduction = new java.util.ArrayList<>();
int counter;for (int i = 0; i < phrase.length(); i += key.length()) {counter = 0;for (int j = i; j < key.length() + i; j++) {traduction.add(pAscii.get(j) + adds.get(counter));counter++;}- int j = 0;
- for (int i = 0; i < phrase.length(); i++) {
- if (j == adds.size()) j = 0;
- traduction.add(adds.get(j) + phraseChars.get(i));
- j++;
- }
- return traduction.stream()
.map(Integer::intValue).map(Character::getName)- .map(n -> (char) n.intValue())
- .map(ch -> Character.toString(ch))
- .collect(Collectors.joining());
- }
- public static String decode(String phrase, String key) {
- return "";
- }
- }
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; import java.util.Random; class SolutionTest { @Test void basicTest() { assertEquals("moqik", Kata.encode("abcde", "abcde")); //assertEquals("abcde", Kata.decode("moqik", "abcde")); assertEquals("KMOQS", Kata.encode("ABCDE", "ABCDE")); //assertEquals("ABCDE", Kata.decode("KMOQS", "ABCDE")); assertEquals("GIFUKTG", Kata.encode("DEARGOD", "FGH")); //assertEquals("DEARGOD", Kata.decode("GIFUKTG", "FGH")); assertEquals("ikhwmvi", Kata.encode("deargod", "fgh")); //assertEquals("deargod", Kata.decode("ikhwmvi", "fgh")); assertEquals("IQlOirL", Kata.encode("ALaBamA", "hoME")); //assertEquals("ALaBamA", Kata.decode("IQlOirL", "hoME")); } @Test void fullTest() { assertEquals("CDEfA", Kata.encode("turbo", "TUXON")); assertEquals("turbo", Kata.decode("CDEfA", "TUXON")); assertEquals("skstzDvzwj", Kata.encode("helloworld", "afgh")); assertEquals("helloworld", Kata.decode("skstzDvzwj", "afgh")); assertEquals("JLN", Kata.encode("xyz", "ijklmn")); assertEquals("xyz", Kata.decode("JLN", "ijklmn")); assertEquals("riDREkxXFVygu", Kata.encode("hOmEsWeEtHoMe", "nOtSoSwEeT")); assertEquals("hOmEsWeEtHoMe", Kata.decode("riDREkxXFVygu", "nOtSoSwEeT")); assertEquals("qUlWuZXovQ", Kata.encode("gOdMoRNinG", "uSA")); assertEquals("gOdMoRNinG", Kata.decode("qUlWuZXovQ", "uSA")); assertEquals("byvNMwgiVt", Kata.encode("WonDErWaLl", "oASiS")); assertEquals("WonDErWaLl", Kata.decode("byvNMwgiVt", "oASiS")); assertEquals("MyynmpA", Kata.encode("Zumbido", "wow")); assertEquals("Zumbido", Kata.decode("MyynmpA", "wow")); assertEquals("BOywAJdrzSfbLMCuCnViNt", Kata.encode("wElovETerNaRyExpsaNdIj", "dAvID")); assertEquals("wElovETerNaRyExpsaNdIj", Kata.decode("BOywAJdrzSfbLMCuCnViNt", "dAvID")); } private String encode(String phrase, String key) { return ""; } private String decode(String phrase, String key) { return ""; } String generateString() { Random random = new Random(); byte[] buffer = new byte[random.nextInt(1501)]; random.nextBytes(buffer); return new String(buffer, java.nio.charset.Charset.forName("UTF-8")); } String generatePhrase() { return generateString(); } String generateKey() { return generateString(); } @RepeatedTest(100) void randomTest() { //String phrase = generatePhrase(); //String key = generateKey(); //assertEquals(Kata.encode(phrase, key), encode(phrase, key)); //assertEquals(Kata.decode(phrase, key), decode(phrase, key)); assertTrue(true); } }
import static org.junit.jupiter.api.Assertions.assertEquals;- import static org.junit.jupiter.api.Assertions.*;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Test;
- import java.util.Random;
- class SolutionTest {
- @Test
- void basicTest() {
- assertEquals("moqik", Kata.encode("abcde", "abcde"));
assertEquals("abcde", Kata.decode("moqik", "abcde"));assertEquals("KMOUS", Kata.encode("ABCDE", "ABCDE"));assertEquals("ABCDE", Kata.decode("KMOUS", "ABCDE"));- //assertEquals("abcde", Kata.decode("moqik", "abcde"));
- assertEquals("KMOQS", Kata.encode("ABCDE", "ABCDE"));
- //assertEquals("ABCDE", Kata.decode("KMOQS", "ABCDE"));
- assertEquals("GIFUKTG", Kata.encode("DEARGOD", "FGH"));
assertEquals("DEARGOD", Kata.decode("GIFUKTG", "FGH"));- //assertEquals("DEARGOD", Kata.decode("GIFUKTG", "FGH"));
- assertEquals("ikhwmvi", Kata.encode("deargod", "fgh"));
assertEquals("deargod", Kata.decode("ikhwmvi", "fgh"));- //assertEquals("deargod", Kata.decode("ikhwmvi", "fgh"));
- assertEquals("IQlOirL", Kata.encode("ALaBamA", "hoME"));
assertEquals("ALaBamA", Kata.decode("IQlOirL", "hoME"));- //assertEquals("ALaBamA", Kata.decode("IQlOirL", "hoME"));
- }
- @Test
- void fullTest() {
- assertEquals("CDEfA", Kata.encode("turbo", "TUXON"));
- assertEquals("turbo", Kata.decode("CDEfA", "TUXON"));
- assertEquals("skstzDvzwj", Kata.encode("helloworld", "afgh"));
- assertEquals("helloworld", Kata.decode("skstzDvzwj", "afgh"));
- assertEquals("JLN", Kata.encode("xyz", "ijklmn"));
- assertEquals("xyz", Kata.decode("JLN", "ijklmn"));
- assertEquals("riDREkxXFVygu", Kata.encode("hOmEsWeEtHoMe", "nOtSoSwEeT"));
- assertEquals("hOmEsWeEtHoMe", Kata.decode("riDREkxXFVygu", "nOtSoSwEeT"));
- assertEquals("qUlWuZXovQ", Kata.encode("gOdMoRNinG", "uSA"));
- assertEquals("gOdMoRNinG", Kata.decode("qUlWuZXovQ", "uSA"));
- assertEquals("byvNMwgiVt", Kata.encode("WonDErWaLl", "oASiS"));
- assertEquals("WonDErWaLl", Kata.decode("byvNMwgiVt", "oASiS"));
- assertEquals("MyynmpA", Kata.encode("Zumbido", "wow"));
- assertEquals("Zumbido", Kata.decode("MyynmpA", "wow"));
- assertEquals("BOywAJdrzSfbLMCuCnViNt", Kata.encode("wElovETerNaRyExpsaNdIj", "dAvID"));
- assertEquals("wElovETerNaRyExpsaNdIj", Kata.decode("BOywAJdrzSfbLMCuCnViNt", "dAvID"));
- }
- private String encode(String phrase, String key) {
- return "";
- }
- private String decode(String phrase, String key) {
- return "";
- }
- String generateString() {
- Random random = new Random();
- byte[] buffer = new byte[random.nextInt(1501)];
- random.nextBytes(buffer);
- return new String(buffer, java.nio.charset.Charset.forName("UTF-8"));
- }
- String generatePhrase() {
- return generateString();
- }
- String generateKey() {
- return generateString();
- }
- @RepeatedTest(100)
- void randomTest() {
String phrase = generatePhrase();String key = generateKey();assertEquals(Kata.encode(phrase, key), encode(phrase, key));assertEquals(Kata.decode(phrase, key), decode(phrase, key));- //String phrase = generatePhrase();
- //String key = generateKey();
- //assertEquals(Kata.encode(phrase, key), encode(phrase, key));
- //assertEquals(Kata.decode(phrase, key), decode(phrase, key));
- assertTrue(true);
- }
- }
Strings
Ciphers
Fundamentals
Cryptography
Algorithms
public class Kata { // <<-METHODS->> private static int getLastDigit(int n) { String str = String.valueOf(n); str = str.substring(str.length() - 1, str.length()); return Integer.valueOf(str); } private static String encode(String phrase, String key) { List<Integer> adds = key.chars() .map(n -> getLastDigit(n) + key.length()) .boxed() .toList(); StringBuilder sb = new StringBuilder(); List<Integer> pAscii = phrase.chars() .boxed() .collect(Collectors.toList()); List<Integer> traduction = new java.util.ArrayList<>(); int counter; for (int i = 0; i < phrase.length(); i += key.length()) { counter = 0; for (int j = i; j < key.length() + i; j++) { traduction.add(pAscii.get(j) + adds.get(counter)); counter++; } } return traduction.stream() .map(Integer::intValue) .map(Character::getName) .collect(Collectors.joining()); } public static String decode(String phrase, String key) { return ""; } }
- public class Kata {
public static String encode(String phrase, String key) {return "";}- // <<-METHODS->>
- private static int getLastDigit(int n) {
- String str = String.valueOf(n);
- str = str.substring(str.length() - 1, str.length());
- return Integer.valueOf(str);
- }
- private static String encode(String phrase, String key) {
- List<Integer> adds = key.chars()
- .map(n -> getLastDigit(n) + key.length())
- .boxed()
- .toList();
- StringBuilder sb = new StringBuilder();
- List<Integer> pAscii = phrase.chars()
- .boxed()
- .collect(Collectors.toList());
- List<Integer> traduction = new java.util.ArrayList<>();
- int counter;
- for (int i = 0; i < phrase.length(); i += key.length()) {
- counter = 0;
- for (int j = i; j < key.length() + i; j++) {
- traduction.add(pAscii.get(j) + adds.get(counter));
- counter++;
- }
- }
- return traduction.stream()
- .map(Integer::intValue)
- .map(Character::getName)
- .collect(Collectors.joining());
- }
- public static String decode(String phrase, String key) {
- return "";
- }
- }
Strings
Ciphers
Fundamentals
Cryptography
Algorithms
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; import java.util.Random; class SolutionTest { @Test void basicTest() { assertEquals("moqik", Kata.encode("abcde", "abcde")); assertEquals("abcde", Kata.decode("moqik", "abcde")); assertEquals("KMOUS", Kata.encode("ABCDE", "ABCDE")); assertEquals("ABCDE", Kata.decode("KMOUS", "ABCDE")); assertEquals("GIFUKTG", Kata.encode("DEARGOD", "FGH")); assertEquals("DEARGOD", Kata.decode("GIFUKTG", "FGH")); assertEquals("ikhwmvi", Kata.encode("deargod", "fgh")); assertEquals("deargod", Kata.decode("ikhwmvi", "fgh")); assertEquals("IQlOirL", Kata.encode("ALaBamA", "hoME")); assertEquals("ALaBamA", Kata.decode("IQlOirL", "hoME")); } @Test void fullTest() { assertEquals("CDEfA", Kata.encode("turbo", "TUXON")); assertEquals("turbo", Kata.decode("CDEfA", "TUXON")); assertEquals("skstzDvzwj", Kata.encode("helloworld", "afgh")); assertEquals("helloworld", Kata.decode("skstzDvzwj", "afgh")); assertEquals("JLN", Kata.encode("xyz", "ijklmn")); assertEquals("xyz", Kata.decode("JLN", "ijklmn")); assertEquals("riDREkxXFVygu", Kata.encode("hOmEsWeEtHoMe", "nOtSoSwEeT")); assertEquals("hOmEsWeEtHoMe", Kata.decode("riDREkxXFVygu", "nOtSoSwEeT")); assertEquals("qUlWuZXovQ", Kata.encode("gOdMoRNinG", "uSA")); assertEquals("gOdMoRNinG", Kata.decode("qUlWuZXovQ", "uSA")); assertEquals("byvNMwgiVt", Kata.encode("WonDErWaLl", "oASiS")); assertEquals("WonDErWaLl", Kata.decode("byvNMwgiVt", "oASiS")); assertEquals("MyynmpA", Kata.encode("Zumbido", "wow")); assertEquals("Zumbido", Kata.decode("MyynmpA", "wow")); assertEquals("BOywAJdrzSfbLMCuCnViNt", Kata.encode("wElovETerNaRyExpsaNdIj", "dAvID")); assertEquals("wElovETerNaRyExpsaNdIj", Kata.decode("BOywAJdrzSfbLMCuCnViNt", "dAvID")); } private String encode(String phrase, String key) { return ""; } private String decode(String phrase, String key) { return ""; } String generateString() { Random random = new Random(); byte[] buffer = new byte[random.nextInt(1501)]; random.nextBytes(buffer); return new String(buffer, java.nio.charset.Charset.forName("UTF-8")); } String generatePhrase() { return generateString(); } String generateKey() { return generateString(); } @RepeatedTest(100) void randomTest() { String phrase = generatePhrase(); String key = generateKey(); assertEquals(Kata.encode(phrase, key), encode(phrase, key)); assertEquals(Kata.decode(phrase, key), decode(phrase, key)); } }
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Test;
- import java.util.Random;
- class SolutionTest {
- @Test
- void basicTest() {
- assertEquals("moqik", Kata.encode("abcde", "abcde"));
- assertEquals("abcde", Kata.decode("moqik", "abcde"));
- assertEquals("KMOUS", Kata.encode("ABCDE", "ABCDE"));
- assertEquals("ABCDE", Kata.decode("KMOUS", "ABCDE"));
- assertEquals("GIFUKTG", Kata.encode("DEARGOD", "FGH"));
- assertEquals("DEARGOD", Kata.decode("GIFUKTG", "FGH"));
- assertEquals("ikhwmvi", Kata.encode("deargod", "fgh"));
- assertEquals("deargod", Kata.decode("ikhwmvi", "fgh"));
- assertEquals("IQlOirL", Kata.encode("ALaBamA", "hoME"));
- assertEquals("ALaBamA", Kata.decode("IQlOirL", "hoME"));
- }
- @Test
- void fullTest() {
- assertEquals("CDEfA", Kata.encode("turbo", "TUXON"));
- assertEquals("turbo", Kata.decode("CDEfA", "TUXON"));
- assertEquals("skstzDvzwj", Kata.encode("helloworld", "afgh"));
- assertEquals("helloworld", Kata.decode("skstzDvzwj", "afgh"));
- assertEquals("JLN", Kata.encode("xyz", "ijklmn"));
- assertEquals("xyz", Kata.decode("JLN", "ijklmn"));
- assertEquals("riDREkxXFVygu", Kata.encode("hOmEsWeEtHoMe", "nOtSoSwEeT"));
- assertEquals("hOmEsWeEtHoMe", Kata.decode("riDREkxXFVygu", "nOtSoSwEeT"));
- assertEquals("qUlWuZXovQ", Kata.encode("gOdMoRNinG", "uSA"));
- assertEquals("gOdMoRNinG", Kata.decode("qUlWuZXovQ", "uSA"));
- assertEquals("byvNMwgiVt", Kata.encode("WonDErWaLl", "oASiS"));
- assertEquals("WonDErWaLl", Kata.decode("byvNMwgiVt", "oASiS"));
- assertEquals("MyynmpA", Kata.encode("Zumbido", "wow"));
- assertEquals("Zumbido", Kata.decode("MyynmpA", "wow"));
- assertEquals("BOywAJdrzSfbLMCuCnViNt", Kata.encode("wElovETerNaRyExpsaNdIj", "dAvID"));
- assertEquals("wElovETerNaRyExpsaNdIj", Kata.decode("BOywAJdrzSfbLMCuCnViNt", "dAvID"));
- }
- private String encode(String phrase, String key) {
- return "";
- }
- private String decode(String phrase, String key) {
- return "";
- }
- String generateString() {
- Random random = new Random();
- byte[] buffer = new byte[random.nextInt(1501)];
- random.nextBytes(buffer);
- return new String(buffer, java.nio.charset.Charset.forName("UTF-8"));
- }
- String generatePhrase() {
- return generateString();
- }
- String generateKey() {
- return generateString();
- }
- @RepeatedTest(100)
- void randomTest() {
- String phrase = generatePhrase();
- String key = generateKey();
- assertEquals(Kata.encode(phrase, key), encode(phrase, key));
- assertEquals(Kata.decode(phrase, key), decode(phrase, key));
- }
- }
Strings
Ciphers
Fundamentals
Cryptography
Algorithms
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; import java.util.Random; class SolutionTest { @Test void basicTest() { assertEquals("moqik", Kata.encode("abcde", "abcde")); assertEquals("abcde", Kata.decode("moqik", "abcde")); assertEquals("KMOUS", Kata.encode("ABCDE", "ABCDE")); assertEquals("ABCDE", Kata.decode("KMOUS", "ABCDE")); assertEquals("GIFUKTG", Kata.encode("DEARGOD", "FGH")); assertEquals("DEARGOD", Kata.decode("GIFUKTG", "FGH")); assertEquals("ikhwmvi", Kata.encode("deargod", "fgh")); assertEquals("deargod", Kata.decode("ikhwmvi", "fgh")); assertEquals("IQlOirL", Kata.encode("ALaBamA", "hoME")); assertEquals("ALaBamA", Kata.decode("IQlOirL", "hoME")); } @Test void fullTest() { assertEquals("CDEfA", Kata.encode("turbo", "TUXON")); assertEquals("turbo", Kata.decode("CDEfA", "TUXON")); assertEquals("skstzDvzwj", Kata.encode("helloworld", "afgh")); assertEquals("helloworld", Kata.decode("skstzDvzwj", "afgh")); assertEquals("JLN", Kata.encode("xyz", "ijklmn")); assertEquals("xyz", Kata.decode("JLN", "ijklmn")); assertEquals("riDREkxXFVygu", Kata.encode("hOmEsWeEtHoMe", "nOtSoSwEeT")); assertEquals("hOmEsWeEtHoMe", Kata.decode("riDREkxXFVygu", "nOtSoSwEeT")); assertEquals("qUlWuZXovQ", Kata.encode("gOdMoRNinG", "uSA")); assertEquals("gOdMoRNinG", Kata.decode("qUlWuZXovQ", "uSA")); assertEquals("byvNMwgiVt", Kata.encode("WonDErWaLl", "oASiS")); assertEquals("WonDErWaLl", Kata.decode("byvNMwgiVt", "oASiS")); assertEquals("BOywAJdrzSfbLMCuCnViNt", Kata.encode("wElovETerNaRyExpsaNdIj", "dAvID")); assertEquals("wElovETerNaRyExpsaNdIj", Kata.decode("BOywAJdrzSfbLMCuCnViNt", "dAvID")); } private String encode(String phrase, String key) { return ""; } private String decode(String phrase, String key) { return ""; } String generateString() { Random random = new Random(); byte[] buffer = new byte[random.nextInt(1501)]; random.nextBytes(buffer); return new String(buffer, java.nio.charset.Charset.forName("UTF-8")); } String generatePhrase() { return generateString(); } String generateKey() { return generateString(); } @RepeatedTest(100) void randomTest() { String phrase = generatePhrase(); String key = generateKey(); assertEquals(Kata.encode(phrase, key), encode(phrase, key)); assertEquals(Kata.decode(phrase, key), decode(phrase, key)); } }
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Test;
- import java.util.Random;
- class SolutionTest {
- @Test
- void basicTest() {
- assertEquals("moqik", Kata.encode("abcde", "abcde"));
- assertEquals("abcde", Kata.decode("moqik", "abcde"));
- assertEquals("KMOUS", Kata.encode("ABCDE", "ABCDE"));
- assertEquals("ABCDE", Kata.decode("KMOUS", "ABCDE"));
- assertEquals("GIFUKTG", Kata.encode("DEARGOD", "FGH"));
- assertEquals("DEARGOD", Kata.decode("GIFUKTG", "FGH"));
- assertEquals("ikhwmvi", Kata.encode("deargod", "fgh"));
- assertEquals("deargod", Kata.decode("ikhwmvi", "fgh"));
- assertEquals("IQlOirL", Kata.encode("ALaBamA", "hoME"));
- assertEquals("ALaBamA", Kata.decode("IQlOirL", "hoME"));
- }
- @Test
- void fullTest() {
- assertEquals("CDEfA", Kata.encode("turbo", "TUXON"));
- assertEquals("turbo", Kata.decode("CDEfA", "TUXON"));
- assertEquals("skstzDvzwj", Kata.encode("helloworld", "afgh"));
- assertEquals("helloworld", Kata.decode("skstzDvzwj", "afgh"));
- assertEquals("JLN", Kata.encode("xyz", "ijklmn"));
- assertEquals("xyz", Kata.decode("JLN", "ijklmn"));
- assertEquals("riDREkxXFVygu", Kata.encode("hOmEsWeEtHoMe", "nOtSoSwEeT"));
- assertEquals("hOmEsWeEtHoMe", Kata.decode("riDREkxXFVygu", "nOtSoSwEeT"));
- assertEquals("qUlWuZXovQ", Kata.encode("gOdMoRNinG", "uSA"));
- assertEquals("gOdMoRNinG", Kata.decode("qUlWuZXovQ", "uSA"));
- assertEquals("byvNMwgiVt", Kata.encode("WonDErWaLl", "oASiS"));
- assertEquals("WonDErWaLl", Kata.decode("byvNMwgiVt", "oASiS"));
- assertEquals("BOywAJdrzSfbLMCuCnViNt", Kata.encode("wElovETerNaRyExpsaNdIj", "dAvID"));
- assertEquals("wElovETerNaRyExpsaNdIj", Kata.decode("BOywAJdrzSfbLMCuCnViNt", "dAvID"));
- }
- private String encode(String phrase, String key) {
- return "";
- }
- private String decode(String phrase, String key) {
- return "";
- }
- String generateString() {
- Random random = new Random();
- byte[] buffer = new byte[random.nextInt(1501)];
- random.nextBytes(buffer);
- return new String(buffer, java.nio.charset.Charset.forName("UTF-8"));
- }
- String generatePhrase() {
- return generateString();
- }
- String generateKey() {
- return generateString();
- }
- @RepeatedTest(100)
- void randomTest() {
- String phrase = generatePhrase();
- String key = generateKey();
- assertEquals(Kata.encode(phrase, key), encode(phrase, key));
- assertEquals(Kata.decode(phrase, key), decode(phrase, key));
- }
- }
Strings
Ciphers
Fundamentals
Cryptography
Algorithms
public class Kata {
public static String encode(String phrase, String key) {
return "";
}
public static String decode(String phrase, String key) {
return "";
}
}
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import java.util.Random;
import java.nio.charset.Charset;
class SolutionTest {
@Test
void basicTest() {
assertEquals("S", Kata.encode("DEARGOD", "afgh"));
assertEquals("DEARGOD", Kata.decode("", "afgh"));
assertEquals("", Kata.encode("", ""));
assertEquals("", Kata.decode("", ""));
}
@Test
void fullTest() {
assertEquals("skstzDvzwj", Kata.encode("helloworld", "afgh"));
assertEquals("helloworld", Kata.decode("skstzDvzwj", "afgh"));
}
private String encode(String phrase, String key) {
return "";
}
private String decode(String phrase, String key) {
return "";
}
String generateString() {
Random random = new Random();
byte[] buffer = new byte[random.nextInt(1501)];
random.nextBytes(buffer);
return new String(buffer, Charset.forName("UTF-8"));
}
String generatePhrase() {
return generateString();
}
String generateKey() {
return generateString();
}
@RepeatedTest(100)
void randomTest() {
String phrase = generatePhrase();
String key = generateKey();
assertEquals(Kata.encode(phrase, key), encode(phrase, key));
assertEquals(Kata.decode(phrase, key), decode(phrase, key));
}
}