aboutsummaryrefslogtreecommitdiff
path: root/challenge-346/eric-cheung/python/ch-2.py
blob: 9b606a0e235ef1d65cda175f3039f6e28b75e2a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from itertools import product
from re import findall

## Example 1
## strInput = "123"
## nTarget = 6

## Example 2
## strInput = "105"
## nTarget = 5

## Example 3
## strInput = "232"
## nTarget = 8

## Example 4
## strInput = "1234"
## nTarget = 10

## Example 5
strInput = "1001"
nTarget = 2

arrOutput = []

arrChar = ["", "+", "-", "*"]
arrCartChar = [arrChar] * (len(strInput) - 1)

strExpr = "%".join([("" if nIndx == 0 else str(nIndx)) + charLoop for nIndx, charLoop in enumerate(list(strInput))])

arrAllList = list(product(*arrCartChar))
arrToRep = ["%" + str(nIndx) for nIndx in range(1, len(strInput))]

for arrLoop in arrAllList:
    strTemp = strExpr
    for strToRep, strNuStr in zip(arrToRep, arrLoop):
        strTemp = strTemp.replace(strToRep, strNuStr)

    if findall("0[0-9]", strTemp):
        continue

    if eval(strTemp) != nTarget:
        continue

    arrOutput.append(strTemp)

print (arrOutput)