+ 5

Hello, please help me understand how to solve

Write a calculator to perform basic arithmetic, but using an alternative syntax for writing the expressions. In this alternative notation, the operators precede the operands. For example while in traditional notation we might write 3+ 4, Instead we would write + 3 4. The main advantage of this format is that it does not require parentheses for any ambiguous expression. Traditional notation 3+4 3-(4*5) (3+4)*5 (3-4)/(5+2) Alternative notation + 3 4 - 3 * 4 5 * + 3 4 5 / - 3 4 + 5 2 In the code provided, exports a calculate function. This function is expected to take an alternative expression as a string, and output the numerical solution. Please Implement the calculate function to solve expressions in the alternative format as expected.

28th Apr 2024, 12:26 AM
Lilit
Lilit - avatar
13 ответов
+ 6
Looks like Bob_Li has figured it out. But I've found a github project about this. https://github.com/aghamohammadi/prefix-calculator You can find an alternative solution here. https://github.com/aghamohammadi/prefix-calculator/blob/main/app/calculator.js It works in sololearn playground if you remove exports. from the calculate function.
28th Apr 2024, 3:40 PM
Chris Coder
Chris Coder - avatar
+ 4
Don't post the code like that, please see the post https://sololearn.com/compiler-playground/Wek0V1MyIR2r/?ref=app
28th Apr 2024, 4:38 AM
Sakshi [Offline 🙃]
Sakshi [Offline 🙃] - avatar
+ 3
Here is my code: exports.calculate = function(expression){ const token = []; const stack = []; for (let i=tokens.length-1; i>=0; i++){ if(isNaN(tokens[i])){ let result; switch(tokens[i]){ case '+': result=stack.pop()+stack.pop() break; case '-': result=stack.pop()+stack.pop() break; case '*': result=stack.pop()+stack.pop() break; case '/': result=stack.pop()+stack.pop() break; } stack.push(result); } else { stack.push(tokens[i]); } } return stack.pop[i]; }
28th Apr 2024, 4:35 AM
Lilit
Lilit - avatar
+ 3
Lilit remember to convert your string to number. maybe something like this: https://sololearn.com/compiler-playground/WRxEnDe6q7yR/?ref=app
28th Apr 2024, 12:48 PM
Bob_Li
Bob_Li - avatar
+ 2
Can you share your attempt?
28th Apr 2024, 4:18 AM
Sakshi [Offline 🙃]
Sakshi [Offline 🙃] - avatar
+ 2
This method of expression is called Polish Notation. For better understanding of the assignment, see this link: https://en.m.wikipedia.org/wiki/Polish_notation
28th Apr 2024, 8:01 AM
Brian
Brian - avatar
+ 2
Python def calculate(expression): """ This function evaluates an expression written in Reverse Polish Notation (RPN). Args: expression: A string containing the expression in RPN format. Returns: The numerical result of the expression. """ stack = [] for token in expression.split(): if token.isdigit(): stack.append(float(token)) # Handle decimals as well else: try: num2 = stack.pop() num1 = stack.pop() result = { '+': num1 + num2, '-': num1 - num2, '*': num1 * num2, '/': num1 / num2 }[token] stack.append(result) except IndexError: raise ValueError("Invalid expression: missing operands") if len(stack) != 1: raise ValueError("Invalid expression: extra operands") return stack.pop()
28th Apr 2024, 2:57 PM
Williams Shalom
+ 2
Chris Coder thanks. great link. I can see a few places where my code could be improved. Those links are really helpful.
28th Apr 2024, 10:24 PM
Bob_Li
Bob_Li - avatar
+ 1
I don’t know anything that they didn’t explain.
28th Apr 2024, 1:19 AM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 1
Yes, I see but my code doesn't work
28th Apr 2024, 1:26 AM
Lilit
Lilit - avatar
+ 1
I can’t see your code.
28th Apr 2024, 2:50 AM
Wilbur Jaywright
Wilbur Jaywright - avatar
0
Please I need to learn
28th Apr 2024, 4:45 PM
Ogbor Danny
Ogbor Danny - avatar
0
Hello guys
29th Apr 2024, 12:56 PM
Osudife John Mary
Osudife John Mary - avatar