We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I also learned a pretty cool trick from a Knuth article. An old Fortran compiler dealt with precedence by inserting varying numbers of parentheses around the operations. So,
+ becomes )))+(((
- becomes )))-(((
* becomes ))*((
/ becomes ))/((
^ becomes )^(
and everything works out! (well, I also had to delete spaces and insert some extra parentheses to deal with the implied multiplication. It's also necessary to pad the beginning and end of the string with ((( and ))). )
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Simplify the Algebraic Expressions
You are viewing a single comment's thread. Return to all comments →
I also learned a pretty cool trick from a Knuth article. An old Fortran compiler dealt with precedence by inserting varying numbers of parentheses around the operations. So,
+ becomes )))+(((
- becomes )))-(((
* becomes ))*((
/ becomes ))/((
^ becomes )^(
and everything works out! (well, I also had to delete spaces and insert some extra parentheses to deal with the implied multiplication. It's also necessary to pad the beginning and end of the string with ((( and ))). )