Skip to content

Commit

Permalink
Add Valid Sudoku
Browse files Browse the repository at this point in the history
  • Loading branch information
James Lieu committed Dec 15, 2021
1 parent 581d6fe commit 7ac229e
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 0 deletions.
27 changes: 27 additions & 0 deletions ValidSudoku/ValidSudoku.Tests/ValidSudoku.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ValidSudoku\ValidSudoku.csproj" />
</ItemGroup>

</Project>
55 changes: 55 additions & 0 deletions ValidSudoku/ValidSudoku.Tests/ValidSudokuTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Xunit;

namespace ValidSudoku.Tests
{
public class ValidSudokuTests
{
[Fact]
public void Test_When_The_Input_Is_A_Valid_Sudoku_Board_Then_Return_True()
{
// arrange
var input = new char[][]
{
new char[] {'5','3','.','.','7','.','.','.','.'},
new char[] {'6','.','.','1','9','5','.','.','.'},
new char[] {'.','9','8','.','.','.','.','6','.'},
new char[] {'8','.','.','.','6','.','.','.','3'},
new char[] {'4','.','.','8','.','3','.','.','1'},
new char[] {'7','.','.','.','2','.','.','.','6'},
new char[] {'.','6','.','.','.','.','2','8','.'},
new char[] {'.','.','.','4','1','9','.','.','5'},
new char[] {'.','.','.','.','8','.','.','7','9'}
};

// act
var actual = ValidSudoku.IsValid(input);

// assert
Assert.True(actual);
}

[Fact]
public void Test_When_The_Input_Is_An_Invalid_Sudoku_Board_Then_Return_False()
{
// arrange
var input = new char[][]
{
new char[] {'8','3','.','.','7','.','.','.','.'},
new char[] {'6','.','.','1','9','5','.','.','.'},
new char[] {'.','9','8','.','.','.','.','6','.'},
new char[] {'8','.','.','.','6','.','.','.','3'},
new char[] {'4','.','.','8','.','3','.','.','1'},
new char[] {'7','.','.','.','2','.','.','.','6'},
new char[] {'.','6','.','.','.','.','2','8','.'},
new char[] {'.','.','.','4','1','9','.','.','5'},
new char[] {'.','.','.','.','8','.','.','7','9'},
};

// act
var actual = ValidSudoku.IsValid(input);

// assert
Assert.False(actual);
}
}
}
31 changes: 31 additions & 0 deletions ValidSudoku/ValidSudoku.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31919.166
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ValidSudoku", "ValidSudoku\ValidSudoku.csproj", "{50414EC2-E8BA-464C-AD2A-7AA0B80FD740}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ValidSudoku.Tests", "ValidSudoku.Tests\ValidSudoku.Tests.csproj", "{55735F2A-492F-4D46-8768-81867047B172}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{50414EC2-E8BA-464C-AD2A-7AA0B80FD740}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{50414EC2-E8BA-464C-AD2A-7AA0B80FD740}.Debug|Any CPU.Build.0 = Debug|Any CPU
{50414EC2-E8BA-464C-AD2A-7AA0B80FD740}.Release|Any CPU.ActiveCfg = Release|Any CPU
{50414EC2-E8BA-464C-AD2A-7AA0B80FD740}.Release|Any CPU.Build.0 = Release|Any CPU
{55735F2A-492F-4D46-8768-81867047B172}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{55735F2A-492F-4D46-8768-81867047B172}.Debug|Any CPU.Build.0 = Debug|Any CPU
{55735F2A-492F-4D46-8768-81867047B172}.Release|Any CPU.ActiveCfg = Release|Any CPU
{55735F2A-492F-4D46-8768-81867047B172}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7FA59BB6-0BD0-4B38-AF2A-81F2F7BDE196}
EndGlobalSection
EndGlobal
40 changes: 40 additions & 0 deletions ValidSudoku/ValidSudoku/ValidSudoku.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace ValidSudoku
{
//Determine if a 9 x 9 Sudoku board is valid.Only the filled cells need to be validated according to the following rules:

//Each row must contain the digits 1-9 without repetition.
//Each column must contain the digits 1-9 without repetition.
//Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.
//Note:

//A Sudoku board (partially filled) could be valid but is not necessarily solvable.
//Only the filled cells need to be validated according to the mentioned rules.
public class ValidSudoku
{
public static bool IsValid(char[][] board)
{
HashSet<string> seen = new HashSet<string>();

for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
char current = board[i][j];
if (current != '.')
{
if (
!seen.Add(current + " row " + i) ||
!seen.Add(current + " column " + j) ||
!seen.Add(current + " subbox " + i/3 + "-" + j/3)
)
{
return false;
}
}

}
}
return true;
}
}
}
9 changes: 9 additions & 0 deletions ValidSudoku/ValidSudoku/ValidSudoku.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>

0 comments on commit 7ac229e

Please sign in to comment.