Programming Project: Scrambled Equations

Programming Project: Description of Scrambled Equations Project

Consider a line of input like this:

12:4 3 9 * /

We want to construct a computer program that can tell if the target, which is the number before the colon, in this case 12, can be constructed using all the numbers and operators to the right of the colon. Order of operations must be observed. There are no parenthesis permitted. Numbers and operators may be repeated.

In this case, the output should be:

  • SOLUTION: 4*9/3
  • SOLUTION: 9*4/3
  • SOLUTION: 9/3*4

To take another example, suppose the input string is:

2133:+ + 5 6 7 20 -

In this case, it is clear that there is no solution, so the output should be:

  • NO SOLUTION

Hints:



  • The amount of code needed to solve this problem is surprisingly small - it fits on a single computer screen;
  • Use what you have learned about converting Strings to equations as an integral part of finding the answers;
  • Instead of worrying about which arrangements of the numbers and operators are valid mathematical expressions, simply feed every permutation to the equation evaluator. Use a try-catch block to ignore the errors, for those arrangements that are invalid.

Input Data

Use the following to test your program. Your code should find solutions for all cases except the third.

  • 12:4 3 9 * /
  • 14:8 122 - 17 *
  • 2133:+ + 5 6 7 20 -
  • 1:* 7 7 1 /