Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Train Carriage Game Solutions

Name: Anonymous 2012-08-26 2:11

I'm looking to program a function that can check if you can "make 10" from some 4 digit number (0000 to 9999).

The function should use these rules when performing the check:
https://www.facebook.com/pages/Train-Carriage-Game/108348062559174?sk=info

Does /prog/ know if there's a simple way to perform such a check short of checking every combination individually? I obviously don't want to be writing the following:


boolean makesTen (int a, int b, int c, int d) {
    if (a + b + c + d == 10) {
        return true;
    } else if (-a + b + c + d == 10) {
        return true;
    } else if (a - b + c + d == 10) {
        return true;
    }
    ...
    ...
    } else {
        return false;
    }
}

Name: Anonymous 2012-08-27 4:50

>>18
I'm working on a solution right now in Java. It just creates every possible expression and evaluates it. Generating all possible expressions without repeating any is trickier than just generating permutations of operators and operands.

What I'm doing is taking the carriage number and finding the permutations of it. For each one of those permutations I'm then assigning a sign (+ve or -ve) to each digit in every which way I can. Now that I have permutations whose digits have signs, I then add the multiplication, division and exponentiation operators, or no operator if I want to add/subtract numbers from one another. For example, say we start off with carriage number 4832. Then we might have:

4238             (a single permutation)
+4+2-3+8         (one way of giving each digit a sign)
+4/+2^-3 +8      (assigning operators - note the space indicating no operator)

Notice that we have a problem. If we evaluate this expression using the order of operations, we can never raise a number to the power of more than a single digit at a time. For example we will never evaluate an expression like:

+4/+2^(-3 +8)

To get past this, we ignore the order of operations (assume all operators have equal precedence) and evaluate right-to-left. Similarly, to evaluate an expression equivalent to:

(+4/+2)^-3 +8

We give every operator has equal precedence and evaluate left-to-right.

About recursion: I generally shy away from recursion unless I can see an obvious benefit (or have to use it). Although it makes things simpler sometimes, it usually makes it a fucktonne trickier imo.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List