-
Notifications
You must be signed in to change notification settings - Fork 457
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
subjects/java/checkpoints/valid-sudoku/ExerciseRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
public class ExerciseRunner { | ||
public static void main(String[] args) { | ||
ValidSudoku validSudoku = new ValidSudoku(); | ||
|
||
// Test case 1 | ||
char[][] board1 = { | ||
{'5', '3', '.', '.', '7', '.', '.', '.', '.'}, | ||
{'6', '.', '.', '1', '9', '5', '.', '.', '.'}, | ||
{'.', '9', '8', '.', '.', '.', '.', '6', '.'}, | ||
{'8', '.', '.', '.', '6', '.', '.', '.', '3'}, | ||
{'4', '.', '.', '8', '.', '3', '.', '.', '1'}, | ||
{'7', '.', '.', '.', '2', '.', '.', '.', '6'}, | ||
{'.', '6', '.', '.', '.', '.', '2', '8', '.'}, | ||
{'.', '.', '.', '4', '1', '9', '.', '.', '5'}, | ||
{'.', '.', '.', '.', '8', '.', '.', '7', '9'} | ||
}; | ||
System.out.println("Is board1 valid? " + validSudoku.isValidSudoku(board1)); // Expected output: true | ||
|
||
// Test case 2 | ||
char[][] board2 = { | ||
{'8', '3', '.', '.', '7', '.', '.', '.', '.'}, | ||
{'6', '.', '.', '1', '9', '5', '.', '.', '.'}, | ||
{'.', '9', '8', '.', '.', '.', '.', '6', '.'}, | ||
{'8', '.', '.', '.', '6', '.', '.', '.', '3'}, | ||
{'4', '.', '.', '8', '.', '3', '.', '.', '1'}, | ||
{'7', '.', '.', '.', '2', '.', '.', '.', '6'}, | ||
{'.', '6', '.', '.', '.', '.', '2', '8', '.'}, | ||
{'.', '.', '.', '4', '1', '9', '.', '.', '5'}, | ||
{'.', '.', '.', '.', '8', '.', '.', '7', '9'} | ||
}; | ||
System.out.println("Is board2 valid? " + validSudoku.isValidSudoku(board2)); // Expected output: false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
## Valid Sudoku | ||
|
||
### Instructions | ||
|
||
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: | ||
|
||
1. Each row must contain the digits 1-9 without repetition. | ||
2. Each column must contain the digits 1-9 without repetition. | ||
3. Each of the nine 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition. | ||
|
||
The Sudoku board could be partially filled, where empty cells are filled with the character '.'. | ||
|
||
### Expected Class | ||
|
||
```java | ||
public class ValidSudoku { | ||
public boolean isValidSudoku(char[][] board) { | ||
// Implementation to determine if the Sudoku board is valid | ||
} | ||
} | ||
``` | ||
|
||
### Usage | ||
|
||
Here is a possible `ExerciseRunner.java` to test your class: | ||
|
||
```java | ||
public class ExerciseRunner { | ||
public static void main(String[] args) { | ||
ValidSudoku validSudoku = new ValidSudoku(); | ||
|
||
// Test case 1 | ||
char[][] board1 = { | ||
{'5', '3', '.', '.', '7', '.', '.', '.', '.'}, | ||
{'6', '.', '.', '1', '9', '5', '.', '.', '.'}, | ||
{'.', '9', '8', '.', '.', '.', '.', '6', '.'}, | ||
{'8', '.', '.', '.', '6', '.', '.', '.', '3'}, | ||
{'4', '.', '.', '8', '.', '3', '.', '.', '1'}, | ||
{'7', '.', '.', '.', '2', '.', '.', '.', '6'}, | ||
{'.', '6', '.', '.', '.', '.', '2', '8', '.'}, | ||
{'.', '.', '.', '4', '1', '9', '.', '.', '5'}, | ||
{'.', '.', '.', '.', '8', '.', '.', '7', '9'} | ||
}; | ||
System.out.println("Is board1 valid? " + validSudoku.isValidSudoku(board1)); // Expected output: true | ||
|
||
// Test case 2 | ||
char[][] board2 = { | ||
{'8', '3', '.', '.', '7', '.', '.', '.', '.'}, | ||
{'6', '.', '.', '1', '9', '5', '.', '.', '.'}, | ||
{'.', '9', '8', '.', '.', '.', '.', '6', '.'}, | ||
{'8', '.', '.', '.', '6', '.', '.', '.', '3'}, | ||
{'4', '.', '.', '8', '.', '3', '.', '.', '1'}, | ||
{'7', '.', '.', '.', '2', '.', '.', '.', '6'}, | ||
{'.', '6', '.', '.', '.', '.', '2', '8', '.'}, | ||
{'.', '.', '.', '4', '1', '9', '.', '.', '5'}, | ||
{'.', '.', '.', '.', '8', '.', '.', '7', '9'} | ||
}; | ||
System.out.println("Is board2 valid? " + validSudoku.isValidSudoku(board2)); // Expected output: false | ||
} | ||
} | ||
``` | ||
|
||
### Expected Output | ||
|
||
```shell | ||
$ javac *.java -d build | ||
$ java -cp build ExerciseRunner | ||
Is board1 valid? true | ||
Is board2 valid? false | ||
$ | ||
``` |