added README and testdata

master
Lothar Buchholz 5 years ago
parent c6669f7888
commit f5aa725b72

@ -0,0 +1,37 @@
# Sudoku verifier
## Definition
Sudokus are logic number puzzles in which cells in a 9×9 grid must be populated with
the digits 1 to 9 in a way that each digit occurrs only once in each unit (that is column,
row or block = 3×3 sub grid).
![a solved simple sudoku](sudoku.gif)
## Goal
Validate if a given solution for a Sudoku is valid, by checking that the digit
1 to 9 appear only once in column, in each row and in each of the 9 sub grids.
### Input
a sequence of 81 digits, organized in 9 columns and 9 rows
### Output
A boolean value, if the grid is valid.
### Constraints
1 <= `digit` <= 9
### Examples
**Input:**
```
9 8 1 5 2 3 6 4 7
6 3 4 8 7 9 2 5 1
2 7 5 1 4 6 9 8 3
1 9 6 4 8 7 5 3 2
5 4 8 3 1 2 7 6 9
7 2 3 6 9 5 4 1 8
3 1 2 7 5 4 8 9 6
4 6 9 2 3 8 1 7 5
8 5 7 9 6 1 3 2 4
```
**Output:**
`true`

@ -1 +1 @@
rootProject.name = 'template' rootProject.name = 'sudoku-verifier'

@ -1,8 +1,10 @@
import java.io.*; import java.io.*;
import java.util.stream.Collectors;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.converter.*;
import org.junit.jupiter.params.provider.CsvFileSource; import org.junit.jupiter.params.provider.CsvFileSource;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
@ -10,6 +12,15 @@ import static org.junit.jupiter.api.Assertions.*;
class SolutionTest { class SolutionTest {
static class SudokuConverter extends SimpleArgumentConverter {
@Override
protected Object convert( Object source, Class<?> targetType ) throws ArgumentConversionException {
assertEquals(String.class, targetType, "Can only convert to String");
BufferedReader in = new BufferedReader( new InputStreamReader( SolutionTest.class.getResourceAsStream( String.valueOf( source ))));
return in.lines().collect( Collectors.joining( System.lineSeparator() ));
}
}
static long totalDuration; static long totalDuration;
@BeforeAll @BeforeAll
@ -24,7 +35,7 @@ class SolutionTest {
@ParameterizedTest @ParameterizedTest
@CsvFileSource( resources = "testdata.csv" ) @CsvFileSource( resources = "testdata.csv" )
void main( final String input, final String expected ) throws IOException { void main( @ConvertWith( SudokuConverter.class ) final String input, final String expected ) throws IOException {
// keep original streams // keep original streams
InputStream oldIn = System.in; InputStream oldIn = System.in;
PrintStream oldOut = System.out; PrintStream oldOut = System.out;

@ -0,0 +1,9 @@
9 8 1 5 2 3 6 4 7
6 3 4 8 7 9 2 5 1
2 7 5 1 4 6 9 8 3
1 9 6 4 8 7 5 3 2
5 4 8 3 1 2 7 6 9
7 2 3 6 9 5 4 1 8
3 1 2 7 5 4 8 9 6
4 6 9 2 3 8 1 7 5
8 5 7 9 6 1 3 2 4

@ -0,0 +1,9 @@
2 4 6 8 9 3 7 5 1
5 9 8 6 7 1 4 2 3
1 7 3 5 2 4 6 9 8
7 5 1 2 3 6 8 4 9
9 8 2 1 4 5 3 7 6
6 3 4 7 8 9 5 1 2
4 1 9 3 5 8 2 6 7
8 2 5 9 6 7 1 3 4
3 6 7 4 1 2 9 8 5

@ -0,0 +1,9 @@
8 4 2 9 6 7 5 3 1
7 9 3 5 1 8 4 6 2
6 1 5 2 4 3 8 7 9
2 7 9 8 7 6 3 1 4
1 6 7 4 3 2 9 5 8
4 3 8 1 9 5 6 2 7
9 2 6 3 8 1 7 4 5
3 8 1 7 5 4 2 9 6
5 7 4 6 2 9 1 8 3

@ -0,0 +1,9 @@
8 9 4 7 1 3 5 2 6
7 1 2 6 5 8 4 3 9
6 3 5 4 9 2 1 8 7
2 4 1 5 3 6 7 9 8
9 8 6 1 2 7 3 5 4
3 5 7 9 8 4 2 6 1
1 6 9 3 4 5 8 7 2
4 2 3 8 7 9 6 1 5
5 7 8 2 6 1 9 4 3

@ -0,0 +1,9 @@
3 2 7 6 9 5 1 8 4
8 1 5 4 7 3 6 2 9
9 4 6 1 2 8 7 5 3
6 5 1 8 4 2 3 9 7
4 3 9 7 5 6 8 1 2
2 7 8 3 1 9 5 6 4
5 6 3 2 8 4 9 7 1
7 8 4 9 3 1 2 6 5
1 9 2 5 6 7 4 3 8

@ -0,0 +1,5 @@
sudoku1.txt, true
sudoku2.txt, true
sudoku3.txt, false
sudoku4.txt, true
sudoku5.txt, false
1 sudoku1.txt true
1 sudoku1.txt true
2 sudoku2.txt true
3 sudoku3.txt false
4 sudoku4.txt true
5 sudoku5.txt false

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Loading…
Cancel
Save