+ 3
*DAILY CHALLENGE* : write a code that gets as input a mathematical string and calculate its result!
INFO: The string can contain following characters: - '0'..'9' (positive integer) - '*', '-', '+', '^' (operands) operand order: 1) solve all powers (^) first 2) solve all products (*) next 3) then solve all sums (+) 4) finally solve all differences (-) EXAMPLE: s = "1+5*3^2-10+2" 1) 3^2 = 9 ---> s = "1+5*9-10+2" 2) 5*9 = 45 ---> s = "1+45-10+2" 3) 1+45 = 46 & 10+2 = 12 ---> s = "46-12" 4) 46-12 = 34 ---> result = "34" HAVE FUN AND GOOD LUCK! https://code.sololearn.com/c5FO89c3v1FA/?ref=app
24 Réponses
+ 9
https://code.sololearn.com/cDfv9X32nXE6/?ref=app
+ 3
Here's my solution in Python.
My program solves any mathematical expression containing integers and the operators {+, -, *, /, ^}
It assumes operator precedence as follows:
1) ^
2) *, /
3) +, -
I used the normal mathematical operator precedence and didn't follow the unusual operator precedence of the challenge, but you can see in my code that it's trivial to change the precedence to be any other.
https://code.sololearn.com/cvBG8iHGc6Le
+ 2
Here's my shot: https://code.sololearn.com/czA9J52R5omw/?ref=app
Please read the first comment in the code to run this effectively in SoloLearn. Running in command line is optimal and self-explanatory.
The only downfall here that I can think of is handling negative integers. For example, my code will spit out an error if you try to put in -6+7 or so but @Julian says that's invalid for this challenge anyway.
+ 1
ADDITIONAL INFO:
- If any other character occurs in the string you can return -12345.
- If the string isn't mathematically valid:
s = "*2" OR s = "1*+2" OR s = "12-" ... (and so on)
All these aren't valid and you can return -12345.
AND DO NOT USE NORMAL OPERATOR PRECENDENCE!!!
+ 1
@Sivan Tal
i don't want you to use normal operator precendence...
and i excluded '/' cause there are only positive INTEGERS allowed ;)
+ 1
Does it count if we make it the short way?
Took me 3 minutes😂
https://code.sololearn.com/c9nlHDNot3C0/?ref=app
+ 1
https://code.sololearn.com/WbgKNb9ZTFSE/?ref=app
+ 1
I did it in Java. I welcome your suggestions and modifications.
https://code.sololearn.com/c37z7l7ed08L/?ref=app
+ 1
NEW DAILY CHALLENGE FOR YOU:
https://www.sololearn.com/discuss/649970/?ref=app
0
@Julian you have an error in your example (assuming the regular operator precedence rules).
1+45-10+2 equals 38, not 34.
0
Well, I posted a solution that supports division. The input may contain only integers but the tesults may be a float.
0
@Tim Thuma
Your solution is short and elegant, but it doesn't catch invalid expressions, it doesn't let you choose the operators allowed, and it doesn't let you control their precedence. Therefore, it's not a good solution to the problem at hand.
0
@Sivan Tal I made this just for a little fun, not to give totally correct answer for challenge.
0
Tim, for the fun, try the following inputs to your program:
sum
i
i**2
:-)
0
@Sayantan - try the following inputs to your program:
sum
x
x**2
0
@ JULIAN
HERE IS MINE
1) give input with only integers/floats and operators
2) invalid input of any type will show why and how that is invalid
3) eg: 5/6*7/8.8^2.1-5+56
eg: 2.4/5.6^3*8.1
eg: 4-5-6-7+6+6*7*8*9
4) detailed step by step calculation is included.
check it out...
https://code.sololearn.com/cbEt2Rn86C5l/?ref=app
0
Short one, easy to maintain and to add new functions. https://code.sololearn.com/c7EdK2f9y1Fq/?ref=app
0
ANOTHER MINI-CHALLENGE HERE:
https://www.sololearn.com/discuss/650079/?ref=app
0
NEW *DAILY CHALLENGE* HERE:
https://www.sololearn.com/discuss/659673/?ref=app