maverick
BAN USER- 0of 0 votes
AnswersGiven a mathematical expression, remove the redundant brackets from the expression.
- maverick in United States
e.g. input: (a + (b*c)) * (d * ( f * j) )
output should be: (a + b * c) *d * f * J
operations to support: +, -, /, *, ++, also ternary operators.| Report Duplicate | Flag | PURGE
Microsoft Software Engineer / Developer C++
Approach: This structure is similar to a tree. analogy: child => oldest son of the tree, next => current node's younger brother.
pseudo code:
function call: preOrderTraverse(pointer to first node i.e. head of LL)
void preOrderTraverse(curr)
{
static pointer * previousNode
if (curr != NULL)
{
if (previousNode != NULL)
{
tie previousNode with curr node by manipulating prev and next ptrs of these nodes
}
else
{
head of resultant LL is = curr
}
previousNode = curr
preOrderTraverse(curr->child)
preOrderTraverse(curr->next)
}
}
Approach: This structure is similar to a tree. analogy: child => oldest son of the tree, next => current node's younger brother.
pseudo code:
function call: preOrderTraverse(pointer to first node i.e. head of LL)
void preOrderTraverse(curr)
{
static pointer * previousNode
if (curr != NULL)
{
if (previousNode != NULL)
{
tie previousNode with curr node by manipulating prev and next ptrs of these nodes
}
else
{
head of resultant LL is = curr
}
previousNode = curr
preOrderTraverse(curr->child)
preOrderTraverse(curr->next)
}
}
#include <iostream>
#include <cstdlib>
#include <intrin.h>
#pragma intrinsic(_rotr8, _rotr16)
int main()
{
int inputInteger = 376;
unsigned int offset = 3; // bits to roate
std::cout << " Original Number: " << inputInteger << "\n";
inputInteger = _rotr16(inputInteger, offset);
//inputInteger = _rotl16(inputInteger, offset); // for left rotate
std::cout << " Rotated Number: " << inputInteger << "\n";
getchar();
return 0;
}
- maverick January 29, 2012#include <iostream>
#include <cstdlib>
int main()
{
int lower = 0; // fibo series: 0, 1, 1, 2, 3, 5 ...
int upper = 1;
int temp = 0;
// ------------------- user input goes here ----------------------------- //
int startPoint = 1; // from 1st element, assuming series starts from 0th element
int range = 4; // 5 elements including startPoint
// ---------------------------------------------------------------------- //
if(startPoint>=0 && range > 0)
{
int endPoint = startPoint + range - 1;
if(startPoint < 2)
{
while(startPoint != 2 && range != 0) // this loop executes at most 2 times
{
std::cout<< startPoint++ <<" ";
range--;
}
endPoint = startPoint + range - 1; //recalculate
}
while(endPoint>=2) // iterate
{
temp = lower;
lower = upper;
upper = lower + temp;
endPoint--;
if(endPoint <= range)
std::cout << upper << " ";
}
}
else
{
std::cout<<"invalid input!";
}
getchar();
return 0;
}
- maverick January 21, 2012
yup, since there are no duplicates; solution: before printing/storing result subsets, we can examine them to make sure they do not contain +ve and -ve entry of the same number.
- maverick March 16, 2013