Program to find amount of water in a given glass

There are some glasses with equal capacity as 1 litre. The glasses are kept as follows:

1
2 3
4 5 6
7 8 9 10

You can put water to only top glass. If you put more than 1 litre water to 1st glass, water overflows and fills equally in both 2nd and 3rd glasses. Glass 5 will get water from both 2nd glass and 3rd glass and so on.
If you have X litre of water and you put that water in top glass, how much water will be contained by jth glass in ith row?

Example. If you will put 2 litre on top.
1st – 1 litre
2nd – 1/2 litre
3rd – 1/2 litre

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        public class Glass
        {
            public double volume;
        }
        public static void Main(string[] args)
        {
            double volumeOfWater = 100;
            int rowNum = 7, colNum = 7;
            double glassCapacity = 1;
            Glass[] glass = new Glass[(rowNum + 1) * (rowNum + 1 + 1)/2];//add an extra row
            //Initialize the glass array and store it in a list
            List<List<Glass>> glassPattern = new List<List<Glass>>();
            for (int glassCounter = 0, row = 0; row < rowNum + 1; row++)
            {
                List<Glass> subList = new List<Glass>();
                for (int col = 0; col <= row; col++)
                {
                    glass[glassCounter] = new Glass();
                    glass[glassCounter].volume = 0;
                    subList.Add(glass[glassCounter++]);
                }
                glassPattern.Add(subList);
            }
            //Fill the top glass
            glassPattern[0][0].volume = volumeOfWater;
            //Mock the waterflow
            int flag = 0;
            for (int i = 0; i < glassPattern.Count - 1 && flag == 0; i++)//for each row but the last
            {
                for (int j = 0; j < glassPattern[i].Count; j++)//each column
                {
                   if(glassPattern[i][j].volume > glassCapacity) //nothing to do if volume <= glassCapacity
                    {
                        double temp = glassPattern[i][j].volume;
                        glassPattern[i][j].volume = glassCapacity;
                        glassPattern[i + 1][j].volume = glassPattern[i + 1][j].volume + (temp - glassCapacity) / 2;
                        glassPattern[i + 1][j + 1].volume = glassPattern[i + 1][j + 1].volume + (temp - glassCapacity) / 2;
                    }
                   if(i == rowNum - 1 && j == colNum - 1)
                    {
                        Console.WriteLine("Volume of water in glass position (" + rowNum + "," + colNum + ") = " + glassPattern[i][j].volume);
                        flag = 1;
                        break;
                    }
                }
            }
        }
    }
}

Output
=======
Volume of water in glass position (7,7) = 0.578125
Press any key to continue . . .
Posted in Information Technology | Leave a comment

Application stages and timelines for Indian Entry Visa

Overall it took 7 days to complete the entire application process. It excludes the passport delivery timelines which depends on the opted shipping speed with the carrier (FedEx/UPS/USPS).

1. Received
2. Verification
3. UNDER PROCESS at CKGS
4. IN TRANSIT to Embassy
5. UNDER PROCESS at Embassy
6. IN TRANSIT to CKGS
7. Sorting
8. Dispatch
9. Delivered

Oct 12, 2016 09:45 pm Application is ‘Dispatch’
Passport picked up by UPS from CKGS in the Pre-Paid Envelope provided by you.

Your AWB Number is XXXXXXXXXXXXXX9083

To track the ‘Progress of Your Shipment’, log onto the site of your Service Provider and enter your AWB Number.

View History (reverse chronological order)
====================================

Oct 12, 2016 – Wednesday
09:45 pm
Passport picked up by UPS from CKGS in the Pre-Paid Envelope provided by you.
Your AWB Number is XXXXXXXXXXXXXX9083
To track the ‘Progress of Your Shipment’, log onto the site of your Service Provider and enter your AWB Number.

Oct 12, 2016 – Wednesday
08:50 pm
Passport ‘Received’ at the CKGS application center and is at the ‘Sorting Facility’ where it is being processed for delivery.

Oct 12, 2016 – Wednesday
07:21 pm
Application ‘Reviewed’ by Consulate and ‘In Transit’ to CKGS Application Centre.

Oct 07, 2016 – Friday
11:40 am
Application is ‘Under Process’ at Consulate for decision making.

Oct 06, 2016 – Thursday
08:22 pm
Application is ‘In Transit’ to the Consulate.

Oct 06, 2016 – Thursday
04:38 pm
Application is ‘Under Process’ at CKGS.

Oct 04, 2016 – Tuesday
06:29 pm
Application Verification is ‘Complete’. Processing of your application will continue.
The Consulate may request for further documents over and above those already submitted as per the Document Checklist.
Please continue tracking your application status online.

Oct 03, 2016 – Monday
07:48 pm
Application ‘Received’ by CKGS and ‘Not Verified’.
You will receive an E-mail advising you of the outcome of the verification process.

Posted in Business, Travel | 1 Comment

Online Video Editing Tool

http://online-video-cutter.com/

Posted in Information Technology | Leave a comment

Optimized Painting

You are given a range [first, last], initially white. You need to paint it black.
For this purpose you have a set of triples
[(f, l, cost), …] – where each triple means that you can paint the range [f, l] for ‘cost’ coins (limitations: cost is floating point >= 0; f, l are integers representing start and end of the range to be painted black).
Find minimum cost needed to paint the whole range [first, last] or return -1 if it’s impossible

Example:
[first, last] = [0, 5] and set of triples are
[[0, 5, 10], [0, 4, 1], [0, 2, 5], [2, 5, 1]]
Clearly the answer is to take [0, 4, 1] and [2, 5, 1] – the total cost will be 2.

Another example:
[first, last] = [0, 5]
triples are [[1,4, 10], [2, 5, 6]]
answer is -1, because it’s impossible to color the whole range.

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    public class Triplet
    {
        public int start;
        public int end;
        public float cost;
    }

    class Program
    {
        public static void Main(string[] args)
        {
            string rangeStr = "[0, 5]";
            string tripletStr = "[0, 5, 10], [0, 4, 1], [0, 2, 5], [2, 5, 1]";
            char[] trimChar = { '[', ']' };
            Regex reg = new Regex(",");
            string[] rangeSplit = reg.Split(rangeStr.Trim(trimChar));
            int start = int.Parse(rangeSplit[0].Trim());
            int end = int.Parse(rangeSplit[1].Trim());
            Triplet[] triplets = ParseTriplet(tripletStr);
            //Filter out valid triplets
            ArrayList al = new ArrayList();
            for (int i = 0; i < triplets.Length; i++)
            {
                if ((start >= triplets[i].start && start < triplets[i].end) || (end > triplets[i].start && end <= triplets[i].end) || (triplets[i].start >= start && triplets[i].start < end) || (triplets[i].end > start && triplets[i].end <= end))
                {
                    al.Add(i);
                }
            }
            //Generate combinations
            float minCost = 0; ArrayList comboList = new ArrayList();
            int flag = 0;
            int maxLimit = (int)Math.Pow(2, al.Count) - 1;
            for (int i = 1; i <= maxLimit; i++)
            {
                string binary = DecimalToBinary(i).PadLeft(al.Count, '0');
                float tempCost = 0; ArrayList tempList = new ArrayList();
                for (int j = 0; j < binary.Length; j++)
                {
                    if (binary[j] != '0')
                    {
                        tempCost = tempCost + triplets[(int)al[j]].cost;
                        tempList.Add(triplets[(int)al[j]]);
                    }
                }
                //Check for valid combinations
                if (ValidCombo(tempList, start, end))
                {
                    if (flag == 0)
                    {
                        flag = 1;
                        comboList.AddRange(tempList);
                        minCost = tempCost;
                    }
                    else if (tempCost < minCost)
                    {
                        comboList.Clear();
                        comboList.AddRange(tempList);
                        minCost = tempCost;
                    }
                }
            }
            if (comboList.Count == 0)
            {
                Console.WriteLine("No solution found...");
            }
            else
            {
                Console.WriteLine("Minimum Cost = " + minCost);
                for (int i = 0; i < comboList.Count; i++)
                {
                    Console.WriteLine("[" + ((Triplet)comboList[i]).start + ", " + ((Triplet)comboList[i]).end + ", " + ((Triplet)comboList[i]).cost + "]");
                }
            }
        }

        public static bool ValidCombo(ArrayList tempList, int start, int end)
        {
            Hashtable ht = CreateHashTable(start, end);
            for (int i = 0; i < tempList.Count; i++)
            {
                for (int j = ((Triplet)tempList[i]).start; j < ((Triplet)tempList[i]).end; j++)
                {
                    if (ht.Contains(j.ToString() + ";" + (j + 1).ToString()))
                    {
                        ht[j.ToString() + ";" + (j + 1).ToString()] = 1;
                    }
                }
            }
            return CheckHashTable(ht);
        }

        public static bool CheckHashTable(Hashtable ht)
        {
            foreach (DictionaryEntry entry in ht)
            {
                if ((int)entry.Value == 0)
                    return false;
            }
            return true;
        }

        public static Hashtable CreateHashTable(int start, int end)
        {
            Hashtable ht = new Hashtable();
            for (int i = start; i < end; i++)
            {
                ht.Add(i.ToString() + ";" + (i + 1).ToString(), 0);
            }
            return ht;
        }

        public static string DecimalToBinary(int num)
        {
            string rem = "";
            while (num >= 1)
            {
                int quot = num / 2;
                rem += (num % 2).ToString();
                num = quot;
            }
            // Reversing the  value
            string bin = "";
            for (int i = rem.Length - 1; i >= 0; i--)
            {
                bin = bin + rem[i];
            }
            return bin;
        }

        public static Triplet[] ParseTriplet(string tripletStr)
        {
            Triplet[] triplets = new Triplet[GetNumberOfTriplets(tripletStr)];
            string tempStr = ""; int flag = 0, counter = 0;
            for (int i = 0; i < tripletStr.Length; i++)
            {
                if (tripletStr[i] == '[')
                {
                    flag = 1;
                }
                else if (tripletStr[i] == ']')
                {
                    flag = 0;
                    Regex reg = new Regex(",");
                    string[] tripletSplit = reg.Split(tempStr);
                    triplets[counter] = new Triplet();
                    triplets[counter].start = int.Parse(tripletSplit[0].Trim());
                    triplets[counter].end = int.Parse(tripletSplit[1].Trim());
                    triplets[counter].cost = float.Parse(tripletSplit[2].Trim());
                    counter++;
                    tempStr = "";
                }
                else if (flag == 1)
                {
                    tempStr = tempStr + tripletStr[i];
                }
            }
            return triplets;
        }

        public static int GetNumberOfTriplets(string tripletStr)
        {
            int count = 0;
            for (int i = 0; i < tripletStr.Length; i++)
            {
                if (tripletStr[i] == '[')
                    count++;
            }
            return count;
        }
    }
}

Output
=======
Minimum Cost = 2
[0, 4, 1]
[2, 5, 1]
Press any key to continue . . .
Posted in Information Technology | Leave a comment

Generate all possible combinations when two strings are merged while retaining the same character sequence

Given two strings A and B, generate all possible combinations when B is merged into A.
Example: A = “hey”
B: “sam”
Then the combinations are:
“heysam”, “hseaym”, “hesaym”, “sahemy” etc.

Note that the character order remains the same for both the strings while merging.

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        public static void Main(string[] args)
        {
            string str1 = "sam";
            string str2 = "hey";
            Merge(str1, str2, "", 0, 0, 0);
            Merge(str2, str1, "", 0, 0, 0);
        }
        public static void Merge (string str1, string str2, string str, int pos1, int pos2, int toggle)
        {
            if (str.Length == str1.Length + str2.Length)
            {
                Console.WriteLine(str);
            }
            else
            {
                if(toggle == 0)
                {
                    for (int i = 1; i <= str1.Length; i++)//string length
                    {
                        if(pos1 + i <= str1.Length)
                        {
                            Merge(str1, str2, str + str1.Substring(pos1, i), pos1 + i, pos2, 1);
                        }
                    }
                }
                else
                {
                    for (int i = 1; i <= str2.Length; i++)//string length
                    {
                        if (pos2 + i <= str2.Length)
                        {
                            Merge(str1, str2, str + str2.Substring(pos2, i), pos1, pos2 + i, 0);
                        }
                    }
                }
            }
        }
    }
}

Output
=======
shaemy
shaeym
shamey
sheaym
sheamy
sheyam
sahmey
sahemy
saheym
samhey
hseaym
hseamy
hseyam
hsaemy
hsaeym
hsamey
hesyam
hesaym
hesamy
heysam
Press any key to continue . . .
Posted in Information Technology | Leave a comment

Print inorder combinations of an array

Take an array and print non over lapping in order pairs.
Example:
# [1,2,3,4] => input

# output below is in order combination

# (1234)
# (1)(234)
# (1)(23)(4)
# (1)(2)(34)
# (12)(34)
# (12)(3)(4)
# (123)(4)
# (1)(2)(3)(4)

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        public static char[] charArray = { '1', '2', '3', '4', '5', '6' };

        public static void Main(string[] args)
        {
            int[] stepArray = new int[charArray.Length];
            for(int i = 0; i < stepArray.Length; i++)
            {
                stepArray[i] = i + 1;
            }
            PrintInorderCombination(charArray.Length, stepArray, 0, "");
        }

        public static void PrintInorderCombination(int sum, int[] stepArray, int tempSum, string str)
        {
            if (tempSum > sum)
            {
                return;
            }
            else if (tempSum == sum && str.Length != 0)
            {
                Regex reg = new Regex(" ");
                string[] combinationString = reg.Split(str);
                int counter = 0;
                for(int i = 0; i < combinationString.Length; i++)
                {
                    Console.Write("(");
                    for (int j = 0; j < int.Parse(combinationString[i]); j++)
                    {
                        Console.Write(charArray[counter++]);
                    }
                    Console.Write(")");
                }
                Console.WriteLine();
            }
            else
            {
                for (int i = 0; i < stepArray.Length; i++)
                {
                    if (str == "")
                    {
                        PrintInorderCombination(sum, stepArray, tempSum + stepArray[i], str + stepArray[i].ToString());
                    }
                    else
                    {
                        PrintInorderCombination(sum, stepArray, tempSum + stepArray[i], str + " " + stepArray[i].ToString());
                    }
                }
            }
        }
    }
}

Output
=======
(1)(2)(3)(4)(5)(6)
(1)(2)(3)(4)(56)
(1)(2)(3)(45)(6)
(1)(2)(3)(456)
(1)(2)(34)(5)(6)
(1)(2)(34)(56)
(1)(2)(345)(6)
(1)(2)(3456)
(1)(23)(4)(5)(6)
(1)(23)(4)(56)
(1)(23)(45)(6)
(1)(23)(456)
(1)(234)(5)(6)
(1)(234)(56)
(1)(2345)(6)
(1)(23456)
(12)(3)(4)(5)(6)
(12)(3)(4)(56)
(12)(3)(45)(6)
(12)(3)(456)
(12)(34)(5)(6)
(12)(34)(56)
(12)(345)(6)
(12)(3456)
(123)(4)(5)(6)
(123)(4)(56)
(123)(45)(6)
(123)(456)
(1234)(5)(6)
(1234)(56)
(12345)(6)
(123456)
Press any key to continue . . .
Posted in Information Technology | Leave a comment

Volume of water that can be filled in the valley

Given a one dimensional array of integers representing the valley configuration, determine the total volume of water that can be filled without any spillover.
Elevation

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        public static void Main(string[] args)
        {
            int[] elevationArray = { 1, 2, 3, 1, 2, 1, 4, 6, 5, 4, 2, 3, 4, 1 };
            int waterVolume = 0, flag = 0;
            while (flag == 0)
            {
                flag = 1;
                int start = -1, end = -1;
                for (int i = 0; i < elevationArray.Length - 1; i++)
                {
                    if (elevationArray[i] > elevationArray[i + 1]) //dip
                    {
                        start = i;
                    }
                    else if (elevationArray[i] < elevationArray[i + 1] && start != -1) //rise followed by a dip
                    {
                        end = i + 1;
                        Console.WriteLine("Start = " + start + " End = " + end);
                        for (int j = start + 1; j < end; j++)
                        {
                            waterVolume = waterVolume + (Math.Min(elevationArray[start], elevationArray[end]) - elevationArray[j]);
                            elevationArray[j] = Math.Min(elevationArray[start], elevationArray[end]);
                        }
                        flag = 0; //need to iterate again
                        start = -1; //reset start position
                    }
                }
            }
            Console.WriteLine("Water Volume = " + waterVolume);
        }
    }
}

Output
=======
Start = 2 End = 4
Start = 4 End = 6
Start = 9 End = 11
Start = 2 End = 6
Start = 9 End = 12
Water Volume = 8
Press any key to continue . . .
Posted in Information Technology | Leave a comment