Ruby, 24
r=eval s.gsub ?/,'**-1*'
Thank you to @ybeltukov for the Mathematica technique. I thought there might be a substitution that could be used to leverage eval
and yours was much simpler than the crazy regexes I was trying!
This shortcut effectively changes the operator precedence in s
to what the question requires. It has the side effect of sometimes resulting in a Rational
object type (e.g. 2/1
== 2
). The results are correct for the cases I've tried.