Tuesday, October 31, 2017

Find Peak

            //given a number of characters find the peak character which is the current that is repeated the most, there can be more than one character at the peak if the number of repeatition tie
            var input = "abbabacdexxxxxxdx"; //return x
            var arr = input.ToCharArray();
            arr = arr.OrderBy(x=> x).ToArray(); //order the list to know when to stop counting occurence
            //Initialize result container
            var result = new List<char>();
            //Loop length of array - pointing to index from left p = 0 
            var currentMax = 0;
            var prevChar = arr[0];
            var r = 0;
            for (int p = 0; p < arr.Length ; p++)
            {
                if (arr[p] == prevChar)
                {
                    r++;
                }
                else
                {
                    if (r > currentMax)
                    {
                        result = new List<char> { arr[p - 1] };
                    }
                    else if (r == currentMax)
                    {
                        result.Add(arr[p - 1]);
                    }
                    currentMax = r;
                    prevChar = arr[p];
                    r = 1;
                }
            }
            //Keep previous character
            //Compare arr[p] to previous 
            //Equal count repeatition r of a
            //not equal stop counting repeatition and compare to max if less than max discard 
            //If equal to max append to peak
            //if less than discard
            //greater than max 
            //discard output collected previously and initialize it only with this character

            if (r > currentMax)
            {
                result = new List<char> { arr.Last() };
            }
            else if (r == currentMax)
            {
                result.Add(arr.Last());

            }

Reverse string without its special characters

Special characters need to stay in place while the entire string is reversed

 //Reverse string without moving any special character from its place
            var input = "ab$cde#x"; //"xe$cdb#a";
            var arr = input.ToCharArray();

            var h = -1;
            var t = -1;
            //Loop - half length of the array with pointer index of array from left
            for (int p = 0; p < arr.Length/2; p++)
            {
                //Find non special character from left
                if (Regex.IsMatch(arr[p].ToString(),"[a-z]") && h < 0) //non special character
                    h = p;

                //Find non special chracter from right
                if (Regex.IsMatch(arr[arr.Length - p - 1].ToString(), "[a-z]") && t < 0) //non special character
                    t = arr.Length - p - 1;


                //If holding two non special chracters then swap them
                if (h >= 0 && t >= 0) //Non special chracters both directions
                {
                    //Swap
                    var temp = arr[h];
                    arr[h] = arr[t];
                    arr[t] = temp;

                    h = -1;
                    t = -1;
                }
                
               



            }

Rotate Array

Rotate an array of n elements to the right by k steps.
For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. How many different ways do you know to solve this problem?

Using LINQ
            var arr = new List<int> { 1, 2, 3, 4, 5, 6, 7 };
            //Rorate array to the right
            var k = 10 % 7;

            //should move to the beginnning
            var output = arr.Skip(arr.Count - k).ToList();
            output.AddRange(arr.Take(arr.Count - k));
         

            Console.ReadLine();

Using Add Method

 var arr = new List<int> { 1, 2, 3, 4, 5, 6, 7 };
            //Rorate array to the right
            var k = 10 % 7;
            //Move the head of the array to the tail number of times = k
            for (int i = 0; i < arr.Count - k; i++)
            {
                arr.Add(arr[0]);
                arr.RemoveAt(0);
            }

Using Insert
 var arr = new List<int> { 1, 2, 3, 4, 5, 6, 7 };
            //Rorate array to the right
            var k = 10 % 7;
            //Move the head of the array to the tail number of times = k
            for (int i = 0; i < k; i++)
            {
                arr.Insert(0, arr.Last());
                arr.RemoveAt(arr.Count -1);
            }

Reverse Words in a string

Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.
The input string does not contain leading or trailing spaces and the words are always separated by a single space.
For example,
Given s = "the sky is blue",
return "blue is sky the".
Could you do it in-place without allocating extra space?

            
Allocating Extra space for output

var input = "ayman ali";
            input = " " + input;
            var arr = input.ToCharArray();
            var output = string.Empty;
            var b = 0;
            var e = arr.Length;
            for (int p = arr.Length - 1; p >= 0; p--)
            {
                if (arr[p] == ' ' )// little off beginning of word
                {
                    b = p + 1;
                    output += input.Substring(b, e - b) + " ";
                    e = p ;

                }
            }
            Console.WriteLine(output.TrimEnd(' '));

            Console.ReadLine();

Without allocating extra space
            //Reverse words in string
            var input = "ayman ali";
             input = input + " ";
            var arr = input.ToCharArray();
            //Loop - Pointer (p) is index - Length is length of array
            //If space found - e = p - 1, begin (b) saved from before
            //Reverse string in place
                //Loop half length of string
                //i to m (Mirror) m = e-i

            //Reverse whole string

            var b = 0;
            var e = 0;
            for (int p = 0; p < arr.Length ; p++)
            {
                if (arr[p] == ' ')
                {
                    e = p - 1;
                    for (int i = b; i < (b+ (e-b) /2); i++)
                    {
                        var temp = arr[i];
                        var m = e - (i - b);
                        arr[i] = arr[m];
                        arr[m] = temp;
                    }
                    b = p + 1;

                }
            }
            //arr need to be revsrsed
            e = arr.Length-1;
            for (int p = 0; p < e/2; p++)
            {
                var temp = arr[p];
                var m = e - p - 1;
                arr[p] = arr[m];
                arr[m] = temp;
            }

            Console.WriteLine(string.Join(string.Empty, arr.Take(e)));
         
            Console.ReadLine();

Monday, October 30, 2017

Reverse Word Characters in Place

Given a string, Reverse every word in the string in place. For example if you have Ayman Ali, running the algorithm should result in namyA ilA.

Solutions
            //Reverse words in string
            var input = "ayman ali";
            input += " ";
            //Convert into Array
            var arr = input.ToCharArray();
            //Loop Through the array with the length of the array
            var b = 0; //Pointer to an actual element
            var e = 0;
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i] == ' ') //Space reached
                {

                    e = i - 1;
                    //Rotate in place
                    for (int p = b; p < e - ((e-b) / 2); p++)
                    {
                        var temp = arr[p];
                        var m = e - (p - b);
                        arr[p] = arr[m];
                        arr[m] = temp;
                    }
                    b = e + 2;
                }
            }
            //If first element note b = Begin of new word
            //IF space or end of string
            //      Record the position as end of word (e)
            //      Rotate the word beginning with b and ending with e
            //      let b = e + 2
            //Else 
            //      Continue
            //End Loop


            //Rotate a word
            //Loop through the word (Length half the word) begin with b and end with e
            //Save Pointer element temporarily
            //Save mirror position (m) into pointer position (p) - Define mirror position 
            //save p into m

            Console.WriteLine(string.Join("",arr.Take(arr.Length-1)));

           //Console.WriteLine(string.Join(",", out));

            Console.ReadLine();