diff --git a/docs/README.md b/docs/README.md index aced7bf48..9db92871a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -25,7 +25,7 @@ slug. | [1 kyu](/kata/1-kyu/index.md) | [2 kyu](/kata/2-kyu/index.md) | [3 kyu](/kata/3-kyu/index.md) | [4 kyu](/kata/4-kyu/index.md) | [5 kyu](/kata/5-kyu/index.md) | [6 kyu](/kata/6-kyu/index.md) | [7 kyu](/kata/7-kyu/index.md) | [8 kyu](/kata/8-kyu/index.md) | [beta](/kata/beta/index.md) | [retired](/kata/retired/index.md) | |:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:---------------------------:|:---------------------------------:| -| 0 | 1 | 2 | 26 | 48 | 445 | 606 | 228 | 58 | 82 | +| 0 | 1 | 2 | 26 | 48 | 445 | 606 | 228 | 59 | 82 | **Note:** The source code is written in Java 17 and may use language features that are incompatible with Java 8, 11. diff --git a/kata/beta/index.md b/kata/beta/index.md index 5320947f7..58e3f0d9b 100644 --- a/kata/beta/index.md +++ b/kata/beta/index.md @@ -73,3 +73,5 @@ - [Who's Next in Line?](whos-next-in-line) # Y - [Your Basic FizzBuzz kata](your-basic-fizzbuzz-kata) +# Z +- [Zeta Swap](zeta-swap) diff --git a/kata/beta/zeta-swap/README.md b/kata/beta/zeta-swap/README.md new file mode 100644 index 000000000..ed0eb5ff2 --- /dev/null +++ b/kata/beta/zeta-swap/README.md @@ -0,0 +1,21 @@ +# [Zeta Swap](https://www.codewars.com/kata/zeta-swap "https://www.codewars.com/kata/6780126afa635e648148397e") + +Hello Detective! The Ministry of Magic has suddenly fallen while we were in the middle of our annual Tetrawizard Tournament. A murder has +been spotted within the chambers of Slytherin. Our only witness is Ron. But, as you know, Ron was preoccupied with Lavendar while it +happened. He is unsure what letter he saw on the back of the coats of the suspects since he reads right to left. + +Your task is to implement a function that takes a string and swaps each letter with its "opposite" in the alphabet (e.g., 'a' ↔ 'z', 'b' ↔ 'y', etc.), +but only if the letter occurs more than once in the string. + +- Non-alphabet characters (e.g., numbers, punctuation, whitespace) should remain unchanged. +- The function should preserve the case of the letters (e.g., 'A' ↔ 'Z', 'b' ↔ 'y'). +- Single-occurrence letters are not swapped. + +Input + +- A string containing alphabetic and non-alphabetic characters. +- Length of the string: 0 ≤ length ≤ 10^5. + +Output + +- A string with swapped letters where applicable, and all other characters unchanged. \ No newline at end of file diff --git a/kata/beta/zeta-swap/main/ZetaSwap.java b/kata/beta/zeta-swap/main/ZetaSwap.java new file mode 100644 index 000000000..0902acbca --- /dev/null +++ b/kata/beta/zeta-swap/main/ZetaSwap.java @@ -0,0 +1,10 @@ +import static java.util.stream.Collectors.counting; +import static java.util.stream.Collectors.groupingBy; +import static java.util.stream.Collectors.joining; + +interface ZetaSwap { + static String swapMurder(String suspects) { + var count = suspects.chars().boxed().collect(groupingBy(c -> c | 32, counting())); + return suspects.chars().mapToObj(c -> "" + (char) (Character.isLetter(c) && count.get(c | 32) > 1 ? (c > 96 ? 219 : 155) - c : c)).collect(joining()); + } +} \ No newline at end of file diff --git a/kata/beta/zeta-swap/test/SolutionTest.java b/kata/beta/zeta-swap/test/SolutionTest.java new file mode 100644 index 000000000..66fb60b97 --- /dev/null +++ b/kata/beta/zeta-swap/test/SolutionTest.java @@ -0,0 +1,18 @@ +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +class SolutionTest { + @ParameterizedTest + @CsvSource(textBlock = """ + abcabc, zyxzyx + aabc, zzbc + abc, abc + HeLLo, HeOOo + 123 !@# aabc, 123 !@# zzbc + """) + void sample(String suspects, String swapped) { + assertEquals(swapped, ZetaSwap.swapMurder(suspects)); + } +} \ No newline at end of file