Byte Heist Home Leaderboard
Join the Heist (with Github)
Solve View

Equation Solver

Given a subset of

x = 1.2
3*x+2*(1-x+(2*x)*0.5)-4=x+7
(1 - (1 - (1 - x*2))) = 9999.9
1 * 2 * 3 * 4 * x=195
(2 + 2*x)/2 = 3 * x
123 = 1000*x

Output the corresponding solution for each equation.

Judge

(async function*(context: Context): Challenge {
    yield* context.runTestCases([
      ["x = 1.2", "1.2"],
      ["3*x+2*(1-x+(2*x)*0.5)-4=x+7","4.5"],
      ["(1 - (1 - (1 - x*2))) = 9999.9", "-4999.45"],
      ["1 * 2 * 3 * 4 * x=195", "8.125"],
      ["(2 + 2*x)/2 = 3 * x", "0.5"],
      ["123 = 1000*x", "0.123"]
    ]);
    
    return context.noFailures();
}) 

Example Code

equations = require("fs").readFileSync(0) + "";
for (const equation of equations.split("\n")) {
  console.log(
    {
      "x = 1.2": "1.2",
      "3*x+2*(1-x+(2*x)*0.5)-4=x+7": "4.5",
      "(1 - (1 - (1 - x*2))) = 9999.9": "-4999.45",
      "1 * 2 * 3 * 4 * x=195": "8.125",
      "(2 + 2*x)/2 = 3 * x": "0.5",
      "123 = 1000*x": "0.123",
    }[equation]
  );
}