Some coding question and answers:
Void Permute (String a, StringBuilder b, bool[] used)
{
If ( b.Length == a.Length) { print b.ToString(); return;}
For (int I = 0; I < a.Length ; i++)
{
If (used[i]) continue;
used[i] = true;
B += A[i];
Permute(a, b, used);
B [B.Length – 1] = ‘/0’;
used[i] = false;
}
}
Combinations of a string
Void Combine (String a, StringBuilder b, int start, int level)
{
For (int I = start ; I < a.Length; i++)
{
If (b.Length == a.Length) print b;
b[level] = a[i];
Print b.ToString();
If (I < a.Length)
Combine(a,b, start+1,level+1)
B[level] = ‘/0’;
}
}
Void ReverseAlternateChunks InSinglyLinkedList(Node root, int chunksize)
{
Node ret = root;
Node prev = NULL;
Node cur = root;
Bool reverse = false;
Int chunkCount = 0;
While (cur != null)
{
{
If (b.Length == a.Length) print b;
b[level] = a[i];
Print b.ToString();
If (I < a.Length)
Combine(a,b, start+1,level+1)
B[level] = ‘/0’;
}
}
Void ReverseAlternateChunks InSinglyLinkedList(Node root, int chunksize)
{
Node ret = root;
Node prev = NULL;
Node cur = root;
Bool reverse = false;
Int chunkCount = 0;
While (cur != null)
{
If (reverse)
{
cur = Reverse(cur, chunkSize);
If (prev)
Prev.next = cur;
Else
Ret = cur;
Reverse = false;
}
For (int i = 0; I < chunkSize; i++)
{
Prev = cur;
Cur = cur.next;
If ( cur == NULL) break;
}
chunkCount++;
If (chunkCount %2 ==0) { reverse = true}
}
Return ret;
}
{
cur = Reverse(cur, chunkSize);
If (prev)
Prev.next = cur;
Else
Ret = cur;
Reverse = false;
}
For (int i = 0; I < chunkSize; i++)
{
Prev = cur;
Cur = cur.next;
If ( cur == NULL) break;
}
chunkCount++;
If (chunkCount %2 ==0) { reverse = true}
}
Return ret;
}
GetZigZagBinaryTree(Node root) // ZigZag is the same level wise printing of nodes except alternate levels of Node(s) are reversed
{
if (root == NULL) return;
var Q = new Queue();
Q.Enqueue(root);
Q.Dequeue(NULL);
int level = 0;
var S = new Stack();
while(Q.Empty() == false)
{
var cur = Q.Dequeue();
if (cur)
{
if (level %2 == 0) {S.Push(cur);}
else
{
while (S.empty() == false) print S.pop().data;
print cur.data;
}
if (cur.left) Q.Enqueue(cur.left);
if (cur.right)Q.Enqueue(cur.right);
}
else
{
Q.Enqueue(NULL);
level ++
}
}
while (S.empty() == false) print S.pop().data;
}
#codingexercise
Double GetNthRootProductEachTermRaisedPTimesQ (Double [] A,Double p, Double q)
{
If ( A== null) return 0;
Return A.NthRootProductEachTermRaisedPTimesQ(p, q);
}
No comments:
Post a Comment