Solve puzzle with code

Here is a quite common programming interview puzzle – You have two buckets. A 3 gallon bucket and a 5 gallon bucket. Buckets are not marked or graduated. You are to fetch 4 gallon of water in a single trip to the river. How will you do it?
Obviousy, this question is asked by changing the capacities of the two buckets and the amount of water to be fetched.
Your mission, should you choose to accept it, is to write a program to solve this puzzle in a generic way. Your program should take 3 inputs – capacities of the two buckets and the amount of water to be fetched. After solving the puzzle, your program must write out the solution step-by-step. You can use any programming language, any platform, any programming technique to write the program. Use whatever you like – C#, VB, C, C++, Java, Ruby, Python, F#, Boo, Javascript or any other language of your choice. Use design patterns, TDD, BDD, MVVM, Linq or whatever. Make it blazing fast, exploit multiple cores, use artificial intelligence. Go nuts! If you want to write the whole program in T-sql, powershell script or a batch script, go ahead. Heck ! you can even do it with regular expressions, if that your thing!
Your mission, should you choose to accept it, is to write a program to solve this common programming interview puzzle in a generic way.
The puzzle goes like this – You have two buckets. A 3 gallon bucket and a 5 gallon bucket. Buckets are not marked or graduated. You are to fetch 4 gallons of water in a single trip to the river. How will you do it?
tommylees-flickr-buckets
Photo Credit : http://www.flickr.com/photos/71256895@N00/3688461774/sizes/l/in/photostream/

Your program should take 3 inputs – capacities of the two buckets and the amount of water to be fetched. After solving the puzzle, your program must write out the solution step-by-step. You can use any language, any platform and any technique to write the program. Use whatever you like – C#, VB, C, C++, Java, Ruby, Python, F#, Boo, Javascript, powershell script or any other language of your choice. Use design patterns, TDD, BDD, MVVM, Linq or whatever. Make it blazing fast, exploit multiple cores, use artificial intelligence. Go nuts! If you want to write the whole program in T-sql, go ahead. Heck ! you can even do it with regular expressions, if that your thing!

ChildWindow Positioning

Silverlight ChildWindow is centered over the parent control. I have extended ChildWindow to open it at a specific position.

Positioned ChildWindow
Positioned ChildWindow

Here is a live demo : childwindow-positioning-demo

And here is the code :

public partial class ChildWindowEx : ChildWindow
{

    Point _pt = new Point(0,0);

    public ChildWindowEx()
    {
        InitializeComponent();
    }

    public ChildWindowEx(Point pt) : this()
    {
        _pt = pt;            
    }

    protected override Size ArrangeOverride(Size finalSize)
    {            
        Grid grid = Utils.FindChild(this, "ContentRoot");

        if (grid != null)
        {               
            grid.Arrange(new Rect(_pt.X, _pt.Y, grid.DesiredSize.Width, grid.DesiredSize.Height));
            return finalSize;
        }

        return base.ArrangeOverride(finalSize);
    }
}

Visual Studio 2010 Solution : download

Using Model-View-ViewModel in WPF Applications

I am presenting at SoCalCodeCamp this weekend (January 30th, 2010) – Advanced WPF – Using Model-View-ViewModel in WPF Applications.
tasklist-mockup
tasklist-mockup

Agenda :

1. Roles of the M-V-VM triad
2. Why use M-V-VM ?
3. Characteristics of the ViewModel
4. Build a Sample WPF Application using M-V-VM



Resources :

  1. MSDN Magazine Article and Sample Application Download
  2. Martin Fowler’s description of the ‘Presentation Model ‘
  3. Jeremy Miller’s ‘Build Your Own CAB’ series

ReSharper vs CodeRush + Refactor Pro

I have been playing with JetBrains ReSharper (version 4.1) and DevExpress CodeRush + Refactor! Pro (version 3.2.2) lately. I wanted to decide on which one to go with. Here are some notes –

R# = ReSharper
CodeRush = CodeRush + Refactor! Pro

  • R# seems to be better for refactoring, because it does not assume that I follow a rigid work flow when writing code. For example, I don’t always decide on the class hierarchies upfront. I do discover them as I write code. So, when I discover that I need to extract a base class from the class I am working on, I can do so with R#. I couldn’t find a way to do extract a base/super class with CodeRush.
  • In R#, I can create an overloaded constructor using “Change Signature” refactoring (Ctrl+R, S). I couldn’t find a way to do this in CodeRush. CodeRush does allow me to create overloaded methods.

However, comparisons that only focus on how many refactorings each tool can do are not very important to me. After all, it should be easy for the individual missing refactorings to be implemented in the subsequent releases of these tools. I want to know which one is a better investment in the long run. The investment is not just a couple of hundred dollars, it is going to be an investment of time too.

  • R# “finds” Types as you try to use them and adds references and “using” directives from other projects in your solution. You will not believe how useful this is! You don’t have to interrupt your train of thought to go add references and “using” directives. CodeRush doesn’t do that.
  • I found R# “Create From Usage” feature incredibly useful with TDD. This feature will be available in Visual Studio 2010. They are calling it “Consume-First Development”.
  • R# has keyboard shortcuts for individual refactorings. All refactorings are accessible from ALT+ENTER or ALT+R. Again, R# helps you keep focused on the code you are writing.  In CodeRush, individual refactorings don’t have keyboard shortcuts.
  • R# allows me to specify exactly what to include when doing ExtractXXX refactorings. CodeRush doesn’t.
  • R# seems to know what I want to do. For example, when I discover that an existing class would be a good starting point to write the class I am thinking about and copy-paste the class and attempt to rename it, CodeRush renames both classes. R# renames the one you intend to rename.
  • R# introduces much less friction when running unit tests. It is possible to add unit-testing add-ins to Visual Studio and access similar functionality, but the way unit-testing is integrated into R#, it is much more conducive to TDD.
  • Collapse a class region. Now try to do a refactoring on this class, like “Rename file to match type” etc. In CodeRush, you will not be able to do it because the three-dot glyph will not be displayed when the class is collapsed.
  • CodeRush keeps getting in my way by automatically writing over as I type. It expands its “smart templates” when you press space. Guess what! That is how I separate words in my sentences – by pressing space. It gets quite annoying. I wish I could turn it off.

As you have probably guessed by now, I think ReSharper is a better fit for me.

Here is a list of some other comparisons, covering aspects and features of these products I haven’t mentioned here that might be important to you. Some of these concluded in favor of CodeRush. Keep in mind that some of the following articles are a couple of years old.

Using NHibernate With SQLite in DbUpdater

DbUpdater (version 1.3 onwards) can be used with sqlite3.

Review the files included in DbUpdater-sqlite3.zip

1. DbUpdater.exe.config must be modified. Note changes to the following keys – exe-file, exe-args, dialect, driver_class, connetion_string.
2. SchemaVersion.hbm.xml must be modified. The ‘class’ attributes value for the ‘generator’ of ‘id’ must be changed to ‘identity’. No other changes are needed here.
3. \SqlScripts\schema-version-table.sql is modified. Note that the datatypes of all columns are changed to TEXT, except for SchemaChangeId, which is changed to integer primary key. integer primary key is used for auto-incremented columns in sqlite.
4. \SqlScripts\baseline.sql is modified. Note the use of current_timestamp function. The actual sql syntax in this file must conform to sqlite dialect.
5. All other .sql scripts in \SqlScripts directory are also modified to conform to sqlite dialect.

The following files are expected to be in DbUpdater.exe directory (as configured in the DbUpdater.exe.config file) –

1. sqlite3.exe : download.
2. System.Data.SQLite.dll : download.
3. createdb.bat file : This file is included. This batch script will call sqlite3.exe to execute sql queries.

Another very useful tool for working with sqlite database files is SQLite Database Browzer.

Dial Gauge is a ProgressBar

I am not just being philosophical here. In WPF, ControlTemplates allow you to transform a control’s appearance. This is not the same as applying stylesheets. This is an entirely new level of customization. What you see in the screenshot below looks like a Dial but is actually a simple ProgressBar control wearing a “Dial” template. The Dial template was created using Microsoft Expression Blend .

dial-is-a-progressbar.PNG

Include the ControlTemplate as a Window.Resource and then assign it to ProgressBar element’s Template attribute :

         

Of course that is not all. The ControlTemplate must define some named elements of FrameworkElement type. Charles Petzold has explained it in detail : MSDN Magazine Article. So, I will not repeat it here.

The sample code can be downloaded here : DialTest.zip
License : Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.