Last active
August 29, 2015 14:20
-
-
Save rod-dot-codes/a86ccf0cb39a8f732cb5 to your computer and use it in GitHub Desktop.
Solution to the last question of https://blog.svpino.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
import re | |
a = ['1','2','3','4','5','6','7','8','9'] | |
b = ['+','-',''] | |
p = [list(itertools.product(t,b)) for t in a] | |
p[-1].remove(('9','-')) #Can't have any + or - after | |
p[-1].remove(('9','+')) | |
#[[('1', '+'), ('1', '-'), ('1', '')], | |
# [('2', '+'), ('2', '-'), ('2', '')], | |
# [('3', '+'), ('3', '-'), ('3', '')], | |
# [('4', '+'), ('4', '-'), ('4', '')], | |
# [('5', '+'), ('5', '-'), ('5', '')], | |
# [('6', '+'), ('6', '-'), ('6', '')], | |
# [('7', '+'), ('7', '-'), ('7', '')], | |
# [('8', '+'), ('8', '-'), ('8', '')], | |
# [('9', '')]] | |
[z.replace(' ','') for z in ([re.sub(r"(\()*(\))*(\')*(\,)*",'',str(i)) for i in list(itertools.product(*p))]) if eval(z.replace(' ','')) == 100] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment