Skip to content

Commit faa20a4

Browse files
committed
target-sum
1 parent d41b451 commit faa20a4

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

Facebook/Ninja/123456789.py

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
'''
2+
Question:
3+
123456789 = 100 (also known as targetSum)
4+
Using standard integer arithmeric operators +, -, how many different solutions
5+
can you find by inserting the operators between some digits?
6+
7+
Solutions;
8+
123-45-67+89 = 100
9+
123-4-5-6-7+8-9 = 100
10+
123+45-67+8-9 = 100
11+
123+4-5+67-89 = 100
12+
12-3-4+5-6+7+89 = 100
13+
12+3-4+5+67+8+9 = 100
14+
12+3+4+5-6-7+89 = 100
15+
1+23-4+56+7+8+9 = 100
16+
1+23-4+5+6+78-9 = 100
17+
1+2+34-5+67-8+9 = 100
18+
1+2+3-4+5+6+78+9 = 100
19+
-1+2-3+4+5+6+78+9 = 100
20+
21+
'''
22+
23+
class Tree:
24+
def __init__(self, data: str) -> None:
25+
self.data = data
26+
self.left = None
27+
self.right = None
28+
self.middle = None
29+
30+
class SolutionOne(object):
31+
def __init__(self, digits: str, target: int) -> None:
32+
self.digits = digits
33+
self.target = target
34+
35+
def strToRes(self, s: str) -> int:
36+
"""
37+
examples:
38+
'#-1#+2#-234#+6' -> -227
39+
'1#+2#-234#+6' -> -225
40+
"""
41+
numbers = s.split('#')
42+
if numbers[0] == '':
43+
return sum([int(x) for x in numbers[1:]])
44+
else:
45+
return sum([int(x) for x in numbers])
46+
47+
def evalAndPrint(self, s: str) -> None:
48+
if self.strToRes(s) == self.target:
49+
print(
50+
s.translate(str.maketrans('', '', '#')) +
51+
' = ' +
52+
str(self.target)
53+
)
54+
55+
def targetSum(self) -> None:
56+
root = Tree(None)
57+
self.createTree(root, 0, len(self.digits))
58+
self.tranverse(root, '', self.digits)
59+
60+
def createTree(self, root: Tree, cur: int, depth: int) -> None:
61+
if cur == 0:
62+
root.left = Tree('')
63+
root.right = Tree('#-')
64+
self.createTree(root.left, cur+1, depth)
65+
self.createTree(root.right, cur+1, depth)
66+
elif cur < depth:
67+
root.left = Tree('')
68+
root.right = Tree('#-')
69+
root.middle = Tree('#+')
70+
self.createTree(root.left, cur+1, depth)
71+
self.createTree(root.right, cur+1, depth)
72+
self.createTree(root.middle, cur+1, depth)
73+
74+
def tranverse(self, tree: Tree, cur_path: str, digits: str) -> None:
75+
if cur_path == '':
76+
self.tranverse(
77+
tree.left,
78+
cur_path + tree.left.data + digits[0],
79+
digits[1:],
80+
)
81+
self.tranverse(
82+
tree.right,
83+
cur_path + tree.right.data + digits[0],
84+
digits[1:],
85+
)
86+
elif tree.left == None and tree.middle == None and tree.right == None:
87+
self.evalAndPrint(cur_path)
88+
else:
89+
self.tranverse(
90+
tree.left,
91+
cur_path + tree.left.data + digits[0],
92+
digits[1:],
93+
)
94+
self.tranverse(
95+
tree.right,
96+
cur_path + tree.right.data + digits[0],
97+
digits[1:],
98+
)
99+
self.tranverse(
100+
tree.middle,
101+
cur_path + tree.middle.data + digits[0],
102+
digits[1:],
103+
)
104+
105+
class SolutionTwo(object):
106+
def __init__(self, digits: str, target: int) -> None:
107+
self.digits = digits
108+
self.target = target
109+
110+
def evalAndPrint(self, s: str) -> None:
111+
if eval(s) == self.target:
112+
print(s + ' = ' + str(self.target))
113+
114+
def targetSum(self) -> None:
115+
self.step('', 0)
116+
117+
def step(self, path: str, i: int) -> None:
118+
if i >= len(self.digits):
119+
self.evalAndPrint(path)
120+
return
121+
122+
self.step(path + '' + self.digits[i], i+1)
123+
self.step(path + '-' + self.digits[i], i+1)
124+
125+
if i != 0:
126+
self.step(path + '+' + self.digits[i], i+1)
127+
128+
if __name__ == '__main__':
129+
print('=== solution 1 ===')
130+
SolutionOne('123456789', 100).targetSum()
131+
132+
print('=== solution 2 ===')
133+
SolutionTwo('123456789', 100).targetSum()

0 commit comments

Comments
 (0)