Monday 23 January 2017

Setting and getting Session values in Asp.NET Core 1.0

Session is accessed through the Session property on HttpContext. Session is an ISession implementation.

Simple Get Set Session in Core 1.0

The following extension methods, we can set and get serializable session objects:

Session Extensions Method

Implementing Session Extensions Method

Sunday 15 January 2017

Detect a Loop in Singly Linked List and Remove Loop

using System;
namespace ConsoleApplication1
{
    class Program
    {

        public class LinkedListNode
        {
            public LinkedListNode() { }
            public LinkedListNode(T value)
            {
                Value = value;
            }
            public T Value { get; set; }
            public LinkedListNode Next { get; set; }
        }

        static void Main(string[] args)
        {
            LinkedListNode Llist = new LinkedListNode();
            Llist.Next = new LinkedListNode(1);
            Llist.Next.Next = new LinkedListNode(2);
            Llist.Next.Next.Next = new LinkedListNode(3);
            Llist.Next.Next.Next.Next = new LinkedListNode(4);
            Llist.Next.Next.Next.Next.Next = new LinkedListNode(5);
            Llist.Next.Next.Next.Next.Next.Next = new LinkedListNode(6);

            Llist.Next.Next.Next.Next.Next.Next.Next = Llist.Next.Next.Next.Next;
            Console.WriteLine(FloydCycleDedection(Llist).ToString());
            Console.WriteLine(FloydCycleDedection(Llist).ToString());
            Console.Read();
        }

        public static bool FloydCycleDedection(LinkedListNode node)
        {
            LinkedListNode slow = node, fast = node;
            while (slow != null && fast != null && fast.Next != null)
            {
                slow = slow.Next;
                fast = fast.Next.Next;
                if (slow == fast)
                {
                    removeLoop(node, slow, fast);
                    return true;
                }
            }
            return false;
        }

        public static void removeLoop(LinkedListNode node, LinkedListNode slow, LinkedListNode fast)
        {
            /* If loop exists */
            if (slow == fast)
            {
                slow = node;
                while (slow != fast.Next)
                {
                    slow = slow.Next;
                }
                fast.Next = null; // remove loop
            }
        }
    }
}

Thursday 12 January 2017

Kadane's algorithm to find subarray with the maximum sum 2D array in C#

using System;
using System.Linq;

namespace test2
{
    class Program
    {

        static void Main(string[] args)
        {
            int[][] int1 = new int[][] {
                new int[] {1, 2, -1, -4, -20},
                new int[] {-8, -3, 4, 2, 1},
                new int[] {3, 8, 10, 1, 3},
                new int[] {-4, -1, 1, 7, -6}
            };
            findMaxSubMatrix(int1);

            //int[] kk = kadane(new int[] { -3,-2,-1 });
            //foreach(var item in kk)
            //{
            //    Console.WriteLine(item);
            //}
            Console.Read();
        }

        public static int[] kadane(int[] a)
        {
            //result[0] == maxSum, result[1] == start, result[2] == end;
            int[] result = new int[] { int.MinValue, 0, -1 };
            int currentSum = 0;
            int localStart = 0;

            for (int i = 0; i < a.Length; i++)
            {
                currentSum += a[i];
                if (currentSum < 0)
                {
                    currentSum = 0;
                    localStart = i + 1;
                }
                else if (currentSum > result[0])
                {
                    result[0] = currentSum;
                    result[1] = localStart;
                    result[2] = i;
                }
            }

            // all numbers in a are negative
            if (result[2] == -1)
            {
                for (int i = 0; i < a.Length; i++)
                {
                    if (a[i] > result[0])
                    {
                        result[0] = a[i];
                        result[1] = i;
                        result[2] = i;
                    }
                }
            }

            return result;
        }

        /**
        * To find and print maxSum, (left, top),(right, bottom)
        */
        public static void findMaxSubMatrix(int[][] a)
        {
            int cols = a[0].Length;
            int rows = a.Length;
            int[] currentResult;
            int maxSum = int.MinValue;
            int left = 0;
            int top = 0;
            int right = 0;
            int bottom = 0;

            for (int leftCol = 0; leftCol < cols; leftCol++)
            {
                int[] tmp = new int[rows];

                for (int rightCol = leftCol; rightCol < cols; rightCol++)
                {

                    for (int i = 0; i < rows; i++)
                    {
                        tmp[i] += a[i][rightCol];
                    }
                    currentResult = kadane(tmp);
                    if (currentResult[0] > maxSum)
                    {
                        maxSum = currentResult[0];
                        left = leftCol;
                        top = currentResult[1];
                        right = rightCol;
                        bottom = currentResult[2];
                    }
                }
            }
            Console.WriteLine("MaxSum: " + maxSum +
            ", range: [(" + left + ", " + top +
            ")(" + right + ", " + bottom + ")]");
        }
    }
}