Which of the following postfix notations correctly represents infix sum 1+2+3+4? - algorithm

I am testing an infix-to-postfix-to-infix converter and found some kind of uncertainty. For example, a simple infix sum
1 + 2 + 3 + 4
can be converted to postfix one
1 2 + 3 + 4 +
assuming that operators with equal precedence are not accumulated. If they are then I get
1 2 3 4 + + +
On the other hand, all the following postfix expressions can be converted to the initial sum
1 2 + 3 + 4 +
1 2 + 3 4 + +
1 2 3 4 + + +
Are all these postfix expressions correct?
UPDATE1
If you would make such converter, to which form would you choose? I need to choose one for testing.

You need to define an extra constraint.
Mathematically, your postfix expressions are all the same. But on a computer integer addition is not really commutative because of overflow.
Replace 1 2 3 4 with a b c d and consider the possibility of overflow. Most programming languages define that a + b + c + d must be evaluated left-to-right so that a b + c + d + is the only correct translation.
Only when you define that the order of evaluation is 'unspecified' all the postfix versions are equivalent. That was the case for (older) C Compilers.

Yep, all correct. They correspond to the following bracketed infix expressions:
((1 + 2) + 3) + 4
(1 + 2) + (3 + 4)
1 + (2 + (3 + 4))

+ is confusing - it is commutative, so in fact, every result seems correct.
Consider replacing + with other operators: 1 a 2 b 3 c 4.
The correct result here, for left-associative operators, is
1 2 a 3 b 4 c
So, in your case, I'd expect 1 2 + 3 + 4 +

Related

Stack And Arithmetic Evaluation

A stack is said to be the ideal data structure for Arithmetic Evaluation. Why is it so?
Why do we even need a data structure for Arithmetic Evaluation? I've been studying about this for some time now and still confused. I don't understand what is the use of Prefix and Postfix expressions because the Infix expression is quite readable.
Answer for the part of why postfix/prefix over infix is quite well explained here.As a summary infix is readable but not easily parsed
As for why stack is used here is:
1: push,pop in O(1) time are quite useful for evaluation.
2: push: add the operand on stack.
3: pop: remove the operand and evaluate expression(binary)
4: final result is the only one left on stack after parsing
The infix expression is readable, yes. But if you want to write an algorithm that can evaluate an arithmetic expression, how would you do?
Take the following expression:
3 + 4 * 5 + 2 ^ 3 * 12 + 6
How does your algorithm proceed from there?
A simple and naive way is to look for the highest-precedence operation, evaluate it, then rewrite the string, and keep doing that until all operations have been performed. You'd get this result:
3 + 4 * 5 + 2 ^ 3 * 12 + 6
3 + 4 * 5 + 8 * 12 + 6
3 + 20 + 96 + 6
23 + 102
125
That is one way to do it. But not a particularly efficient way. Looking through the string for the highest-precedence operation takes time linear in the length of the string, and you have to do that once per operation, and rewrite the string every time. You end up with something like a quadratic complexity. There might be a few tricks to be slightly more efficient, but it's not going to be as efficient as other existing methods.
Another possible method is to put the expression into a tree, called a "syntax tree" or "abstract syntax tree". We get this:
+
/ / \ \
3 * * 6
/ \ / \
4 5 ^ 12
/ \
2 3
This tree is easier to evaluate for an algorithm, compared to the expression we had before: it is a linked structure, in which you can easily replace one branch by the value of that branch without having to rewrite everything else in the tree. So you replace 2^3 with 8 in the tree, then 8 * 12 with 96, etc.
Postfix (or prefix) notation is harder to read for humans, but much easier to manipulate for an algorithm. My previous example becomes this in postfix:
3 4 5 * + 2 3 ^ 12 * + 6 +
This can be evaluated easily reading it from left to right; every time you encounter a number, push it onto a stack; every time you encounter an operator, pop the two numbers on top of the stack, perform the operation, and push the result.
Assuming the postfix expression was correct, there should be a single number in the stack at the end of the evaluation.
EXPR | [3] 4 5 * + 2 3 ^ 12 * + 6 +
STACK | 3
EXPR | 3 [4] 5 * + 2 3 ^ 12 * + 6 +
STACK | 3 4
EXPR | 3 4 [5] * + 2 3 ^ 12 * + 6 +
STACK | 3 4 5
EXPR | 3 4 5 [*] + 2 3 ^ 12 * + 6 +
STACK | 3 20
EXPR | 3 4 5 * [+] 2 3 ^ 12 * + 6 +
STACK | 23
EXPR | 3 4 5 * + [2] 3 ^ 12 * + 6 +
STACK | 23 2
EXPR | 3 4 5 * + 2 [3] ^ 12 * + 6 +
STACK | 23 2 3
EXPR | 3 4 5 * + 2 3 [^] 12 * + 6 +
STACK | 23 8
EXPR | 3 4 5 * + 2 3 ^ [12] * + 6 +
STACK | 23 8 12
EXPR | 3 4 5 * + 2 3 ^ 12 [*] + 6 +
STACK | 23 96
EXPR | 3 4 5 * + 2 3 ^ 12 * [+] 6 +
STACK | 119
EXPR | 3 4 5 * + 2 3 ^ 12 * + [6] +
STACK | 119 6
EXPR | 3 4 5 * + 2 3 ^ 12 * + 6 [+]
STACK | 125
And there we have the result. We only had to read through the expression once. Thus the execution time is linear. This is much better than the quadratic execution time we had when trying to evaluate the infix expression directly and had to read through it several time, looking for the next operation to perform.
Note that converting from infix to postfix can also be done in linear time, using the so-called Shunting Yard algorithm, which uses two stacks. Stacks are awesome!

Convert n into negative base form in Swift 3

Okay, so I'm fully well aware of how this is applied in C# and C. But I can't figure it out in Swift 3.
Is there an algorithm to convert n into negabinary base (-2).
7 = -3*-2 + 1 (least significant digit)
-3 = 2*-2 + 1
2 = -1*-2 + 0
-1 = 1*-2 + 1
1 = 0*-2 + 1 (most significant digit)

Finding number representation in different bases

I was recently solving a problem when I encountered this one: APAC Round E Q2
Basically the question asks to find the smallest base (>1) in which if the number (input) is written then the number would only consist of 1s. Like 3 if represented in base 2 would become 1 (consisting of only 1s).
Now, I tried to solve this the brute force way trying out all bases from 2 till the number to find such a base. But the constraints required a more efficient one.
Can anyone provide some help on how to approach this?
Here is one suggestion: A number x that can be represented as all 1s in a base b can be written as x = b^n + b^(n-1) + b^(n-2) + ... + b^1 + 1
If you subtract 1 from this number you end up with a number divisble by b:
b^n + b^(n-1) + b^(n-2) + ... + b^1 which has the representation 111...110. Dividing by b means shifting it right once so the resulting number is now b^(n-1) + b^(n-2) + ... + b^1 or 111...111 with one digit less than before. Now you can repeat the process until you reach 0.
For example 13 which is 111 in base 3:
13 - 1 = 12 --> 110
12 / 3 = 4 --> 11
4 - 1 = 3 --> 10
3 / 3 = 1 --> 1
1 - 1 = 0 --> 0
Done => 13 can be represented as all 1s in base 3
So in order to check if a given number can be written with all 1s in a base b you can check if that number is divisble by b after subtracting 1. If not you can immediately start with the next base.
This is also pretty brute-forcey but it doesn't do any base conversions, only one subtraction, one divisions and one mod operation per iteration.
We can solve this in O( (log2 n)^2 ) complexity by recognizing that the highest power attainable in the sequence would correspond with the smallest base, 2, and using the formula for geometric sum:
1 + r + r^2 + r^3 ... + r^(n-1) = (1 - r^n) / (1 - r)
Renaming the variables, we get:
n = (1 - base^power) / (1 - base)
Now we only need to check power's from (floor(log2 n) + 1) down to 2, and for each given power, use a binary search for the base. For example:
n = 13:
p = floor(log2 13) + 1 = 4:
Binary search for base:
(1 - 13^4) / (1 - 13) = 2380
...
No match for power = 4.
Try power = 3:
(1 - 13^3) / (1 - 13) = 183
(1 - 6^3) / (1 - 6) = 43
(1 - 3^3) / (1 - 3) = 13 # match
For n around 10^18 we may need up to (floor(log2 (10^18)) + 1)^2 = 3600 iterations.

converting Infix to prefix conversion

Recently I have went through some sites,
for converting the Infix to Prefix notation and finally got tucked up.
I have given the steps which I have done..
Ex:- (1+(2*3)) + (5*6) + (7/8)
Method 1:- (Manual Conversion without any algorithm):-
Step1: (1+(*23)) + (*56) + (/78)
Step2: (+1*23) + (*56) + (/78)
Step3: +(+1*23)(*56) + (/78)
Step4: +[+(+1*23)(*56)](/78)
Step5: +++1*23*56/78 **--- Final Ans -----**
Method 2:-
As per the site http://scanftree.com/Data_Structure/infix-to-prefix
Step1: Reverse it:-
) 8 / 7 ( + ) 6 * 5 ( + ) ) 3 * 2 ( + 1 (
Step2: Replace the '(' by ')' and vice versa:
( 8 / 7 ) + ( 6 * 5 ) + ( ( 3 * 2 ) + 1 )
Step3: Convert the expression to postfix form:-
8 7 / 6 5 * + 3 2 * 1 + +
Step4: Reverse it
+ + 1 * 2 3 + * 5 6 / 7 8 --- Final Ans -----
So, here I got totally hanged.
Could any one please provide some light on following things:-
On where I went wrong in the above 2 methods
Which is the right answer
so I can able to understand the concept more better.
Your method is not correct, look at the comment, it says that ( a + b ) + c = a + ( b + c ) . What about (a + b) * c? (a + b) * c != a + (b * c).
According to your manual algorithm, you put the last + is placed to the front. It is not correct. If you use * instead of last + , where did you put it ? Just think about that, then you can easily find the problem with your algorithm.
Another algorithm for solving this problem is just parenthesis it before proceeding. Replace every left parenthesis with the operator inside it.
Example, ((1+(2*3)) + ((5*6) + (7/8))) then it become ++1*23+*56+/78. Your algorithm is correct if the precedence of the operator inside is same. If it is not it will fail.
NOTE : Your answer can also be obtained by the arrangement of parenthesis. (((1+(2*3)) + (5*6)) + (7/8)) then it becomes +++1*23*56/78. But if the last one is * instead of + then it doesn't work.
(b * b - 4 * a * c ) / (2 * c) EQUATION--1;
Now I will solve this mathematically by substituting different variables, and doing 2 terms at time.
=> x=bb* ; y=4a* ; z=2c*
these above are the substitution of first time, use in eq 1
( x - y * c )/(z)
again doing the substitutions with new variables.
=> i=yc* ;
(x - i)/z
=> j=xi-;
j/z
Now this here is the base case solve it then substitute back all the variables accordingly.
jz/
Now back substitution
xi- 2c* /
bb* yc * - 2c* /
bb* 4a* c * -2a*/
The Manual conversion is correct because when we reverse the infix expression to calculate the prefix, the associativity of the expression changes from left to right to right to left which is not considered in the algorithm and often it is ignored.
Example:
expression:5-3-2 :infix to prefix(manual conversion)
(-5 3)-2
-(- 5 3) 2
- - 5 3 2
now by the algorithm(if associativity not changed)
reverse expression: 2 - 3 - 5
postfix: 2 3 - 5 -
again reverse to get prefix: - 5 - 3 2
now see if we ignored the associativity, it made a huge difference
now if we change the associativity from left to right to right to left:
then :
reverse expression: 2 - 3 - 5
postfix: 2 3 5 - - (like a^b^b to postfix: abc^^ because it is also right associative)
reverse - - 5 3 2

number of ways of distribution

if we have n different things and we need to distribute them among m different people then how many ways can we do it such that for each of the m persons there is conditions that:
person 1 can have at least a things and at most b things
person 2 can have at least c things and at most d things
.. and so on ?
e.g if n = 5 and m =3 and the conditions are:
person 1 can receive at least 0 and at most 1 gift
person 2 can receive at least 1 and at most 3 gift
person 3 can receive at least 1 and at most 4 gift
then the number of ways of distributing these 5 gifts is 6((0 1 4), (0 2 3), (0 3 2), (1 1 3), (1 2 2), (1 3 1)).
One way i believe is to iterate through all possible combinations for each range and see which ones sum upto n , but can't think of an efficient algorithm.
Thanks
You probably want to use a generating function approach. Represent the number of objects that person i gets by the exponents of x. This means that if person i can have at least 3 and at most 7 things, this corresponds to the term
x^3 + x^4 + x^5 + x^6 + x^7
Remember to think of + as OR and * as AND. If we want to impose conditions and person 1 and person 2, then multiply their functions together. For example, with person 1 having between 3 and 7 things, and say person 2 has at least 5 things, and add a third person with at most 10 things. Then we get:
(x^3 + x^4 + x^5 + x^6 + x^7) * (x^5 + x^6 + ... ) * (1 + x + x^2 + ... + x^10)
which can also be written as
(x^3 + x^4 + x^5 + x^6 + x^7) * ( x^5/(1+x) ) * (1 + x + x^2 + ... + x^10)
The way to get information back from this is the following. The coefficient of x^M in the expansion of these terms gives the number of ways to distribute a total of M things among all the people subject to the given constraints.
You can work this out from the formulas, or write a program to extract the coefficient, but the idea is to use generating functions as a convenient and efficient way to encode the constraints along with the answer.

Resources