r/programming • • 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

2.1k comments sorted by

View all comments

Show parent comments

3

u/abeliangrape May 08 '15

It pays to use a high level language for these types of problems. Check out my python solution:

from itertools import *
l = [str(x/2) if x%2==0 else '' for x in range(2,19)]
for ops in product(['','+','-'],repeat=8):
  l[1::2] = ops
  if eval(''.join(l))==100:
    print "%s = 100"%''.join(l)

And the output:

123+45-67+8-9 = 100
123+4-5+67-89 = 100
123-45-67+89 = 100
123-4-5-6-7+8-9 = 100
12+3+4+5-6-7+89 = 100
12+3-4+5+67+8+9 = 100
12-3-4+5-6+7+89 = 100
1+23-4+56+7+8+9 = 100
1+23-4+5+6+78-9 = 100
1+2+34-5+67-8+9 = 100
1+2+3-4+5+6+78+9 = 100

2

u/hk__ May 08 '15

They should forbid the use of eval for this problem.

1

u/baseketball May 08 '15

The eval part isn't difficult. You can always write your own eval for something like this and it would be 100x easier to read and understand than the solution the blogger gave.

1

u/[deleted] May 08 '15

Nice elegant solution here!