Skip to content

Instantly share code, notes, and snippets.

@rod-dot-codes
Last active August 29, 2015 14:20
Show Gist options
  • Save rod-dot-codes/a86ccf0cb39a8f732cb5 to your computer and use it in GitHub Desktop.
Save rod-dot-codes/a86ccf0cb39a8f732cb5 to your computer and use it in GitHub Desktop.
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