+ 18

Order of operations [Challange]

Using only addition,subtraction and multiplication perform the following. User input> 5 - 2 * 2 + 4 * 5 System outputs > step1> 2 * 2 = 4 step2> 4 * 5 = 20 step3> 5 - 4 = 1 step4> 1 + 20 = 21 Challange by D_Stark

26th Jan 2018, 5:05 PM
D_Stark
D_Stark - avatar
12 Answers
30th Jan 2018, 8:49 AM
Sachin
Sachin - avatar
+ 9
very interesting and hard challenge 😁
26th Jan 2018, 5:29 PM
Vukan
Vukan - avatar
+ 6
@Vukan i havent tried it yet 😄 but i was trying to think up a challange for my self to use format(), binary, bitwise and regex so i come up with this but i might not need all that 😜 i think if i can see whats on either side of the operator it shouldent be too bad.... says me lol 😀😀
26th Jan 2018, 5:45 PM
D_Stark
D_Stark - avatar
+ 6
use the concept of stack first convert the give expression into postfix or prefix expression and then apply the evaluation of postfix or prefix expression
26th Jan 2018, 6:18 PM
Rstar
Rstar - avatar
+ 6
Anyone have any luck with this?
26th Jan 2018, 11:38 PM
D_Stark
D_Stark - avatar
+ 6
here's my code again, bugs fixed and, ui changed, please report any bugs. thanks for the challenge, gave me a good RegEx exercise. https://code.sololearn.com/WE8xzWCz5553/?ref=app
5th Feb 2018, 3:56 AM
raz
raz - avatar
+ 5
Nice try @Gordie But try to do some changes and correct ur code. As mentioned in comment is not working properly for input : 12 + 3 * 2 / 6
28th Jan 2018, 8:02 AM
Ajay Gaikwad
Ajay Gaikwad - avatar
+ 2
more operations and brackets would be added to the task.
6th Jan 2020, 8:47 PM
Michail Getmanskiy
Michail Getmanskiy - avatar
+ 1
27th Jan 2018, 4:33 PM
Tushar Sharma
Tushar Sharma - avatar
+ 1
Hello all and Michail Getmanskiy, D_Stark , I have attached my code here please check it out. Some notes related to my code. 1. It will work for basic operations like +, -, *, / and %. It also processes parenthesis. 2. I have used the concept like expression tree. But I haven't used any data structures to find out the steps. It's pure logic used to find the next step. Sample test case which is given : Input : 5 -2 * 2 + 4 * 5 Output : Step 1 : 2 * 2 = 4 Step 2 : 5 - 4 = 1 Step 3 : 4 * 5 = 20 Step 4 : 1 + 20 = 21 Final result : 21 Explanation: Steps Remaining Exp. 1: 2 * 2 = 4 5 - 4 + 4 * 5 2: 5 - 4 = 1 1 + 4 * 5 3: 4 * 5 = 20 1 + 20 4: 1 + 20 = 21 21 In solution version v2 I will try to get the ans like given in question. https://code.sololearn.com/cFI79zCiU4dL/?ref=app
9th Jan 2020, 2:04 PM
Ajay Gaikwad
Ajay Gaikwad - avatar
0
Hi! System convert this expression into postfix expression after that solve it
29th Jan 2018, 2:03 AM
Naresh Saini
Naresh Saini - avatar