Reverse LinkList using Recursion

Node Class : For creating LinkList

public class Node<T>
    {
        private T nodeValue;
        private Node<T> nextNode;

        public Node()
        {
            this.nodeValue = default(T);
            this.nextNode = null;
        }

        public Node(T nodeValue)
        {
            this.nodeValue = nodeValue;
            this.nextNode = null;
        }

        /// <summary>
        /// Value of node
        /// </summary>
        public T NodeValue
        {
            get { return nodeValue; }
            set { this.nodeValue = value; }
        }

        /// <summary>
        /// The node this node links to
        /// </summary>
        public Node<T> NextNode
        {
            get { return this.nextNode; }
            set { this.nextNode = value; }
        }
    }

LinkList

class LinkList
    {
        public static void Test()
        {
            // Create a link list
            Node<int> a1 = new Node<int>();
            a1.NodeValue = 10;

            Node<int> a2 = new Node<int>();
            a2.NodeValue = 11;

            Node<int> a5 = new Node<int>();
            a5.NodeValue = 12;

            Node<int> a3 = new Node<int>();
            a3.NodeValue = 13;

            Node<int> a4 = new Node<int>();
            a4.NodeValue = 14;
            a1.NextNode = a2;
            a2.NextNode = a3;
            a3.NextNode = a4;
            a4.NextNode = a5;
            a5.NextNode = null;

            //reverse the link list
            
            Console.WriteLine("----------------------------------------------------");
            Console.WriteLine("Reverse  List");           
            Reverse(ref a1);           
            PrintLinkList(a1);
            Console.WriteLine("----------------------------------------------------");

        }
        public static void Reverse(ref Node<int> head)
        {

            Node<int> first;
            Node<int> rest;
            if (head == null) return;
            first = head;
            rest = first.NextNode;
            if (rest == null) return;
            Reverse(ref rest);
            first.NextNode.NextNode = first;
            first.NextNode = null;
            head = rest;

        }
        public static void PrintLinkList(Node<int> head)
        {
            var temp = head;
            while (temp != null)
            {
                Console.Write(temp.NodeValue + "-->"
                  );
                temp = temp.NextNode;

            }
            Console.WriteLine();

        }




    }

Write a Program to Return Excel Sheet Column Header Name if Input is a Number

input

           -11
              1
            26
            52
            28
           702
           703

output

input number -11 is invalid
input number 1 , output alphabet A
input number 26 , output alphabet Z
input number 52 , output alphabet AZ
input number 28 , output alphabet AB
input number 702 , output alphabet ZZ
input number 703 , output alphabet AAA

 public static string ConvertToAlphabet(int number)
        {
            int temp = number;
            char[] alphabets = new char[] { 'A', 'B', 'C', 'D', 'E', 
                                            'F', 'G', 'H', 'I', 'J', 
                                            'K', 'L', 'M', 'N', 'O', 
                                            'P', 'Q', 'R', 'S', 'T', 
                                            'U', 'V', 'W', 'X', 'Y', 
                                            'Z' };
            string Alphabet = string.Empty;
            int Index = 0;

            if (number <= 0)
            {

                Console.WriteLine("input number {0} is invalid", temp);

                return string.Empty;
            }

            if (number < 27)
            {

                Console.WriteLine("input number {0} , output alphabet {1}"
                    , temp, alphabets[number - 1]);

                return alphabets[number - 1].ToString();
            }
            while (number > 0)
            {
                Index = number % 26;
                number = number / 26; ;

                if (Index == 0)
                {
                    Index = 26;
                    number = number - 1;
                }
                Alphabet = alphabets[Index - 1] + Alphabet;

            }

            Console.WriteLine("input number {0} , output alphabet {1}"
                , temp, Alphabet);
            return Alphabet;
        }

Find two numbers in an array whose sum =X

Solution : Here is the idea is to start from the first element of the array and keep a Hashtable to store the potential pairs. We will check  if sum – (a[i])  already exists in the Hashtable . If yes, we have a pair (a[i], value from Hashtable ) as the pair with the sum. If no, we store a[i] in the Hashtable .

Here is a little dry run of array a= {1,2,3,4,5,6,7,8} and sum = 6.

  1. 6 – 1 = 5  — found in Hashtable H ? No – store it in H.  H will contain (1)
  2. 6 –2 = 4 – found in Hashtable H ? No – store it in H.  H will contain (1, 2)
  3. 6 –3 = 4 – found in Hashtable H ? No – store it in H.  H will contain (1, 2, 3)
  4. 6 –4 = 2 – found in Hashtable H ? Yes – now we have a pair (a[i] , found in H) – (4,2)
  5. 6 –5 = 1 – found in Hashtable H ? Yes – now we have a pair (a[i] , found in H) – (5,1)
public static void TargetSum(int[] array, int target)
    {

      Dictionary<int, int> hashMap = new Dictionary<int, int>();

      int lookup = 0;

      for (int i = 0; i < array.Length; i++)
      {
        lookup = target - array[i];

        if (hashMap.ContainsKey(lookup) == true)
        {
          Console.WriteLine("Pair {0},{1}", lookup, array[i]);

        }
        else
        {
          hashMap.Add(array[i], array[i]);
        }
      }



    }

Counting Sort

Definition from Wikipedia

In computer science, counting sort is an algorithm for sorting a collection of objects according to keys that are small integers; that is, it is an integer sorting algorithm. It operates by counting the number of objects that have each distinct key value, and using arithmetic on those counts to determine the positions of each key value in the output sequence. Its running time is linear in the number of items and the maximum key value, so it is only suitable for use directly in situations where the keys are not significantly larger than the number of items. However, it is often used as a subroutine in another sorting algorithm, radix sort, that can handle larger keys more efficiently.

Here is the C# implementation of Counting Sort

 class Program
  {
    static void Main(string[] args)
    {
      //create an input array with maximum value of an element is 1024
      int[] input = new int[10000];
      Random random = new Random();
      for (int i = 0; i < 10000; i++)
      {
        input[i] = random.Next(0, 1024);

      }
      Sorting.CountingSort(input);
    }

   
  }

 public class Sorting
  {
    public static void CountingSort(int[] arrayA)
    {
      Console.WriteLine("Original Array");
      for (int i = 0; i < arrayA.Length; i++)
      {

        Console.Write(arrayA[i] + " , ");
      }
      Console.WriteLine("");
      int k = 1024;
      int[] arrayB = new int[arrayA.Length];
      int[] arrayC = new int[k];
      for (int i = 0; i < arrayC.Length; i++)
      {

        arrayC[i] = 0;
      }
      for (int j = 0; j < arrayA.Length; j++)
      {
        arrayC[arrayA[j]] = arrayC[arrayA[j]] + 1;
      }

      //Place the number of elements less than each value at i into array C.    
      for (int i = 1; i < k; i++)
        arrayC[i] = arrayC[i] + arrayC[i - 1];

      //Place each element of arrayA into its correct sorted position in the
      //output array B.
      for (int j = arrayA.Length - 1; j >= 0; j--)
      {
        arrayB[arrayC[arrayA[j]] - 1] = arrayA[j];
        arrayC[arrayA[j]] = arrayC[arrayA[j]] - 1;
      }

      //Overwrite the original arrayA with the output arrayB.
      Console.WriteLine("Sorted Array");
      for (int i = 0; i < arrayA.Length; i++)
      {
        arrayA[i] = arrayB[i];
        Console.Write(arrayA[i] + " , ");
      }

      Console.WriteLine("");

    }
  }
 
in this program I am showing output on console, it can be easily saved on file also.

Find the sum of all the multiples of 3 or 5 below or equal to 1000

To find the sum of all the multiples of 3 or 5 below or equal to 1000, first we have to find the sum of all the numbers which are divisible by 3, than sum of numbers which are divisible by 5. Once we are done with this we have to subtract the sum of numbers which are divisible by 3 and 5.

There are two approaches to solve this problem.

  1. The first approach is to write a loop and find the numbers which are divisible by 3,5 and add them, after this subtract the numbers which are divisible by 3 and 5.
  2. The other approach is to find the count of numbers which are divisible by 3,5 and 15. After this apply the arithmetic progression’s sum formula to find the sum.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
  class Program
  {
    static void Main(string[] args)
    {
      int sum = 0;
      int range = 1000;
      int Number1 = 3; int Number2 = 5;
      for (int i = 0, j = 0; i <= range; i += Number1, j += Number2)
      {
        sum += i;
        if (j < range && j % Number2 == 0)
        {
          sum += j;
        }
        if (i <= range && i % Number2 * Number1 == 0)
        {
          sum -= i;
        }

      }
      Console.WriteLine("\n");
      Console.WriteLine("Sum of all the multiples of 3 or 5 below or equal to 1000" +
      "using Loop "  + sum);

      int Total_Number1 = range / Number1;
      int Total_Number2 = range / Number2;
      int Total_Number1AndNumber2 = range / (Number1 * Number2);

      int sumTotal_Number1 = Total_Number1 * (Number1 + Total_Number1 * Number1) / 2;
      int sumTotal_Number2 = Total_Number2 * (Number2 + Total_Number2 * Number2) / 2;
      int sumTotal_Number1andNumber2 = Total_Number1AndNumber2 * (Number2 * Number1
          + Total_Number1AndNumber2 * Number2 * Number1) / 2;
      int total = sumTotal_Number1 + sumTotal_Number2 - sumTotal_Number1andNumber2;
      Console.WriteLine("Sum of all the multiples of 3 or 5 below or equal to 1000 " +
                      "using arithmetic progression’s sum formula  " + total);

      Console.ReadLine();
    }
  }
}

Please post your comments if you find a better way to find the sum of numbers which are divisible by 3 or 5 and less than 1000.

Disable Internet Explorer Friendly Error Messages

Sometimes Internet Explorer display friendly error messages like “Internet explorer cannot display this webpage”. Via looking into this error message we do not get, any idea related to error message and most people think, it’s an internet connection problem.

In order to view the actual error message, we need to disable “Internet Explorer’s friendly error message” feature. In order to disable this feature go to Tools menu, click Internet Options, click the resulting dialog’s Advanced tab, and clear the “Show friendly HTTP error messages” checkbox.

Extend Visual Studio Team Foundation Server 2010 Using Java

Microsoft released a SDK for visual studio team foundation server 2010, for java developers. This SDK allows java developers to write code for VSTFS like .NET developers can write. This SDK allow java developers to write software components that can be integrated with VSTFS 2010.

 

The Team Foundation Server SDK for Java includes documentation, samples and redistributable components to help you develop software products that integrate with Microsoft Visual Studio Team Foundation Server 2010. By downloading the SDK from the link below you agree to the Microsoft Visual Studio Team Foundation Server 2010 Software Development Kit for Java license terms.

The SDK contains the following components:

  • Redistributable library (jar file) containing the TFS API’s
  • Redistributable native code libraries required by the SDK for Java
  • API Documentation in Javadoc format
  • Check-in policy code sample
  • Custom work item control code sample
  • Console application code sample
  • Code snippets

Download Microsoft Visual Studio Team Foundation Server 2010 Software Development Kit for Java

What is .NET Framework

Microsoft .NET Framework is an integral part of Windows operating system and it is used for developing applications that runs on family of Windows operating system. The .NET Framework has two major components called Common Language Runtime and .NET Base Class Library. .NET Framework support an array of language, VB.C#, F#, C++ and J# are supported by Visual Studio’s default installation.

Features of .NET Framework

Interoperability : Sometimes application developed in .NET has to interact with applications developed outside .NET. Access to COM components is provided in the System.Runtime.InteropServices and System.EnterpriseServices namespaces of the framework; access to other functionality is provided using the P/Invoke feature.

Common Runtime Engine : The Common Language Runtime (CLR) is the execution engine of the .NET Framework. Its CLR responsibility to locate, load and manage .NET types on your behalf. The CLR also takes care of a number of low-level details such as memory management, application hosting, handling threads, and performing various security checks.

Common Type System  : The CTS specification fully describes all possible data types and programming constructs supported by the runtime, specifies how these entities can interact with each other, and details how they are represented in the .NET metadata format. Common Type System makes .NET Framework language independent means, class libraries developed in one language can be used applications that are developed in some other language.

Base Class Library : The Base Class Library (BCL), part of the Framework Class Library (FCL), is a library of functionality available to all languages using the .NET Framework. The BCL provides classes which encapsulate a number of common functions, including file reading and writing, graphic rendering, database interaction, XML document manipulation and so on.

Simplified Deployment Model : .NET Framework provides a simplified deployment. Applications developed using .NET Framework are not required to register them as binary unit into system registry. Further more .NET Framework allows multiple versions of the same *.dll to exist in harmony on a single machine.

Visual Studio LightSwitch: An Integrated Development Environment for Managers

Microsoft is working on product called LightSwitch and release a beta version of Visual Studio LightSwitch on August 23, 2010. Visual Studio LightSwitch will come with pre-configured templates, pre-written code and other reusable components. Visual Studio LightSwitch also allows users to write custom code in Visual Basic .NET or C#. Applications developed using LightSwitch can be deployed on desktop, browser or on cloud.

Applications created with LightSwitch support exporting data to Microsoft Office Excel for fast and easy sharing and reporting. You can also attach your application to existing data sources, which makes it easy to collect, analyze, and reuse information from a variety of data sources including Microsoft SQL Server, Microsoft SQL Azure, SharePoint, Microsoft Office Access (post-Beta), and other third-party data sources.

With LightSwitch you can create custom applications for the way you do business. Keep your technology and business options open, while building a practical yet scalable application that matches your current needs now and in the future. The pre-built templates and components in LightSwitch are fully extensible, so you can get the specific functionality your application demands. In addition, your application can grow to meet the increasing demands of popular applications using the Microsoft Windows Azure Cloud Hosting option.

Optional Parameters in C#

With the release of .NET 4.0 C# programmers are now able to created methods with optional parameters like VB programmers doing. Optional arguments are widely used in VBA for long time. Although they make life a little bit easier for programmers (you don’t have to repeat default values in your method calls).

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace optionalParameters
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             OptionalParameterTest t = new OptionalParameterTest();
  13.             Console.WriteLine("Optional Parameter :  {0}", t.OptionalParameter("One"));
  14.             Console.WriteLine("Optional Parameter :  {0} ", t.OptionalParameter("One1", "Two1"));
  15.             Console.WriteLine("Optional Parameter :  {0} ", t.OptionalParameter("One1", "Two1","three1"));
  16.             Console.WriteLine("Optional Parameter :  {0} ", t.OptionalParameter("One1", "Two1","Three1","Four1"));
  17.             Console.ReadLine();
  18.  
  19.         }
  20.  
  21.     }
  22.     public class OptionalParameterTest
  23.     {
  24.         public string OptionalParameter(string  one, string two = "Two",string three="Three",string four="Four")
  25.         {
  26.  
  27.            
  28.             return one + "  " + two + "  " + three + "  " + four;
  29.         }
  30.     }
  31.  
  32. }

Optional Parameters