Skip to content

Commit 05144d4

Browse files
committed
sudoku fist pass, fixing up tests
1 parent 5dcb953 commit 05144d4

File tree

6 files changed

+214
-2
lines changed

6 files changed

+214
-2
lines changed

.vscode/launch.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
// Use IntelliSense to find out which attributes exist for C# debugging
6+
// Use hover for the description of the existing attributes
7+
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
8+
"name": ".NET Core Launch (console)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build",
12+
// If you have changed target frameworks, make sure to update the program path.
13+
"program": "${workspaceFolder}/UnitTest/bin/Debug/netcoreapp2.0/UnitTest.dll",
14+
"args": [],
15+
"cwd": "${workspaceFolder}/UnitTest",
16+
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
17+
"console": "internalConsole",
18+
"stopAtEntry": false
19+
},
20+
{
21+
"name": ".NET Core Attach",
22+
"type": "coreclr",
23+
"request": "attach"
24+
}
25+
]
26+
}

.vscode/tasks.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/UnitTest/UnitTest.csproj",
11+
"/property:GenerateFullPaths=true",
12+
"/consoleloggerparameters:NoSummary"
13+
],
14+
"problemMatcher": "$msCompile"
15+
},
16+
{
17+
"label": "publish",
18+
"command": "dotnet",
19+
"type": "process",
20+
"args": [
21+
"publish",
22+
"${workspaceFolder}/UnitTest/UnitTest.csproj",
23+
"/property:GenerateFullPaths=true",
24+
"/consoleloggerparameters:NoSummary"
25+
],
26+
"problemMatcher": "$msCompile"
27+
},
28+
{
29+
"label": "watch",
30+
"command": "dotnet",
31+
"type": "process",
32+
"args": [
33+
"watch",
34+
"run",
35+
"--project",
36+
"${workspaceFolder}/UnitTest/UnitTest.csproj"
37+
],
38+
"problemMatcher": "$msCompile"
39+
}
40+
]
41+
}

Algorithms/LeetCode/ArrayIntersection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using System.Collections.Generic;
22
using System.Linq;
33

4-
namespace Algorithms.Numeric
4+
namespace Algorithms.LeetCode
55
{
6-
public static class Solution
6+
public static class ArrayIntersection
77
{
88
public static int[] Intersection(int[] nums1, int[] nums2)
99
{

Algorithms/LeetCode/Sudoku.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace Algorithms.LeetCode
5+
{
6+
public static class Sudoku
7+
{
8+
public static bool IsValidSudoku(char[][] board) {
9+
var squareVals = new HashSet<char>[3,3];
10+
11+
//scan rows & columns
12+
for (int i = 0; i < board.Length; i++)
13+
{
14+
var columnVals = new HashSet<char>();
15+
var rowVals = new HashSet<char>();
16+
17+
for (int j = 0; j < board[i].Length; j++)
18+
{
19+
//row check
20+
if (board[i][j] != '.') {
21+
if (!rowVals.Add(board[i][j])) {
22+
return false;
23+
}
24+
25+
//square check
26+
if (squareVals[i/3, j/3] == null) {
27+
squareVals[i/3, j/3] = new HashSet<char>();
28+
}
29+
30+
if (!squareVals[i/3, j/3].Add(board[i][j])) {
31+
return false;
32+
}
33+
}
34+
35+
//column check
36+
if (board[j][i] != '.') {
37+
if (!columnVals.Add(board[j][i])) {
38+
return false;
39+
}
40+
}
41+
}
42+
}
43+
44+
return true;
45+
}
46+
}
47+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Algorithms.LeetCode;
2+
using System.Linq;
3+
using Xunit;
4+
5+
namespace UnitTest.LeetCode
6+
{
7+
public class ArrayIntersectionTest
8+
{
9+
[Fact]
10+
public void ArrayIntersection_DoTest()
11+
{
12+
ArrayIntersection.Intersection(new int[] {1, 2, 3}, new int[] {2,3,4});
13+
14+
15+
Assert.Equal(0, 0);
16+
}
17+
}
18+
}

UnitTest/LeetCode/SudokuTest.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using Algorithms.LeetCode;
2+
using Xunit;
3+
4+
namespace UnitTest.LeetCode
5+
{
6+
public static class SudokuTest
7+
{
8+
[Fact]
9+
public static void ValidBoard_ReturnsTrue()
10+
{
11+
char[][] board = new char[][] {
12+
new char[] {'5','3','.','.','7','.','.','.','.'},
13+
new char[] {'6','.','.','1','9','5','.','.','.'},
14+
new char[] {'.','9','8','.','.','.','.','6','.'},
15+
new char[] {'8','.','.','.','6','.','.','.','3'},
16+
new char[] {'4','.','.','8','.','3','.','.','1'},
17+
new char[] {'7','.','.','.','2','.','.','.','6'},
18+
new char[] {'.','6','.','.','.','.','2','8','.'},
19+
new char[] {'.','.','.','4','1','9','.','.','5'},
20+
new char[] {'.','.','.','.','8','.','.','7','9'}};
21+
var result = Sudoku.IsValidSudoku(board);
22+
23+
Assert.True(result);
24+
}
25+
26+
[Fact]
27+
public static void DuplicateSquareBoard_ReturnsFalse()
28+
{
29+
char[][] board = new char[][] {
30+
new char[] {'8','3','.','.','7','.','.','.','.'},
31+
new char[] {'6','.','.','1','9','5','.','.','.'},
32+
new char[] {'.','9','8','.','.','.','.','6','.'},
33+
new char[] {'.','.','.','.','6','.','.','.','3'},
34+
new char[] {'4','.','.','8','.','3','.','.','1'},
35+
new char[] {'7','.','.','.','2','.','.','.','6'},
36+
new char[] {'.','6','.','.','.','.','2','8','.'},
37+
new char[] {'.','.','.','4','1','9','.','.','5'},
38+
new char[] {'.','.','.','.','8','.','.','7','9'}};
39+
var result = Sudoku.IsValidSudoku(board);
40+
41+
Assert.False(result);
42+
}
43+
44+
[Fact]
45+
public static void DuplicateRowBoard_ReturnsFalse()
46+
{
47+
char[][] board = new char[][] {
48+
new char[] {'8','3','.','.','7','.','3','.','.'},
49+
new char[] {'6','.','.','1','9','5','.','.','.'},
50+
new char[] {'.','9','8','.','.','.','.','6','.'},
51+
new char[] {'8','.','.','.','6','.','.','.','3'},
52+
new char[] {'4','.','.','8','.','3','.','.','1'},
53+
new char[] {'7','.','.','.','2','.','.','.','6'},
54+
new char[] {'.','6','.','.','.','.','2','8','.'},
55+
new char[] {'.','.','.','4','1','9','.','.','5'},
56+
new char[] {'.','.','.','.','8','.','.','7','9'}};
57+
var result = Sudoku.IsValidSudoku(board);
58+
59+
Assert.False(result);
60+
}
61+
62+
[Fact]
63+
public static void DuplicateColumnBoard_ReturnsFalse()
64+
{
65+
char[][] board = new char[][] {
66+
new char[] {'8','3','.','.','7','.','3','.','.'},
67+
new char[] {'6','.','.','1','9','5','.','.','.'},
68+
new char[] {'.','9','8','.','.','.','.','6','.'},
69+
new char[] {'8','.','.','.','6','.','.','.','3'},
70+
new char[] {'4','.','.','8','.','3','.','.','1'},
71+
new char[] {'7','.','.','.','2','.','.','.','6'},
72+
new char[] {'.','6','.','.','.','.','2','8','.'},
73+
new char[] {'.','.','.','4','1','9','.','.','5'},
74+
new char[] {'.','.','.','.','8','.','.','7','9'}};
75+
var result = Sudoku.IsValidSudoku(board);
76+
77+
Assert.False(result);
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)