#codingexercise
Print right justified # in stair case format
static void StairCase(int n) {
for (int i = 0; i < n; i++){
string s = "";
for (int k = 0; k < n-i; k++)
s += " ";
for (int k = 0; k < i; k++)
s += "#";
Console.WriteLine(s);
}
}
There are some glasses with equal capacity as 1 litre. The glasses are kept as follows:
1
2 3
4 5 6
7 8 9 10
You can put water to only top glass. If you put more than 1 litre water to 1st glass, water overflows and fills equally in both 2nd and 3rd glasses. Glass 5 will get water from both 2nd glass and 3rd glass and so on.
If you have X litre of water and you put that water in top glass, how much water will be contained by jth glass in ith row?
This is like Pascal's triangle where the inner values are formed from the sum of the two values above it.
Print right justified # in stair case format
static void StairCase(int n) {
for (int i = 0; i < n; i++){
string s = "";
for (int k = 0; k < n-i; k++)
s += " ";
for (int k = 0; k < i; k++)
s += "#";
Console.WriteLine(s);
}
}
There are some glasses with equal capacity as 1 litre. The glasses are kept as follows:
1
2 3
4 5 6
7 8 9 10
You can put water to only top glass. If you put more than 1 litre water to 1st glass, water overflows and fills equally in both 2nd and 3rd glasses. Glass 5 will get water from both 2nd glass and 3rd glass and so on.
If you have X litre of water and you put that water in top glass, how much water will be contained by jth glass in ith row?
This is like Pascal's triangle where the inner values are formed from the sum of the two values above it.
double GetWater( int i, int j, double X)
{
assert (j <= i);
int glasscount = i * (i+1) / 2;
var d = new double[glasscount]();
int index = 0;
glass[index] = X;
for (int row = 1; row <= i && X != 0; ++row)
{
for (int col = 0; col < row; col++, index++)
{
X = glass[index];
glass[index] = (X >= 1.0) ? 1.0 : X;
X= (X >= 1.0f) ? (X-1) : 0;
glass[index + row] += X/2;
glass[index + row + 1] += X/2;
}
}
return glass[i*(i-1)/2 + j - 1];
}
# print pascal's triangle
void PrintPascal(int n)
{
int A[n, n];
for (int row = 0; row < n; row++){
for (int col = 0; col < row; col++){
if (row == col || col == 0)
A[row,col] = 1;
else
A[row, col] = A[row-1][col-1] + A[row-1][col]
Console.Write(A[row,col]);
}
Console.WriteLine();
}
}
# print pascal's triangle
void PrintPascal(int n)
{
int A[n, n];
for (int row = 0; row < n; row++){
for (int col = 0; col < row; col++){
if (row == col || col == 0)
A[row,col] = 1;
else
A[row, col] = A[row-1][col-1] + A[row-1][col]
Console.Write(A[row,col]);
}
Console.WriteLine();
}
}
Given a binary search tree. Find two numbers in the tree whose sum is k
Node getNode(node root, int val)
{
if (root == null) return null;
if (root.val == val) return root;
if (root.val < val)
return getNode(root.right, val);
else
return getNode(root.left, val);
}
void getNodes(node root, int sum, ref Tuple<Node, Node> ret)
{
if (root == null) return;
node pair = getNode(root, sum-root.val);
if (pair != null) {ret.first = root; ret.second = pair; return;}
if (ret.first == null && ret.second == null) getNodes(root.left, sum, ref ret);
if (ret.first == null && ret.second == null) getNodes(root.right, sum, ref ret);
}
var ret = new Tuple<Node, Node> ();
ret.first = null;
ret.second = null;
getNodes(root, sum ref ret);
if (ret.first && ret.second)
Console.Write("{0}:{1}", ret.first, ret.second);
convert little indian to big indian
List<byte> ToBigIndian(List<byte> num)
{
var ret = new List<byte>();
for (int i = 0; i < num.Count; i++)
ret.Insert(num[i],0) // at initial position
return ret;
}
A topic for a talk : https://1drv.ms/w/s!Ashlm-Nw- wnWk2xYkvf4o1W0hEJk
Node getNode(node root, int val)
{
if (root == null) return null;
if (root.val == val) return root;
if (root.val < val)
return getNode(root.right, val);
else
return getNode(root.left, val);
}
void getNodes(node root, int sum, ref Tuple<Node, Node> ret)
{
if (root == null) return;
node pair = getNode(root, sum-root.val);
if (pair != null) {ret.first = root; ret.second = pair; return;}
if (ret.first == null && ret.second == null) getNodes(root.left, sum, ref ret);
if (ret.first == null && ret.second == null) getNodes(root.right, sum, ref ret);
}
var ret = new Tuple<Node, Node> ();
ret.first = null;
ret.second = null;
getNodes(root, sum ref ret);
if (ret.first && ret.second)
Console.Write("{0}:{1}", ret.first, ret.second);
convert little indian to big indian
List<byte> ToBigIndian(List<byte> num)
{
var ret = new List<byte>();
for (int i = 0; i < num.Count; i++)
ret.Insert(num[i],0) // at initial position
return ret;
}
A topic for a talk : https://1drv.ms/w/s!Ashlm-Nw-
No comments:
Post a Comment