diff --git a/sitou/src/B015_7segment.java b/sitou/src/B015_7segment.java new file mode 100644 index 0000000000000000000000000000000000000000..ea72e4f243ec6ca1c7a31ab8c518a7ee131ecde0 --- /dev/null +++ b/sitou/src/B015_7segment.java @@ -0,0 +1,180 @@ +package src; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Scanner; + +public class B015_7segment { + public static void main(String[] args) { + execute(); + } + + static void execute() { + final InputData inputData = input(); + final Result result = storeJudgementResult(inputData); + output(result); + } + + private static InputData input() { + final Scanner scan = new Scanner(System.in); + final List leftSegment = new ArrayList<>(); + final List rightSegment = new ArrayList<>(); + + try { + for (int i = 0; i < 7; i++) { + leftSegment.add(scan.nextInt()); + } + for (int i = 0; i < 7; i++) { + rightSegment.add(scan.nextInt()); + } + } finally { + scan.close(); + } + + return new InputData(leftSegment, rightSegment); + + } + + /** + * 結果を格納する. + * + * @param inputData 入力データ + * @return 結果を格納したデータ + */ + public static Result storeJudgementResult(InputData inputData) { + final List result = new ArrayList<>(); + + result.add(checkWhetherNumber(inputData)); + + // 対称移動 + InputData insideOutData = insideOut(inputData); + result.add(checkWhetherNumber(insideOutData)); + + // 対称移動+上下反転=回転移動 + InputData upsideDownData = upsideDown(insideOutData); + result.add(checkWhetherNumber(upsideDownData)); + + return new Result(result); + + } + + /** + * 数字かどうか判定した結果(Yes/No)を返す. + * + * @param inputData 入力データ + * @return "Yes"/"No" + */ + public static String checkWhetherNumber(InputData inputData) { + // リストを1つの文字列にする + String ls = ""; + String rs = ""; + for (Integer n : inputData.getLeftSegment()) { + ls += n; + } + for (Integer n : inputData.getRightSegment()) { + rs += n; + } + + final boolean isNumberLeft = isNumber(ls); + final boolean isNumberRight = isNumber(rs); + + String isNumberBoth = "No"; + if (isNumberLeft && isNumberRight) { + isNumberBoth = "Yes"; + } + + return isNumberBoth; + } + + /** + * 数字かどうか判定する. + * + * @param seg セグメントディスプレイの状態(数字7桁) + * @return true/false + */ + private static boolean isNumber(final String seg) { + for (NumberPatterns numberPattern : NumberPatterns.values()) { + if (seg.equals(numberPattern.getPattern())) { + return true; + } + } + return false; + } + + /** + * セグメントディスプレイの表示を対称移動する. + * + * @param inputData 入力データ + * @return セグメントディスプレイの状態 + */ + public static InputData insideOut(final InputData inputData) { + List leftSegment = new ArrayList<>(inputData.getLeftSegment()); + List rightSegment = new ArrayList<>(inputData.getRightSegment()); + Collections.swap(leftSegment, 1, 5); + Collections.swap(leftSegment, 2, 4); + Collections.swap(rightSegment, 1, 5); + Collections.swap(rightSegment, 2, 4); + + List kari = new ArrayList<>(leftSegment); + leftSegment = new ArrayList<>(rightSegment); + rightSegment = new ArrayList<>(kari); + + return new InputData(leftSegment, rightSegment); + } + + /** + * セグメントディスプレイの表示を上下反転する. + * + * @param inputData 入力データ + * @return セグメントディスプレイの状態 + */ + public static InputData upsideDown(final InputData inputData) { + List leftSegment = new ArrayList<>(inputData.getLeftSegment()); + List rightSegment = new ArrayList<>(inputData.getRightSegment()); + Collections.swap(leftSegment, 0, 3); + Collections.swap(leftSegment, 1, 2); + Collections.swap(leftSegment, 4, 5); + Collections.swap(rightSegment, 0, 3); + Collections.swap(rightSegment, 1, 2); + Collections.swap(rightSegment, 4, 5); + + return new InputData(leftSegment, rightSegment); + } + + public enum NumberPatterns { + // 各数字に対応する7セグメントディスプレイの状態 + ZERO("1111110"), + ONE("0110000"), + TWO("1101101"), + THREE("1111001"), + FOUR("0110011"), + FIVE("1011011"), + SIX("1011111"), + SEVEN("1110010"), + EIGHT("1111111"), + NINE("1111011"); + + private final String pattern; + + private NumberPatterns(final String pattern) { + this.pattern = pattern; + } + + public final String getPattern() { + return pattern; + } + } + + /** + * 結果を出力する. + * + * @param result 結果を格納したデータ + */ + private static void output(final Result result) { + for (int i = 0; i < 3; i++) { + System.out.println(result.getIsNumber().get(i)); + } + } + +} diff --git a/sitou/src/InputData.java b/sitou/src/InputData.java new file mode 100644 index 0000000000000000000000000000000000000000..19809a10f9707e8c443742830dd267be9a153cda --- /dev/null +++ b/sitou/src/InputData.java @@ -0,0 +1,22 @@ +package src; + +import java.util.List; + +public class InputData { + private final List leftSegment; + private final List rightSegment; + + public InputData(final List leftSegment, final List rightSegment) { + this.leftSegment = leftSegment; + this.rightSegment = rightSegment; + } + + public final List getLeftSegment() { + return leftSegment; + } + + public final List getRightSegment() { + return rightSegment; + } + +} diff --git a/sitou/src/Result.java b/sitou/src/Result.java new file mode 100644 index 0000000000000000000000000000000000000000..fcc302e2595846896d714801d0a20be71f9be510 --- /dev/null +++ b/sitou/src/Result.java @@ -0,0 +1,15 @@ +package src; + +import java.util.List; + +public class Result { + final List isNumber; + + Result(final List isNumber) { + this.isNumber = isNumber; + } + + public final List getIsNumber() { + return isNumber; + } +} diff --git a/sitou/test/B015_Test.java b/sitou/test/B015_Test.java new file mode 100644 index 0000000000000000000000000000000000000000..8a18bcf8881c3a05979fbf64ba389df01290c936 --- /dev/null +++ b/sitou/test/B015_Test.java @@ -0,0 +1,56 @@ +package test; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.Test; +import src.B015_7segment; +import src.InputData; +import src.Result; + +public class B015_Test { + @Test + public void 正しく判定できるかテスト() { + final InputData inputData = new InputData(Arrays.asList(1, 1, 1, 1, 0, 1, 1), Arrays + .asList(0, 1, 1, 0, 0, 0, 0)); + final Result result = B015_7segment.storeJudgementResult(inputData); + final List actual = result.getIsNumber(); + + final List expected = Arrays.asList("Yes", "No", "No"); + + assertThat(actual, is(expected)); + } + + @Test + public void 正しく表裏反転できるかテスト() { + InputData inputData = new InputData(Arrays.asList(1, 1, 1, 1, 0, 1, 1), Arrays + .asList(0, 1, 1, 0, 0, 0, 0)); + InputData insideOutData = B015_7segment.insideOut(inputData); + List actualLeft = new ArrayList<>(insideOutData.getLeftSegment()); + List actualRight = new ArrayList<>(insideOutData.getRightSegment()); + + List expectedLeft = Arrays.asList(0, 0, 0, 0, 1, 1, 0); + List expectedRight = Arrays.asList(1, 1, 0, 1, 1, 1, 1); + + assertThat(actualLeft, is(expectedLeft)); + assertThat(actualRight, is(expectedRight)); + } + + @Test + public void 正しく上下反転できるかテスト() { + InputData inputData = new InputData(Arrays.asList(0, 0, 0, 0, 1, 1, 0), Arrays + .asList(1, 1, 0, 1, 1, 1, 1)); + InputData upsideDownData = B015_7segment.upsideDown(inputData); + List actualLeft = new ArrayList<>(upsideDownData.getLeftSegment()); + List actualRight = new ArrayList<>(upsideDownData.getRightSegment()); + + List expectedLeft = Arrays.asList(0, 0, 0, 0, 1, 1, 0); + List expectedRight = Arrays.asList(1, 0, 1, 1, 1, 1, 1); + + assertThat(actualLeft, is(expectedLeft)); + assertThat(actualRight, is(expectedRight)); + } + +}