DEV Community

Day 3: From string to math

Xavier Chretien on January 08, 2020

Details Difficulty: Hard Programming language: Kotlin Problem Giving a string consisting of parentheses, single digits, po...
Collapse
 
nickholmesde profile image
Nick Holmes

Nice solution to the problem, but you can simplify. Addition is associative, e.g.

a+(b+c) = (a+b)+c = a+b+c

(Subtraction is trivially changed to addition 2 - 1 = 2 + -1. I see you have understood this).

As the parenthesis don't do anything, you can just ignore them. (I would set up a range a characters to ignore, and add whitespace to it as well)

I've got to go to a meeting now, but I'll post an F# version later - might be able to get it down to a single expression.

Collapse
 
apomalyn profile image
Xavier Chretien

Oh! Really good idea, thank you 😃 I will try this. I don't have really looked into the different types of class there is in Kotlin. Thank you !

Collapse
 
nickholmesde profile image
Nick Holmes

Indeed. :-(