Skip to content

Instantly share code, notes, and snippets.

@pbrandiezs
Created March 30, 2022 20:14
Show Gist options
  • Save pbrandiezs/f6f9d6f55324474068bacd12fde2063f to your computer and use it in GitHub Desktop.
Save pbrandiezs/f6f9d6f55324474068bacd12fde2063f to your computer and use it in GitHub Desktop.
Math Challenge
Math Challenge
Have the function MathChallenge(num) read the num parameter being passed which will be a combination of 1 or more single digits, and determine if it's possible to separate the digits with either a plus or minus sign to get the final expression to equal zero. For example: if num is 35132 then it's possible to separate the digits the following way, 3 - 5 + 1 + 3 - 2, and this expression equals zero. Your program should return a string of the signs you used, so for this example your program should return -++-. If it's not possible to get the digit expression to equal zero, return the string not possible.
If there are multiple ways to get the final expression to equal zero, choose the one that contains more minus characters. For example: if num is 26712 your program should return -+-- and not +-+-.
Examples
Input: 199
Output: not possibleInput: 26712
Output: -+--
function MathChallenge(num) {
// code goes here
var numstr;
var numdigits = [];
var operatorsNeeded;
var combinations;
var combinationsList = [];
var evalString = "";
var operatorString = "";
var bestMatchStr = "not possible";
// Convert num to string
numstr = num.toString();
// Parse the string to get the individual digits into an array
numdigits = numstr.split('');
// Convert each number char into a digit
for ( let i=0; i < (numdigits.length); i++) {
numdigits[i] = parseInt(numdigits[i]);
}
// Try combinations of + and - to get them to add to 0
// Build an operator combinations array for a list of each +/- to try
// Get the number of operators needed, and the number of combinations.
operatorsNeeded = numdigits.length-1;
combinations = 2 ** operatorsNeeded;
// Generate a binary list of combinations.
for (i=0; i < combinations; i++) {
binary = (i >>> 0).toString(2).padStart(operatorsNeeded,'0');
combinationsList.push(binary);
}
// Calculate for each combination
for (i=0; i < combinations; i++) {
tryThisCombination = combinationsList[i];
tryThisCombinationOperators = tryThisCombination.split('');
// Generate the evalString
for (pointer=0; pointer < numdigits.length-1; pointer++) {
evalString = evalString + numdigits[pointer].toString();
if (tryThisCombination[pointer] == '0') {
operatorString = operatorString + '-';
evalString=evalString + ' - ';
} else {
operatorString = operatorString + '+';
evalString=evalString + ' + ';
}
}
evalString = evalString + numdigits[pointer].toString();
// Check for a match where sum equal to 0, keep track of the
// best solution with the most - characters.
if (eval(evalString) === 0 ) {
operatorStringMinusCount = operatorString.split("-").length -1;
bestMatchStringMinusCount = bestMatchStr.split("-").length - 1;
if (operatorStringMinusCount > bestMatchStringMinusCount) {
bestMatchStr = operatorString;
};
};
// Clear for next check
equation=[];
evalString="";
operatorString = "";
}
return bestMatchStr;
}
// keep this function call here
console.log(MathChallenge(readline()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment