Monday, February 23, 2015

The Singleton Pattern

I'm not going to explain Singleton pattern. It's already nicely done in this article.
Once you have gone through that article. Feel free to experiment with code below.

using System;
namespace SingletonTest
{
    public class SingletonAlmostLazy
    {
        private static readonly SingletonAlmostLazy sObj = new SingletonAlmostLazy();

        // this ensures that type initialization will be done only when this class is referenced by 'SingletonAlmostLazy.XXX'
        // v4.0 onwards type initilization changed a bit (Don't exactly know what that is :) )
        // In < v4.0, type initialization may happen before as well - i.e, before the actual reference
        static SingletonAlmostLazy() { }
        private SingletonAlmostLazy()
        {
            Console.WriteLine("This is singleton constructor");
        }
        public static SingletonAlmostLazy Instance { get { return sObj; } }

        public static void DoSomething()
        {
            Console.WriteLine("Doing something in SingletonAlmostLazy");
        }
    }

    class SingletonFullLazy
    {
        private class SingletonHolder
        {
            internal static readonly SingletonFullLazy sObj = new SingletonFullLazy();

            // Lazy
            static SingletonHolder() { }
        }

        private SingletonFullLazy()
        {
            Console.WriteLine("This is singleton constructor");
        }

        public static SingletonFullLazy Instance { get { return SingletonHolder.sObj; } }

        public static void DoSomething()
        {
            Console.WriteLine("Doing something in SingletonFullLazy");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            SingletonAlmostLazy.DoSomething();
            Console.WriteLine("After SingletonAlmostLazy.DoSomething()");
            SingletonAlmostLazy obj = SingletonAlmostLazy.Instance;
            SingletonAlmostLazy obj2 = SingletonAlmostLazy.Instance;

            Console.WriteLine("Done with AlmostLazy...");
            Console.WriteLine("\n\n\n");

            //---------------

            SingletonFullLazy.DoSomething();

            Console.WriteLine("After SingletonFullLazy.DoSomething()");

            SingletonFullLazy o = SingletonFullLazy.Instance;
            SingletonFullLazy o2 = SingletonFullLazy.Instance;

            Console.WriteLine("Done with FullLazy...");
            Console.ReadLine();
        }
    }
}
Output:
This is singleton constructor
Doing something in SingletonAlmostLazy
After SingletonAlmostLazy.DoSomething()
Done with AlmostLazy...




Doing something in SingletonFullLazy
After SingletonFullLazy.DoSomething()
This is singleton constructor
Done with FullLazy...

Wednesday, February 18, 2015

Find all possible ways to sum numbers such that it sumes to N (order matters)

You are given a number N and a set of numbers. Find all possible ways to sum those numbers in the set such that total comes to N and order matters.
using System;
using System.Collections.Generic;

namespace Solutions
{
    public class NumbersSumToValueQuora
    {
        List<List<int>> findSetOfNumbers(List<int> numberList, int value)
        {
            return myHelper(numberList, value, new LinkedList<int>(), new List<List<int>>());
        }

        List<List<int>> myHelper(List<int> numberList, int remaining, LinkedList<int> currSet, List<List<int>> result)
        {
            if (remaining == 0)
            {
                result.Add(new List<int>(currSet));
                return result;
            }
            for (int i = 0; i < numberList.Count; i++)
            {
                int temp = numberList[i];
                int nVal = remaining - temp;
                if (nVal >= 0)
                {
                    numberList.RemoveAt(i);
                    currSet.AddLast(temp);

                    myHelper(numberList, remaining - temp, currSet, result);

                    currSet.RemoveLast();
                    numberList.Insert(i, temp);
                }
            }
            return result;
        }
        public static void HelperNumbersSumToValueQuora()
        {
            NumbersSumToValueQuora obj = new NumbersSumToValueQuora();

            List<List<int>> ans = obj.findSetOfNumbers(new List<int> { 1, 2, 3, 4, 5 }, 4);
            foreach (var slist in ans)
            {
                Console.WriteLine("{" + String.Join(", ", slist.ToArray()) + "}");
            }
        }
    }
}