Sunday, May 15, 2016

#codingexercise
Rotate a 2D array without using extra space for an array
void Rotate(ref int[,] A, int r, int c)
{
//Choose one square
//    Start with four corners    1   2
                                              4   3
//        swap one diagonal     4 with 2
//            swap horizontally  1 with 2, 4 with 3
//    Repeat for next four elements at the next position in clockwise
//Repeat for inner square
for (int k = 0; k < c / 2; k++){ // to progress to inner square corners
    int 1x = 0+k, 1y=0+k;
    int 2x = 0+k, 2y = c-1-k;
    int 3x = r-1-k, 3y = c-1-k;
    int 4x = r-1-k, 4y = 0+k;
    for (int m = 0; m < c-1; m++) // to progress along each side
   {
         Swap(ref A, 4x-m,4y,2x+m,2y);
         Swap(ref A, 1x,1y+m, 2x+m, 2y);
         Swap(ref A, 4x-m,4y,3x,3y-m);
   }
}
}

Find the minimum number of characters required to form a palindrome
int MakePalindrome(string A, int center)
{
int len = GetPalindromeLength(A, center);
if (len = A.length) return 0;
int min = INT_MAX;
for (int I = center-1; I  > 0; I--)
{
    len = GetPalindromeLength(A, I);
    if (A.length -len < min)
        min = a.length - len;
}
return min;
}

Fast modular exponentiation (Pow(A,B) % C)
{
int n = hcf(A%C, C);
for (int  i = 1; i < INT_MAX; i++)
      if (n *i > A)
           return A - n * i;
return A%C;
}

# better fast modular exponentiation
represent B in Binary and from right for the positions of the 1s, split B as  sum of 2^position
then take modulo of each component and take the product of the results.

Minimum Number following a pattern
DDIDDIID
321654798
//untested
int GetMinNumberFromPattern(string A)
{
int max = 1;
var number = new List<int>();
for (int I =0;i < a.Length; I++)
{
switch(A[I])
{
  case 'I':
      if (I==0) {ret.Add(last_incr);continue;}
           int start = I+1;
           int end = I+1;
           for (int k = start; k < A.Length && A[k] == 'D'; k++)
                 end++;
           ret.Add(max + end - start + 1);
           max = max + end - start + 1;
         break;
  case 'D':
      if (max > 1) {max--; ret.Add(max); continue;}
           int start = I;
           int end = I;
           for (int k = start; k < A.Length && A[k] == 'D'; k++)
                 end++;
           ret.Add(max + end - start);
           max = max + end - start;
  default:
     throw;
}
}
return ret.ToNumber();
}
int gcd(int x, int y)
{
if (y == 0) return x;
return gcd(y, x%y);
}

Count ways to reach the n’th stair
int f(int n)
{
if ( n <=1) return 1;
return f(n-1)  + f(n-2);
}

#autocorrect spelling implementation
First form a dictionary of all known words
Second train a model on the extracted words from a test. The words form the features. This could be a word count table or a radix tree aka trie.
Third, form a set of  modified words with insertions, deletions, substitutions of the given word.
Optionally form a set of known words from the modified words of step 3 and add it to the set generated.
return the max of the words from the set with the dictionary words as lookup keys.

No comments:

Post a Comment