+ 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

22nd Aug 2017, 10:14 AM
Julian Fechner
Julian Fechner - avatar
24 ответов
+ 9
https://code.sololearn.com/cDfv9X32nXE6/?ref=app
22nd Aug 2017, 11:11 AM
Wen Qin
Wen Qin - avatar
+ 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
22nd Aug 2017, 10:47 AM
Sivan Tal
Sivan Tal - avatar
+ 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.
22nd Aug 2017, 7:01 PM
janKalupana
janKalupana - avatar
+ 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!!!
22nd Aug 2017, 10:19 AM
Julian Fechner
Julian Fechner - avatar
22nd Aug 2017, 10:36 AM
Izi Lior Katie
Izi Lior Katie - avatar
+ 1
@Sivan Tal i don't want you to use normal operator precendence... and i excluded '/' cause there are only positive INTEGERS allowed ;)
22nd Aug 2017, 11:05 AM
Julian Fechner
Julian Fechner - avatar
+ 1
Does it count if we make it the short way? Took me 3 minutes😂 https://code.sololearn.com/c9nlHDNot3C0/?ref=app
22nd Aug 2017, 11:44 AM
Tim Thuma
Tim Thuma - avatar
+ 1
https://code.sololearn.com/WbgKNb9ZTFSE/?ref=app
22nd Aug 2017, 2:41 PM
ysraelcon
ysraelcon - avatar
+ 1
I did it in Java. I welcome your suggestions and modifications. https://code.sololearn.com/c37z7l7ed08L/?ref=app
22nd Aug 2017, 5:06 PM
kamakshi
kamakshi - avatar
23rd Aug 2017, 9:40 AM
Julian Fechner
Julian Fechner - avatar
0
@Julian you have an error in your example (assuming the regular operator precedence rules). 1+45-10+2 equals 38, not 34.
22nd Aug 2017, 10:40 AM
Sivan Tal
Sivan Tal - avatar
0
Well, I posted a solution that supports division. The input may contain only integers but the tesults may be a float.
22nd Aug 2017, 11:11 AM
Sivan Tal
Sivan Tal - avatar
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.
22nd Aug 2017, 11:56 AM
Sivan Tal
Sivan Tal - avatar
0
@Sivan Tal I made this just for a little fun, not to give totally correct answer for challenge.
22nd Aug 2017, 11:58 AM
Tim Thuma
Tim Thuma - avatar
0
Tim, for the fun, try the following inputs to your program: sum i i**2 :-)
22nd Aug 2017, 12:00 PM
Sivan Tal
Sivan Tal - avatar
0
@Sayantan - try the following inputs to your program: sum x x**2
22nd Aug 2017, 12:30 PM
Sivan Tal
Sivan Tal - avatar
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
23rd Aug 2017, 4:44 AM
sayan chandra
sayan chandra - avatar
0
Short one, easy to maintain and to add new functions. https://code.sololearn.com/c7EdK2f9y1Fq/?ref=app
23rd Aug 2017, 9:18 AM
VcC
VcC - avatar
23rd Aug 2017, 10:34 AM
Julian Fechner
Julian Fechner - avatar
26th Aug 2017, 1:52 PM
Julian Fechner
Julian Fechner - avatar