Implement a function that takes a string of four digits as its argument, with each digit from 1 ──► 9 (inclusive) with repetitions allowed, and returns an arithmetic expression that evaluates to the number 24. If no such solution exists, return "no solution exists."
Rules:
Only the following operators/functions are allowed: multiplication, division, addition, subtraction Division should use floating point or rational arithmetic, etc, to preserve remainders. Forming multiple digit numbers from the supplied digits is disallowed. (So an answer of 12+12 when given 1, 2, 2, and 1 is wrong). The order of the digits when given does not have to be preserved.Example inputs:
solve24("4878");
solve24("1234");
solve24("6789");
solve24("1127");
Example outputs (strings):
(7-8/8)*4
3*1*4*2
(6*8)/(9-7)
(1+7)*(2+1)
solve24
is a function.
testString: assert(typeof solve24 === 'function', 'solve24
is a function.');
- text: solve24("4878")
should return (7-8/8)*4
or 4*(7-8/8)
testString: assert(include(answers[0], solve24(testCases[0])), 'solve24("4878")
should return (7-8/8)*4
or 4*(7-8/8)
');
- text: solve24("1234")
should return any arrangement of 1*2*3*4
testString: assert(include(answers[1], solve24(testCases[1])), 'solve24("1234")
should return any arrangement of 1*2*3*4
');
- text: solve24("6789")
should return (6*8)/(9-7)
or (8*6)/(9-7)
testString: assert(include(answers[2], solve24(testCases[2])), 'solve24("6789")
should return (6*8)/(9-7)
or (8*6)/(9-7)
');
- text: solve24("1127")
should return a permutation of (1+7)*(1*2)
testString: assert(include(answers[3], solve24(testCases[3])), 'solve24("1127")
should return a permutation of (1+7)*(1*2)
');
```