Skip to content

Commit 7bc7ca5

Browse files
committed
Problem 49 i.e. P3.04 done for Java
1 parent 29cbfd1 commit 7bc7ca5

File tree

3 files changed

+88
-0
lines changed
  • java8
    • src
      • main/java/com/shekhargulati/ninetynine_problems/java8/_03_logic_and_codes
      • test/java/com/shekhargulati/ninetynine_problems/java8/_03_logic_and_codes

3 files changed

+88
-0
lines changed

java8/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,3 +742,32 @@ Skipping this problem for now.
742742
### [P48](https://github.com/shekhargulati/99-problems/blob/master/java8/src/main/java/com/shekhargulati/ninetynine_problems/java8/_03_logic_and_codes/P48.java) **(\*\*) Truth tables for logical expressions (3).**
743743

744744
Skipping this problem for now.
745+
746+
747+
### [P49](https://github.com/shekhargulati/99-problems/blob/master/java8/src/main/java/com/shekhargulati/ninetynine_problems/java8/_03_logic_and_codes/P49.java) **(\*\*) Gray code.**
748+
749+
An n-bit Gray code is a sequence of n-bit strings constructed according to certain rules. For example,
750+
```
751+
n = 1: C(1) = ['0','1'].
752+
n = 2: C(2) = ['00','01','11','10'].
753+
n = 3: C(3) = ['000','001','011','010','110','111','101','100'].
754+
```
755+
```java
756+
@Test
757+
public void shouldFindGrayCodeWhenNIs1() throws Exception {
758+
List<String> graySequence = P49.gray(1);
759+
assertThat(graySequence, contains("0", "1"));
760+
}
761+
762+
@Test
763+
public void shouldFindGrayCodeWhenNIs2() throws Exception {
764+
List<String> graySequence = P49.gray(2);
765+
assertThat(graySequence, contains("00", "01", "11", "10"));
766+
}
767+
768+
@Test
769+
public void shouldFindGrayCodeWhenNIs3() throws Exception {
770+
List<String> graySequence = P49.gray(3);
771+
assertThat(graySequence, contains("000", "001", "011", "010", "110", "111", "101", "100"));
772+
}
773+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.shekhargulati.ninetynine_problems.java8._03_logic_and_codes;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.stream.Stream;
8+
9+
import static java.util.stream.Collectors.toList;
10+
11+
/**
12+
* (**) Gray code.
13+
* An n-bit Gray code is a sequence of n-bit strings constructed according to certain rules. For example,
14+
* n = 1: C(1) = ['0','1'].
15+
* n = 2: C(2) = ['00','01','11','10'].
16+
* n = 3: C(3) = ['000','001','011','010','110','111','101','100'].
17+
*/
18+
public class P49 {
19+
20+
public static List<String> gray(int n) {
21+
if (n == 1) {
22+
return Arrays.asList("0", "1");
23+
}
24+
List<String> original = gray(n - 1);
25+
List<String> reversed = new ArrayList<>(original);
26+
Collections.reverse(reversed);
27+
return Stream.concat(original.stream().map(s -> "0" + s), reversed.stream().map(s -> "1" + s)).collect(toList());
28+
}
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.shekhargulati.ninetynine_problems.java8._03_logic_and_codes;
2+
3+
import org.junit.Test;
4+
5+
import java.util.List;
6+
7+
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
8+
import static org.junit.Assert.assertThat;
9+
10+
public class P49Test {
11+
12+
@Test
13+
public void shouldFindGrayCodeWhenNIs1() throws Exception {
14+
List<String> graySequence = P49.gray(1);
15+
assertThat(graySequence, contains("0", "1"));
16+
}
17+
18+
@Test
19+
public void shouldFindGrayCodeWhenNIs2() throws Exception {
20+
List<String> graySequence = P49.gray(2);
21+
assertThat(graySequence, contains("00", "01", "11", "10"));
22+
}
23+
24+
@Test
25+
public void shouldFindGrayCodeWhenNIs3() throws Exception {
26+
List<String> graySequence = P49.gray(3);
27+
assertThat(graySequence, contains("000", "001", "011", "010", "110", "111", "101", "100"));
28+
}
29+
30+
}

0 commit comments

Comments
 (0)