Number of ways to have breakfast

Find the number of ways you can have breakfast in ‘n’ days, given Bread-butter can be eaten every day, Pizza can be eaten every alternate day and Burger can be eaten every two days.

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

namespace ConsoleApplication1
{
    class Program
    {
        public static int counter = 0;
        static void Main(string[] args)
        {
            string[] menu = {"Bread-Butter", "Pizza", "Burger" };
            int[] constraint = { 0, 1, 2 };
            int days = 3;
            GenerateCombinations(menu, constraint, days, "", 0);
            Console.WriteLine("\nNumber of ways to have breakfast = " + counter);
        }

        public static void GenerateCombinations(string[] menu, int[] constraint, int days, string str, int depth)
        {
            if(depth == days)
            {
                //Check for valid combinations and return
                if(IsValidCombo(menu, constraint, str))
                {
                    counter++;
                    Console.WriteLine(str);
                }
                return;
            }
            else
            {
                for(int i = 1; i <= Math.Pow(2, menu.Length) - 1; i++)
                {
                    string comboStr = Convert.ToString(i, 2).PadLeft(menu.Length, '0');
                    string tempStr = "";
                    for (int j = 0; j < comboStr.Length; j++)
                    {
                        if (int.Parse(comboStr[j].ToString()) == 1)
                        {
                            tempStr = tempStr + menu[j] + " ";
                        }
                    }
                    GenerateCombinations(menu, constraint, days, str + tempStr.Trim() + ";", depth + 1);
                }
            }
        }
        public static bool IsValidCombo(string[] menu, int[] constraint, string combo)
        {
            combo = combo.Trim(';');
            int[] position = new int[constraint.Length];
            for(int i = 0; i < position.Length; i++)
            {
                position[i] = -1;
            }
            Regex reg = new Regex(";");
            string[] str = reg.Split(combo);
            reg = new Regex(" ");
            for (int i = 0; i < str.Length; i++)//loop count = number of days
            {
                string[] tempStr = reg.Split(str[i]);
                for(int j = 0; j < tempStr.Length; j++)//loop for each day
                {
                    int k = GetIndex(menu, tempStr[j]);
                    if (position[k] == -1)
                    {
                        position[k] = i;
                    }
                    else//food item was consumed before
                    {
                        if(i - position[k] < constraint[k] + 1)
                        {
                            return false;
                        }
                        else
                        {
                            position[k] = i;
                        }
                    }
                }
            }
            return true;
        }
        public static int GetIndex(string[] menu, string str)
        {
            for(int i = 0; i < menu.Length; i++)
            {
                if (menu[i] == str)
                    return i;
            }
            return -1;
        }
    }
}

Output
=======
Burger;Pizza;Bread-Butter;
Burger;Bread-Butter;Pizza;
Burger;Bread-Butter;Bread-Butter;
Burger;Bread-Butter;Bread-Butter Pizza;
Burger;Bread-Butter Pizza;Bread-Butter;
Pizza;Burger;Pizza;
Pizza;Burger;Bread-Butter;
Pizza;Burger;Bread-Butter Pizza;
Pizza;Bread-Butter;Burger;
Pizza;Bread-Butter;Pizza;
Pizza;Bread-Butter;Pizza Burger;
Pizza;Bread-Butter;Bread-Butter;
Pizza;Bread-Butter;Bread-Butter Burger;
Pizza;Bread-Butter;Bread-Butter Pizza;
Pizza;Bread-Butter;Bread-Butter Pizza Burger;
Pizza;Bread-Butter Burger;Pizza;
Pizza;Bread-Butter Burger;Bread-Butter;
Pizza;Bread-Butter Burger;Bread-Butter Pizza;
Pizza Burger;Bread-Butter;Pizza;
Pizza Burger;Bread-Butter;Bread-Butter;
Pizza Burger;Bread-Butter;Bread-Butter Pizza;
Bread-Butter;Burger;Pizza;
Bread-Butter;Burger;Bread-Butter;
Bread-Butter;Burger;Bread-Butter Pizza;
Bread-Butter;Pizza;Burger;
Bread-Butter;Pizza;Bread-Butter;
Bread-Butter;Pizza;Bread-Butter Burger;
Bread-Butter;Pizza Burger;Bread-Butter;
Bread-Butter;Bread-Butter;Burger;
Bread-Butter;Bread-Butter;Pizza;
Bread-Butter;Bread-Butter;Pizza Burger;
Bread-Butter;Bread-Butter;Bread-Butter;
Bread-Butter;Bread-Butter;Bread-Butter Burger;
Bread-Butter;Bread-Butter;Bread-Butter Pizza;
Bread-Butter;Bread-Butter;Bread-Butter Pizza Burger;
Bread-Butter;Bread-Butter Burger;Pizza;
Bread-Butter;Bread-Butter Burger;Bread-Butter;
Bread-Butter;Bread-Butter Burger;Bread-Butter Pizza;
Bread-Butter;Bread-Butter Pizza;Burger;
Bread-Butter;Bread-Butter Pizza;Bread-Butter;
Bread-Butter;Bread-Butter Pizza;Bread-Butter Burger;
Bread-Butter;Bread-Butter Pizza Burger;Bread-Butter;
Bread-Butter Burger;Pizza;Bread-Butter;
Bread-Butter Burger;Bread-Butter;Pizza;
Bread-Butter Burger;Bread-Butter;Bread-Butter;
Bread-Butter Burger;Bread-Butter;Bread-Butter Pizza;
Bread-Butter Burger;Bread-Butter Pizza;Bread-Butter;
Bread-Butter Pizza;Burger;Pizza;
Bread-Butter Pizza;Burger;Bread-Butter;
Bread-Butter Pizza;Burger;Bread-Butter Pizza;
Bread-Butter Pizza;Bread-Butter;Burger;
Bread-Butter Pizza;Bread-Butter;Pizza;
Bread-Butter Pizza;Bread-Butter;Pizza Burger;
Bread-Butter Pizza;Bread-Butter;Bread-Butter;
Bread-Butter Pizza;Bread-Butter;Bread-Butter Burger;
Bread-Butter Pizza;Bread-Butter;Bread-Butter Pizza;
Bread-Butter Pizza;Bread-Butter;Bread-Butter Pizza Burger;
Bread-Butter Pizza;Bread-Butter Burger;Pizza;
Bread-Butter Pizza;Bread-Butter Burger;Bread-Butter;
Bread-Butter Pizza;Bread-Butter Burger;Bread-Butter Pizza;
Bread-Butter Pizza Burger;Bread-Butter;Pizza;
Bread-Butter Pizza Burger;Bread-Butter;Bread-Butter;
Bread-Butter Pizza Burger;Bread-Butter;Bread-Butter Pizza;

Number of ways to have breakfast = 63
Press any key to continue . . .
Posted in Information Technology | Leave a comment

For a given Sum and N, print all the combinations

For example if Sum = 16 and N = 2, then the output should be as follows:
0 16
1 15
2 14
3 13
4 12
5 11
6 10
7 9
8 8
9 7
10 6
11 5
12 4
13 3
14 2
15 1
16 0

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum = 10;
            int count = 3;
            PrintAllCombinations(sum, count, 0, "");
        }

        public static void PrintAllCombinations(int sum, int count, int depth, string str)
        {
            if(depth == count)
            {
                //Perform validation and return
                if(sum == Sum(str))
                {
                    Console.WriteLine(str);
                }
                return;
            }
            else
            {
                for(int i = 0; i <= sum; i++)
                {
                    PrintAllCombinations(sum, count, depth + 1, str + i.ToString() + " ".ToString());
                }
            }
        }
        public static int Sum (string str)
        {
            Regex reg = new Regex(" ");
            string[] strArray = reg.Split(str.Trim());
            int sum = 0;
            for(int i = 0; i < strArray.Length; i++)
            {
                sum = sum + int.Parse(strArray[i]);
            }
            return sum;
        }
    }
}

Output
=======
0 0 10
0 1 9
0 2 8
0 3 7
0 4 6
0 5 5
0 6 4
0 7 3
0 8 2
0 9 1
0 10 0
1 0 9
1 1 8
1 2 7
1 3 6
1 4 5
1 5 4
1 6 3
1 7 2
1 8 1
1 9 0
2 0 8
2 1 7
2 2 6
2 3 5
2 4 4
2 5 3
2 6 2
2 7 1
2 8 0
3 0 7
3 1 6
3 2 5
3 3 4
3 4 3
3 5 2
3 6 1
3 7 0
4 0 6
4 1 5
4 2 4
4 3 3
4 4 2
4 5 1
4 6 0
5 0 5
5 1 4
5 2 3
5 3 2
5 4 1
5 5 0
6 0 4
6 1 3
6 2 2
6 3 1
6 4 0
7 0 3
7 1 2
7 2 1
7 3 0
8 0 2
8 1 1
8 2 0
9 0 1
9 1 0
10 0 0
Press any key to continue . . .
Posted in Information Technology | Leave a comment

Think twice before deleting files from your Flickr account

On Flickr, there is no “trash” or “recycle bin”. When you delete something on Flickr, it’s actually deleted forever.

There seems to be a basic flaw with the design. As an end user when I add a photo to multiple albums and then open one of the albums and delete it, nowhere in the prompt messages it states that the photo will be deleted from all the albums. For my specific instance, I couldn’t find an obvious option to move the photo from one folder to the other and hence added it to the desired album and later deleted it from the other. Surprisingly the image got deleted from both the albums and there is absolutely no mechanism in place to recover it. This is really annoying 😦

Posted in Information Technology | Leave a comment

Why does my voice sound so different when it is recorded and played back?

https://www.scientificamerican.com/article/why-does-my-voice-sound-different/

Sound can reach the inner ear by way of two separate paths, and those paths in turn affect what we perceive. Air-conducted sound is transmitted from the surrounding environment through the external auditory canal, eardrum and middle ear to the cochlea, the fluid-filled spiral in the inner ear. Bone-conducted sound reaches the cochlea directly through the tissues of the head.

When you speak, sound energy spreads in the air around you and reaches your cochlea through your external ear by air conduction. Sound also travels from your vocal cords and other structures directly to the cochlea, but the mechanical properties of your head enhance its deeper, lower-frequency vibrations. The voice you hear when you speak is the combination of sound carried along both paths. When you listen to a recording of yourself speaking, the bone-conducted pathway that you consider part of your “normal” voice is eliminated, and you hear only the air-conducted component in unfamiliar isolation. You can experience the reverse effect by putting in earplugs so you hear only bone-conducted vibrations.

Posted in Science | Leave a comment

Total amount of time covered by the intervals

There’s a room with a TV and people are coming in and out to watch it. The TV is on only when there’s at least a person in the room. For each person that comes in, we record the start and end time. We want to know for how long the TV has been on. In other words, given a list of arrays of time intervals, write a function that calculates the total amount of time covered by the intervals.

For example:
input = [(1,4), (2,3)]
output = 3
input = [(4,6), (1,2)]
output = 3
input = [(1,4), (6,8), (2,4), (7,9), (10, 15)]
output = 11

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numArray = { 1, 4, 6, 8, 2, 4, 7, 9, 10, 15 };
            //Sort the array based on starting point
            numArray = BubbleSort(numArray);
            ArrayList numArrayList = new ArrayList();
            numArrayList.AddRange(numArray);
            for (int i = 1; i = (int)numArrayList[i + 1]) && ((int)numArrayList[i] = (int)numArrayList[i + 1]) && ((int)numArrayList[i] >= (int)numArrayList[i + 2]))
                {
                    numArrayList.RemoveAt(i + 1);
                    numArrayList.RemoveAt(i + 1);
                    i = i - 2;
                }
            }
            int totalTime = 0;
            for (int i = 0; i < numArrayList.Count - 1; i = i + 2)
            {
                totalTime = totalTime + ((int)numArrayList[i + 1] - (int)numArrayList[i]);
            }
            Console.WriteLine("Total time covered = " + totalTime);
        }

        public static int[] BubbleSort(int[] numArray)
        {
            int temp1, temp2;
            for (int pass = 0; pass <= numArray.Length - 4; pass++)
            {
                for (int i = 0; i  numArray[i + 2])
                    {
                        temp1 = numArray[i];
                        temp2 = numArray[i + 1];
                        numArray[i] = numArray[i + 2];
                        numArray[i + 1] = numArray[i + 3];
                        numArray[i + 2] = temp1;
                        numArray[i + 3] = temp2;
                    }
                }
            }
            return numArray;
        }
    }
}

Output
=======
Total time covered = 11
Press any key to continue . . .
Posted in Information Technology | Leave a comment

Summation of different combinations

You have a string of digits say 123. You can insert a + or – sign in front of every digit. Find all of the different possibilities and return the sum of all results.

For example:
+1+2+3 = 6
+12+3 = 15
+123 = 123
+1+23 = 24

-1-2-3 = 6

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        public static int sum = 0;
        static void Main(string[] args)
        {
            string str = "123";
            GenerateCombinations(str, 0, "", "");
            Console.WriteLine("Total Sum = " + sum);
        }

        public static void GenerateCombinations(string str, int startPos, string tempStrWithoutSymbol, string tempStrWithSymbol)
        {
            if (tempStrWithoutSymbol == str)
            {
                Console.WriteLine(tempStrWithSymbol + " = " + Calculate(tempStrWithSymbol));
                sum = sum + Calculate(tempStrWithSymbol);
            }

            else
            {
                for (int len = 1; len <= str.Length - startPos; len++)
                {
                    GenerateCombinations(str, startPos + len, tempStrWithoutSymbol + str.Substring(startPos, len), tempStrWithSymbol + "+" + str.Substring(startPos, len));
                    GenerateCombinations(str, startPos + len, tempStrWithoutSymbol + str.Substring(startPos, len), tempStrWithSymbol + "-" + str.Substring(startPos, len));
                }
            }
        }

        public static int Calculate(string strExp)
        {
            DataTable dt = new DataTable();
            return ((int)dt.Compute(strExp, ""));
        }
    }
}

Output
=======
+1+2+3 = 6
+1+2-3 = 0
+1-2+3 = 2
+1-2-3 = -4
+1+23 = 24
+1-23 = -22
-1+2+3 = 4
-1+2-3 = -2
-1-2+3 = 0
-1-2-3 = -6
-1+23 = 22
-1-23 = -24
+12+3 = 15
+12-3 = 9
-12+3 = -9
-12-3 = -15
+123 = 123
-123 = -123
Total Sum = 0
Press any key to continue . . .
Posted in Information Technology | Leave a comment

Application stages and timelines for OCI

Overall it took 5+ months to complete the entire application process.

Application Status Remarks Date of Status
Your Passport and OCI Card have now reached CKGS Application Centre from the Embassy / Consulate and is being sorted and will be ready for dispatch through the Pre-Paid Envelope label provided by you.
Your AWB Number is XXXXXXXXXXXX.
To Track the progress of your shipment, log onto the site of your Service Provider and enter your AWB Number.
02/11/2017
Your Documents have now reached CKGS application centre from the Embassy / Consulate.
Your document(s) is being sorted and will be ready for shipment shortly. Please continue tracking your application status online.
02/11/2017
Your printed OCI Card is ready for match-up with your US Passport / Foreign Passport. Please complete the OCI match-up process and refer to the email sent to you from CKGS with further details.
To make sure you receive our alerts / updates, add noreply.usa@ckgs.com to your white list. Please continue tracking your application status online.
10/16/2016
Your OCI card has been dispatched from India to the Embassy / Consulate. It will take 1 to 2 weeks before it arrives. Please continue tracking your application status online. 09/27/2016
Application is under process at the Embassy / Consulate for further scrutinization and may take upto 6 to 8 weeks for next update. Please continue tracking your application status online. 08/31/2016
Your Application is under process at the Embassy / Consulate. Please continue tracking your application status online. 08/29/2016
Your application has been processed at the CKGS Application Centre and is in transit to the Indian Embassy/Consulate for decision making.
Please continue tracking your application status online.
08/26/2016
We have received your incomplete / missing documents and we are verifying the same.
You will receive an update shortly on the progress of your application.
Please continue tracking your application status online.
ADDITIONAL DOCUMENTS RECEIVED (FEDEX XXXX XXXX XXXX) 08/26/2016
Your Application is on hold due to the incomplete / missing documents as mentioned under the REMARKS column. Only once the above documents are received by us at CKGS and post verification, will we be able to continue to process your application.
Please refer to our EMAIL sent to you from CKGS with further details.To make sure you receive our alerts / updates, add noreply.usa@ckgs.com to your white list / Approved / Safe Sender list.
1.PHOTO UNACCEPTABLE TOO ORANGE. 08/23/2016
We have received your payment and we are verifying the same. If the payment made is complete, you will receive a payment receipt shortly. If there is any further discrepancy, you will receive an update. Please continue tracking your application status online. 08/23/2016
Your application has been received and is under process in CKGS. Please continue tracking your application status online. Your tracking id is USAXXXXXXXXX 08/23/2016
Posted in Business, Travel | Leave a comment