Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | 444 | 606 | 228 | 56 | 82 |
| 0 | 1 | 2 | 26 | 48 | 445 | 606 | 228 | 56 | 82 |

**Note:** The source code is written in Java 17 and may use language features that are incompatible
with Java 8, 11.
Expand Down
39 changes: 39 additions & 0 deletions kata/6-kyu/even-gray-code/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# [Even Gray Code](https://www.codewars.com/kata/even-gray-code "https://www.codewars.com/kata/66dc904c177237f5b3e9eb5f")

Background:

The Gray Code is a representation of binary integers in which successive integers differ in only 1 bit. The bit switched at each step is the
one that yields the smallest value not yet represented. See [Gray Code](https://www.codewars.com/kata/5416ce834c2460b4d300042d).

The *"Even Gray Code"* requires successive binary integers to differ in an **even** number of bits. The bits changed at each step should
yield the smallest value not yet represented in the standard binary encoding.

Example - The 4-bit Even Gray Code:
```
0000 Start at 0
0011 Switch bits 1,2
0101 Switch bits 2,3
0110 Switch bits 1,2
1001 Switch all 4 bits
1010 Switch bits 1,2
1100 Switch bits 2,3
1111 Switch bits 1,2
```

Inputs:

The `size` of the Even Gray Code, a integer between 2 and 20.

The `position` where we want the Even Gray Code value, a non-negative integer guaranteed to be smaller than the number of elements in the
Even Gray Code of length `size`.

Output:

A string representing the value at position `position` of the Even Gray Code of length `size`.

Example:

`even_gray_code(4,5) should return '1010'`, because, as shown above, `1010` is the value of the Even Gray Code of length 4 at position 5.
(The first position is position 0.)

Source: International Collegiate Programming Contest, North Central North American Regional, 2023.
5 changes: 5 additions & 0 deletions kata/6-kyu/even-gray-code/main/GrayCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface GrayCode {
static String evenGrayCode(int size, int position) {
return ("%" + size + "s").formatted(Integer.toBinaryString(position) + Integer.bitCount(position) % 2).replace(' ', '0');
}
}
23 changes: 23 additions & 0 deletions kata/6-kyu/even-gray-code/test/GrayCodeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class GrayCodeTest {
@ParameterizedTest
@CsvSource(textBlock = """
2, 0, 00
2, 1, 11
3, 0, 000
3, 1, 011
3, 2, 101
3, 3, 110
4, 5, 1010
5, 8, 10001
10, 230, 0111001101
20, 17, 00000000000000100010
""")
void sample(int size, int position, String code) {
assertEquals(code, GrayCode.evenGrayCode(size, position));
}
}
1 change: 1 addition & 0 deletions kata/6-kyu/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
- [Esolang Interpreters #1 - Introduction to Esolangs and My First Interpreter (MiniStringFuck)](esolang-interpreters-number-1-introduction-to-esolangs-and-my-first-interpreter-ministringfuck)
- [Estimating Amounts of Subsets](estimating-amounts-of-subsets)
- [Even Fibonacci Sum](even-fibonacci-sum)
- [Even Gray Code](even-gray-code)
- [Everybody hates Mondays](everybody-hates-mondays)
- [Evil Autocorrect Prank](evil-autocorrect-prank)
- [Experimenting with a sequence of complex numbers](experimenting-with-a-sequence-of-complex-numbers)
Expand Down