diff options
| author | mo <mokha@cisco.com> | 2017-06-17 10:06:45 -0600 |
|---|---|---|
| committer | mo <mokha@cisco.com> | 2017-06-17 10:06:45 -0600 |
| commit | fcabb6809c86dc0dbbf083b805b09bebbab440d6 (patch) | |
| tree | 38710c338ccca412879b3422b84ed492e79f3019 /spec | |
| parent | 9a03cbffe387a09385083ecc92830ef521483840 (diff) | |
add sudoku problem.
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/sudoku2_spec.rb | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/spec/sudoku2_spec.rb b/spec/sudoku2_spec.rb new file mode 100644 index 0000000..976d40a --- /dev/null +++ b/spec/sudoku2_spec.rb @@ -0,0 +1,60 @@ +<<-DOC +Sudoku is a number-placement puzzle. The objective is to fill a 9 × 9 grid with numbers in such a way that each column, each row, and each of the nine 3 × 3 sub-grids that compose the grid all contain all of the numbers from 1 to 9 one time. + +Implement an algorithm that will check whether the given grid of numbers represents a valid Sudoku puzzle according to the layout rules described above. Note that the puzzle represented by grid does not have to be solvable. + +Example + +For + +grid = [['.', '.', '.', '1', '4', '.', '.', '2', '.'], + ['.', '.', '6', '.', '.', '.', '.', '.', '.'], + ['.', '.', '.', '.', '.', '.', '.', '.', '.'], + ['.', '.', '1', '.', '.', '.', '.', '.', '.'], + ['.', '6', '7', '.', '.', '.', '.', '.', '9'], + ['.', '.', '.', '.', '.', '.', '8', '1', '.'], + ['.', '3', '.', '.', '.', '.', '.', '.', '6'], + ['.', '.', '.', '.', '.', '7', '.', '.', '.'], + ['.', '.', '.', '5', '.', '.', '.', '7', '.']] +the output should be +sudoku2(grid) = true; + +For + +grid = [['.', '.', '.', '.', '2', '.', '.', '9', '.'], + ['.', '.', '.', '.', '6', '.', '.', '.', '.'], + ['7', '1', '.', '.', '7', '5', '.', '.', '.'], + ['.', '7', '.', '.', '.', '.', '.', '.', '.'], + ['.', '.', '.', '.', '8', '3', '.', '.', '.'], + ['.', '.', '8', '.', '.', '7', '.', '6', '.'], + ['.', '.', '.', '.', '.', '2', '.', '.', '.'], + ['.', '1', '.', '2', '.', '.', '.', '.', '.'], + ['.', '2', '.', '.', '3', '.', '.', '.', '.']] +the output should be +sudoku2(grid) = false. + +The given grid is not correct because there are two 1s in the second column. Each column, each row, and each 3 × 3 subgrid can only contain the numbers 1 through 9 one time. + +Input/Output + +[time limit] 4000ms (rb) +[input] array.array.char grid + +A 9 × 9 array of characters, in which each character is either a digit from '1' to '9' or a period '.'. + +[output] boolean + +Return true if grid represents a valid Sudoku puzzle, otherwise return false. +DOC +describe "sudoku2" do + def sudoku2(grid) + end + + [ + { grid: [[".",".",".","1","4",".",".","2","."], [".",".","6",".",".",".",".",".","."], [".",".",".",".",".",".",".",".","."], [".",".","1",".",".",".",".",".","."], [".","6","7",".",".",".",".",".","9"], [".",".",".",".",".",".","8","1","."], [".","3",".",".",".",".",".",".","6"], [".",".",".",".",".","7",".",".","."], [".",".",".","5",".",".",".","7","."]], expected: true }, + ].each do |x| + it do + expect(sudoku2(x[:grid])).to eql(x[:expected]) + end + end +end |
