P49 { public static List<String> gray(int n) { if (n == 1) { return Arrays.asList("0", "1"); } List<String> original = gray(n - 1); List<String> reversed = new ArrayList<>(original); Collections.reverse(reversed); return Stream.concat(original.stream().map(s -> "0" + s), reversed.stream().map(s -> "1" + s)).collect(toList()); }  static List<String> gray(int n);  }
@Test public void shouldFindGrayCodeWhenNIs1() throws Exception { List<String> graySequence = P49.gray(1); assertThat(graySequence, contains("0", "1")); }
@Test public void shouldFindGrayCodeWhenNIs2() throws Exception { List<String> graySequence = P49.gray(2); assertThat(graySequence, contains("00", "01", "11", "10")); }
@Test public void shouldFindGrayCodeWhenNIs3() throws Exception { List<String> graySequence = P49.gray(3); assertThat(graySequence, contains("000", "001", "011", "010", "110", "111", "101", "100")); }