Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, December 30, 2010

2011's Six Month Book Reading Plan

Six Books for first half of New Year.

CLR via C# (Dev-Pro)
CLR via C# (Dev-Pro) is a very good resource to master the intricacies of the common language runtime (CLR) and the .NET Framework 4.0. I think it is time to master C# and .Net Framework and this is one of the most recommended books on stackoverflow.

C# in Depth, Second Edition
C# in Depth, Second Edition is by far the most recommended book for learning deeply about C#. It assumes that you are already familiar with C# and its syntax and doing that allows it to get rid of boring introductory material.

Head First Design Patterns
When people ask for design patterns book for .net, they get Head First Design Patterns. The book provides examples in java but they are relevant for .net developers as well. This book does not cover all the GoF Design Patterns but does a good job of explaining the patterns.

Professional ASP.NET Design Patterns
Professional ASP.NET Design Patterns is more about layered architecture than design patterns. This book covers a lot of ground and is more suited for beginners to mid-level developers. David Hayden has a good review of the book here.

Refactoring: Improving the Design of Existing Code
Refactoring: Improving the Design of Existing Code is kind of natural progression from design patterns. Every application I have worked so far could have used a bit of refactoring and I intend to learn more about it from this book.

The Art of Unit Testing: With Examples in .Net
The Art of Unit Testing: With Examples in .Net The only way I can make changes, refactor and still have confidence in my code is if I have a way of knowing that I haven't broken anything and Unit Tests are a way of doing just that - giving you instant feedback that all is well or NOT!

Wednesday, November 24, 2010

Sudoku Validation

In one of the interviews I gave recently, there was a question about sudoku validation (9x9). The logic to check itself is quiet simple -
  1. Check for existing value in row
  2. Check for existing value in column
  3. Check for existing value in block

Solution
Two dimensional array is a good data structure to use for it. My first solution involved using three loops but on refinement I was able to use only one loop for the validation. I find the start of block location first. In the loop checking row and column is quite straight forward. It is checking for value in a block that is a bit tricky and I had to play with LinqPad and Excel before I came up with correct statements.

public static bool Validate(int number, int row, int col)
{
    Console.WriteLine("Writing at pos {0},{1} - value {2}", row - 1, col - 1, number);

    var startBlockX = ((row - 1) / 3) * 3;
    var startBlockY = ((col - 1) / 3) * 3;

    for (int i = 0; i < 9; i++)
    {
        if (arr[i, col - 1] == number)
        {
            Console.WriteLine("found in col value {0}", arr[i, col - 1]);
            return false;
        }

        if (arr[row - 1, i] == number)
        {
            Console.WriteLine("found in row value {0}", arr[row - 1, i]);
            return false;
        }

        var mx = (i / 3) + startBlockX;
        var my = startBlockY + (i % 3);
        Console.WriteLine("block pos {0},{1}", mx, my);

        if (arr[mx, my] == number)
        {
            Console.WriteLine("found in block");
            return false;
        }
    }

    return true;
}

Checking
Using the following array and checking for highlighted spots in figure above for value of 9.

var arr = new int[9, 9];

arr[3, 6] = 9;
arr[3, 8] = 7;
arr[0, 8] = 9;
arr[1, 2] = 2;
arr[7, 7] = 9;
Validate(9, 3, 3);
Checking for pos 2,2 - value 9
found in block

Validate(9, 1, 5);
Checking for pos 0,4 - value 9
found in row

Validate(9, 3, 9);
Checking for pos 2,8 - value 9
found in col