Wednesday 30 November 2016

Print Staircase

Sample input 6
Sample Output 
     #
    ##
   ###
  ####
 #####
######
Solution-

int n = Convert.ToInt32(Console.ReadLine());
            for(int i = 1; i <= n; i++)
            {
                for(int j = 1; j <= n; j++)
                {
                    if (j <= n - i)
                    {
                        Console.Write(" ");
                    }
                    else
                    {
                        Console.Write("#");
                    }
                }
                Console.WriteLine();
            }

Saturday 26 November 2016

Thursday 24 November 2016

Reverse a String word by word

Given a string, reverse it word by word.

input : I like to play with c-sharp.
expected output : c-sharp with play to like I.

  static void Main(string[] args)
        {
            string s = Console.ReadLine();
            string[] arr = s.Split(' ');
            int length = arr.Length;

            // it will hold reverse array
            string[] revArr = new string[length];

            // this index holds index value for new reversed string.
            int index = length - 1;
            for (int j = length - 1; j >= 0; j--)
            {
                revArr[index-j] = arr[j];
            }

            string revString = String.Join(" ", revArr);
            Console.WriteLine(revString);

            Console.ReadLine();

        }



Count the length of last word in a String

Suppose you have a string "I love Programming"
Output - should be  11.
if there is no word then Output: 0

static void Main1(string[] args)
        {
            string s = Console.ReadLine();
            string[] arr = s.Split(' ');
            int length = arr.Length;
            Console.WriteLine(arr[length - 1].Length);
            Console.ReadLine();

        }

Wednesday 23 November 2016

Practical Difference between Static class and Singleton Pattern

Static Class - 

1. static class can not implement interfaces or can not drive from class , it can drive from Object class only.
2. static class can not passed as a method parameter
3. static class is a sealed class

As is the case with all class types, the type information for a static class is loaded by the .NET Framework common language runtime (CLR) when the program that references the class is loaded. The program cannot specify exactly when the class is loaded. However, it is guaranteed to be loaded and to have its fields initialized and its static constructor called before the class is referenced for the first time in your program. A static constructor is only called one time, and a static class remains in memory for the lifetime of the application domain in which your program resides.

Singleton Pattern-

1.  Singleton  can  implement interfaces or can drive from class.
2.  Singleton  can  passed as a method parameter
3.. its a abstract class because of private constructor

Because the Singleton instance is referenced by a private static member variable, the instantiation does not occur until the class is first referenced by a call to the Instance property. This solution therefore implements a form of the lazy instantiation property, as in the Design Patterns form of Singleton.


using System;

public class Singleton
{
   private static Singleton instance;

   private Singleton() {}

   public static Singleton Instance
   {
      get 
      {
         if (instance == null)
         {
            instance = new Singleton();
         }
         return instance;
      }
   }
}

Static Initialization 

public sealed class Singleton
{
   private static readonly Singleton instance = new Singleton();
   
   private Singleton(){}

   public static Singleton Instance
   {
      get 
      {
         return instance; 
      }
   }
}

This implementation has two main advantages:
  • Because the instance is created inside the Instance property method, the class can exercise additional functionality (for example, instantiating a subclass), even though it may introduce unwelcome dependencies.

  • The instantiation is not performed until an object asks for an instance; this approach is referred to as lazy instantiation. Lazy instantiation avoids instantiating unnecessary singletons when the application starts.
  • For More - Singleton Pattern
  • Wednesday 16 November 2016

    Addition of 2 numbers of more than 30 length or more

    For example you have to numbers
    num1 = 24454543532465465434345465465656546454
    num2 = 97867543532465465434345465465656546454

    you can not store these in int or long because of range, so how will you do.

    Solution-
    1. just store both numbers in Array
    2. start sum of elements of array from last
    3. if any 'carry' then forward it to previous.

    Text File words count

    Suppose you have a text file, I want total number of words and which word how many times came. How will you approach it?

    Solution-
    1. First I will read file as string.
    2. I will split the string based on space and store in Array.
    3. Take A dictionary and key will be word and value will be count for that word.
    4. If same word or key repeats then increment the count by one.
    5. Enjoy...