r/programming • u/svpino • May 08 '15
Five programming problems every Software Engineer should be able to solve in less than 1 hour
https://blog.svpino.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour
2.5k
Upvotes
1
u/cpp_is_king May 08 '15
It's not a challenging problem at all. The solution is brute force. Just write a recursive depth-first search, where each node has 3 branches. -, +, (null). pseudocode:
You can do this even more succinctly with functional programming languages and languages that make conversion between integers and strings more natural, but I think the above should be easily understandable by just about anyone.
I haven't tested it, but I'm pretty sure it should just work. (I leave accumulate and print as an exercise, just treat each number from 1 to 9 as a digit of the current number, building up current number until you hit a non-null operator, then apply the operator to running sum. Or for print, alternate between printing digit and printing operator, skipping the operator if it is null.