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

How to reduce the space between bars of excel chart

http://annkemery.com/adjusting-bar-chart-spacing/

Posted in Information Technology | Leave a comment

C# code to retrieve image dimensions

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

namespace ConsoleApplication1
{
    class Program
    {
        public static void Main(string[] args)
        {
            string path = @"C:\Users\saikatd\Test";
            if (File.Exists(path))
            {
                // This path is a file
                ProcessFile(path);
            }
            else if (Directory.Exists(path))
            {
                // This path is a directory
                ProcessDirectory(path);
            }
            else
            {
                Console.WriteLine("{0} is not a valid file or directory.", path);
            }
        }


        // Process all files in the directory passed in, recurse on any directories 
        // that are found, and process the files they contain.
        public static void ProcessDirectory(string targetDirectory)
        {
            // Process the list of files found in the directory.
            string[] fileEntries = Directory.GetFiles(targetDirectory);
            foreach (string fileName in fileEntries)
                ProcessFile(fileName);

            // Recurse into subdirectories of this directory.
            string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
            foreach (string subdirectory in subdirectoryEntries)
                ProcessDirectory(subdirectory);
        }

        // Insert logic for processing found files here.
        public static void ProcessFile(string path)
        {
            Image img = Image.FromFile(path);
            if(img.Width != 3088 || img.Height != 2056) //Canon 5MP Default Resolution
            {
                Console.WriteLine("Path: " + path);
                Console.WriteLine("Width: " + img.Width + ", Height: " + img.Height);
            }
            img.Dispose();
        }
    }
}

Output
=======
Path: C:\Users\saikatd\Test\IMG_0323.jpg
Width: 3009, Height: 2003
Path: C:\Users\saikatd\Test\IMG_9966.jpg
Width: 3009, Height: 2003
Path: C:\Users\saikatd\Test\IMG_9969.jpg
Width: 3009, Height: 2003
Path: C:\Users\saikatd\Test\IMG_9971.jpg
Width: 3009, Height: 2003
Path: C:\Users\saikatd\Test\IMG_9977.jpg
Width: 2935, Height: 1954
Path: C:\Users\saikatd\Test\IMG_9981.jpg
Width: 3009, Height: 2003
Path: C:\Users\saikatd\Test\IMG_9984.JPG
Width: 3031, Height: 2056
Press any key to continue . . .
Posted in Information Technology | Leave a comment