Guest User

Untitled

a guest
Jan 23rd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.List;
  4.  
  5. import com.google.common.annotations.VisibleForTesting;
  6.  
  7. public class SudokuSolver {
  8.  
  9. /**
  10. * Write a function that takes a list of integers in no particular
  11. * order and returns true if it has exactly the numbers 1 through 9.
  12. * <p>
  13. * If it has a number that is not 1 through 9, it returns false.
  14. * If it does not have ALL the numbers 1 through 9 it returns false.
  15. * For example:
  16. * <p>
  17. * [ ] -> False
  18. * None -> False
  19. * [1, 2] -> False
  20. * [1, 2, 3, 4, 5, 9, 7, 6, 8] -> True
  21. * [-1] -> False
  22. * [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] -> False
  23. * [1, 1, 1, 1, 1, 1, 1, 1, 1] -> False
  24. */
  25. public static void main(String... args) {
  26.  
  27. }
  28.  
  29. public static boolean isOneThroughNine(List<Integer> input) {
  30. if (input.size() != 9) {
  31. return false;
  32. }
  33.  
  34. Collections.sort(input);
  35.  
  36. for (int i = 1; i < 10; i++) {
  37. if (input.get(i - 1) != i) {
  38. return false;
  39. }
  40. }
  41. return true;
  42. }
  43.  
  44. /**
  45. * Write a function that takes a list of integers of already chosen
  46. * numbers 1 through 9
  47. * (eg. [3, 6]) and a next number to be chosen. Check if this is a
  48. * valid choice.
  49. * <p>
  50. * For example
  51. * <p>
  52. * valid_choice(3, [3, 6]) -> False
  53. * valid_choice(10, [3, 6]) -> False
  54. * valid_choice(5, [3, 6]) -> True
  55. */
  56. public static boolean isValidChoice(int numberToCheck, List<Integer> alreadyUsed) {
  57. return numberToCheck >= 1 &&
  58. numberToCheck <= 9 &&
  59. !alreadyUsed.contains(numberToCheck);
  60. }
  61.  
  62. /**
  63. * Given a 9 x 9 (2-dimensional array) and validate it.
  64. * <p>
  65. * Valid matrix
  66. * <p>
  67. * [ [5, 3, 4, 6, 7, 8, 9, 1, 2],
  68. * [6, 7, 2, 1, 9, 5, 3, 4, 8],
  69. * [1, 9, 8, 3, 4, 2, 5, 6, 7],
  70. * <p>
  71. * [8, 5, 9, 7, 6, 1, 4, 2, 3],
  72. * [4, 2, 6, 8, 5, 3, 7, 9, 1],
  73. * [7, 1, 3, 9, 2, 4, 8, 5, 6],
  74. * <p>
  75. * [9, 6, 1, 5, 3, 7, 2, 8, 4],
  76. * [2, 8, 7, 4, 1, 9, 6, 3, 5],
  77. * [3, 4, 5, 2, 8, 6, 1, 7, 9]]
  78. */
  79.  
  80. public static boolean validSudoku(int[][] input) {
  81.  
  82. return validSudokuRowsAndColums(input) &&
  83. validSudokuGrids(input);
  84. }
  85.  
  86. @VisibleForTesting
  87. static boolean validSudokuRowsAndColums(int[][] input) {
  88. for (int i = 0; i < 9; i++) {
  89. List<Integer> toCheckRow = new ArrayList<>();
  90. List<Integer> toCheckColumn = new ArrayList<>();
  91. for (int j = 0; j < 9; j++) {
  92. toCheckRow.add(input[i][j]);
  93. toCheckColumn.add(input[j][i]);
  94. }
  95. if (!isOneThroughNine(toCheckRow) || !isOneThroughNine(toCheckColumn)) {
  96. return false;
  97. }
  98. }
  99. return true;
  100. }
  101.  
  102. @VisibleForTesting
  103. static boolean validSudokuGrids(int[][] input) {
  104. for (int gridXOrigin = 0; gridXOrigin < 7; gridXOrigin += 3) {
  105. for (int gridYOrigin = 0; gridYOrigin < 7; gridYOrigin += 3) {
  106. List<Integer> toCheck = new ArrayList<>();
  107. for (int x = gridXOrigin; x < gridXOrigin + 3; x++) {
  108. for (int y = gridYOrigin; y < gridYOrigin + 3; y++) {
  109. toCheck.add(input[x][y]);
  110. }
  111. }
  112. if (!isOneThroughNine(toCheck)) {
  113. return false;
  114. }
  115. }
  116. }
  117. return true;
  118. }
  119.  
  120. }
Add Comment
Please, Sign In to add comment