Domanda di colloquio di Meta

write a program that solves linear equations with - + and * operators given as strings. For example the input can be: "8+5+x+3*x = 10-x+4*3*x"

Risposta di colloquio

Anonimo

18 mag 2015

Parse based on '=' first, then parse based on '+' and '-' and then parse each split based on '*', then have 4 counters: SumLeftNums, SumLeftXs, SumRightNums, SumRightXs, and calculate the answer using the following equation: (SumLeftNums-SumRightNums)/(SumRightXs-SumLeftXs), just try to solve the equation in the question by hand, you will get all the clues, we could get fancy and have our own parse and split functions. Also we could use stack to calculate something similar to infix equation but it will get complicated.