-
-
Save kinglozzer/a30caf64e472d149c498 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Work out result of sum, without using eval or regular expressions | |
// Code should parse/tokenize the input string, and return the result. | |
// Simple functions such as str_replace(), strpos(), substr() can be used | |
// NO external libraries may be used | |
/** | |
* Return answer, operator precedence ( / * - + ) must be retained. | |
* @param String $sum Input | |
* @return String The result of the sum | |
*/ | |
function calc($sum) { | |
} | |
header('Content-type:text/plain'); | |
// Should print 100 | |
print calc("10*10")."\n"; | |
// Should print 19 | |
print calc("20+-1")."\n"; | |
// Should print 3 | |
print calc("3*3/3")."\n"; | |
// Should print -1180 | |
print calc("-100*23/2+-30")."\n"; | |
// Should print 8 | |
print calc("2+3*2")."\n"; | |
// Should print 6 | |
print calc("2+2+2")."\n"; | |
/** Bonus points :) **/ | |
// Should print 25 | |
print calc("5*(2+3)")."\n"; | |
// Should print 2 | |
print calc("((10/5)+(4/2))/(1+1+1-1)")."\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment