Sunday, July 10, 2016

#codingexercise
Given n ropes of different length, combine them into a single rope,such that total cost is minimum. You can tie two ropes at a time,and cost of tying is sum of length of ropes.
Int getmin (list <int> Len)
{
Int cost = 0
Heap h = build_heap(len)
While (h.size () >1)
{
First = extract_min (h)
Second = extract_min (h)
Cost += first + second
H.insert (first+second );
}
Return cost;
}
Given a “2 x n” board and tiles of size “2 x 1″, count the number of ways to tile the given board using the 2 x 1 tiles. A tile can either be placed horizontally i.e., as a 1 x 2 tile or vertically i.e., as 2 x 1 tile.
Let the number of ways be GetCount(n)
If the tile is placed vertically, the problem reduces to GetCount(n-1)
If the tile is placed horizontally, the problem reduces to GetCount(n-2)
This is Fibonacci series
int GetCount(int n)
{
if (n <=1) return n;
return GetCount(n-1) + GetCount(n-2);
}
A car factory has two assembly lines, each with n stations.
Each station is dedicated to some sort of work. Cars may proceed from one station to other on the same line or transfer across another line. There is a cost to transfer as well as cost for entry and exit on each line in terms of minutes. Compute the minimum time it will take to build a car.

int GetCarTime(int [,] A, int [,] t, int numStations,  ref int[] entry, ref int[] exit) // A, entry and exit have two rows each and correspond to the static costs
{
int T1[numStations];
int T2[numStations];
T1[0] = entry[0] + A[0,0];
T2[0] = entry[1] + A[1,0];
for (i = 1; i < numStations; ++i)
{
T1[i] = mint(T1[i-1] + A[0,i], T2[i-1] +t[1,i] + A[0,i]);
T2[i] = min(T2[i-1] + A[1,i], T1[i-1] +t[0,i] +A[1,i]);
}
return min(T1[numStations-1] + exit[0], T2[numStations-1] + exit[1]);
}
Given an array of random numbers, Push all the zero’s of a given array to the right end of the array in minimum possible swaps. Order of appearance doesn’t matter. Print the minimum swaps needed to do so.

input : {1, 9, 8, 0, 0, -2, 0, 1, 6}.output :swaps : 2 (-2 as it is and swap 1 and 6 from first two zeros. )
Int GetMinShifts(List<int>A, int N)
{
int count = 0;
Int swaps = 0;
for (int i =0; i < N; i++)
    for ( int j = N-1; j > i; j--){
    if (A[i] == 0 && A[j] != 0){
          int temp = A[j];
          A[j] = A[i];
          A[i] = temp;
          swaps++;
         break;
     } 
}
return swaps;
}

convert big indian to little indian
List<byte> ToLittleIndian(List<byte> num)
{
var ret = new List<byte>();
for (int i = num.Count-1; i >=0; i--)
      ret.Add(num[i]) // at initial position
return ret;
}

No comments:

Post a Comment