Idea from http://blog.stevenlevithan.com/archives/algebra-with-regexes. The function returns only one of the solutions, and returns an empty string if there are no solutions. I had never thought that RegExp is so powerful...
-
-
Save tsaniel/1099617 to your computer and use it in GitHub Desktop.
solveAlgebraicEquation (using RegExp)
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
function( | |
a, // the set of variables | |
b, // the sum | |
c, // placeholder | |
d, // placeholder | |
e // placeholder | |
){ | |
c = '^'; | |
for (d = 0; e = a[d++];) | |
c += '(,*)\\' + d + '{' + ~-e + '}' // part of the RegExp pattern. | |
return ( | |
( | |
Array(-~b) + '' // let b plus one and coerce the array into a string that consists of commas. | |
).match(c + '$') | |
|| | |
'' // if no matches, go to an empty string. | |
).slice(1) // shift the matched whole substring or do nothing when no matches. | |
} |
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
function(a,b,c,d,e){c="^";for(d=0;e=a[d++];)c+="(,*)\\"+d+"{"+~-e+"}";return((Array(-~b)+"").match(c+"$")||"").slice(1)} |
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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
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
{ | |
"name": "solveAlgebraicEquation", | |
"description": "Solve algebraic equation with RegExp", | |
"keywords": [ | |
"solveAlgebraicEquation", | |
"algebraic equation", | |
"RegExp" | |
] | |
} |
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
<!DOCTYPE html> | |
<title>solveAlgebraicEquation</title> | |
<div>Output: <b id="ret"></b></div> | |
<script> | |
var myFunction = function(a,b,c,d,e){c="^";for(d=0;e=a[d++];)c+="(,*)\\"+d+"{"+~-e+"}";return((Array(-~b)+"").match(c+"$")||"").slice(1)}; | |
// The input should be ([a, b, c, ...], sum) | |
// e.g. Solve 11x + 2y + 5z = 115 | |
var solution = myFunction([11, 2, 5], 115); | |
document.getElementById( "ret" ).innerHTML = 'One of the solutions of 11x + 2y + 5z = 115 is x = ' + | |
solution[0].length + ', y = ' + | |
solution[1].length + ', z = ' + | |
solution[2].length; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can leave out the "RegExp", since the arguments of match get coerced to RegExp anyway. So it is:
function(a,b,c,d,e){c="^";for(d=0;e=a[d++];)c+="(,*)\\"+d+"{"+~-e+"}";return((Array(-~b)+"").match(c+"$")||"").slice(1)};