Sudoku validator

If you’re looking for a coding job and haven’t started practicing for interview coding challenges, I’d highly recommend checking out codefights.  It’s also a good place just to have fun and keep your mind sharp.  It really is addicting.  Until now I’ve been using a site called codechef.  Codechef is great but I like the content and organization of codefights a bit better.  I don’t think I’ll share all of my solutions for codefights on here but I’d like to share a few that I really had fun with.  Here’s my solution for a sudoku validator:

bool sudoku2(char[][] grid) 
{
    string[] subgrids = new string[9];
    for(int i = 0; i < 9; i++)
    {
        string row = "";
        string column = "";
        for(int j = 0; j < 9; j++)
        {
            //Check rows
            if(grid[i][j] != '.' && row.Contains(grid[i][j]))return false;
            row = row + grid[i][j];
            //Check columns
            if(grid[j][i] != '.' && column.Contains(grid[j][i]))return false;
            column = column + grid[j][i];
            //Check subgrids
            if(i < 3 && j < 3)
            {
                if(!String.IsNullOrEmpty(subgrids[0]))
                {
                    if(grid[i][j] != '.' && subgrids[0].Contains(grid[i][j]))return false;
                }
                subgrids[0] = subgrids[0] + grid[i][j];
            }
            else if(i < 3 && 2 < j && j < 6)
            {
                if(!String.IsNullOrEmpty(subgrids[1]))
                {
                    if(grid[i][j] != '.' && subgrids[1].Contains(grid[i][j]))return false;
                }
                subgrids[1] = subgrids[1] + grid[i][j];
            }
            else if(i < 3 && j > 5)
            {
                if(!String.IsNullOrEmpty(subgrids[2]))
                {
                    if(grid[i][j] != '.' && subgrids[2].Contains(grid[i][j]))return false;
                }
                subgrids[2] = subgrids[2] + grid[i][j];
            }
            else if(2 < i && i < 6 && j < 3)
            {
                if(!String.IsNullOrEmpty(subgrids[3]))
                {                    
                    if(grid[i][j] != '.' && subgrids[3].Contains(grid[i][j]))return false;
                }
                subgrids[3] = subgrids[3] + grid[i][j];
            }
            else if(2 < i  && i< 6 && 2 < j  && j < 3)
            {
                if(!String.IsNullOrEmpty(subgrids[4]))
                {      
                    if(grid[i][j] != '.' && subgrids[4].Contains(grid[i][j]))return false;
                }
                subgrids[4] = subgrids[4] + grid[i][j];
            }
            else if(2 < i && i < 6 && j > 5)
            {
                if(!String.IsNullOrEmpty(subgrids[5]))
                {    
                    if(grid[i][j] != '.' && subgrids[5].Contains(grid[i][j]))return false;
                }
                subgrids[5] = subgrids[5] + grid[i][j];
            }
            else if(i > 5 && j < 3)
            {
                if(!String.IsNullOrEmpty(subgrids[6]))
                {    
                    if(grid[i][j] != '.' && subgrids[6].Contains(grid[i][j]))return false;
                }
                subgrids[6] = subgrids[6] + grid[i][j];
            }
            else if(i > 5 && 2 < j  && j < 6)
            {
                if(!String.IsNullOrEmpty(subgrids[7]))
                {    
                    if(grid[i][j] != '.' && subgrids[7].Contains(grid[i][j]))return false;
                }
                subgrids[7] = subgrids[7] + grid[i][j];
            }
            else if(i > 5 && j > 5)
            {
                if(!String.IsNullOrEmpty(subgrids[8]))
                {    
                    if(grid[i][j] != '.' && subgrids[8].Contains(grid[i][j]))return false;
                }
                subgrids[8] = subgrids[8] + grid[i][j];
            }
        }
    }
    
    return true;
}

If you haven’t done this on codefights you might notice that this validator allows for blank spaces.  The blank spaces are identified with periods.

In an actual game of sudoku you probably would not want to validate a puzzle until the end but something like this might be useful as a sudoku trainer.  For example you could run this function every time a player makes a move to let them know if it is a valid move or not.  The only problem with that though is that it would allow a player to make an incorrect move early on which wouldn’t be very helpful.  If I were to design a sudoku trainer, I would just check the player’s input with the completed table.  That would also be far fewer lines of code.  But I guess the purpose of these challenges are just to see if you can solve the problem, not to actually write useful code for an application.¯\_(ツ)_/¯

Advertisements