-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_exp_maximize.py
More file actions
45 lines (39 loc) · 1.2 KB
/
math_exp_maximize.py
File metadata and controls
45 lines (39 loc) · 1.2 KB
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
# 프로그래머스 코딩테스트 - 수식최대화(Lv2)
import re
from itertools import permutations
def calc(permutation, n, exp):
if n == len(permutation):
return str(exp)
if permutation[n] == "*":
split = exp.split("*")
temp = []
for s in split:
temp.append(calc(permutation, n + 1, s))
print(temp)
return str(eval("*".join(temp)))
if permutation[n] == "+":
split = exp.split("+")
temp = []
for s in split:
temp.append(calc(permutation, n + 1, s))
print(temp)
return str(eval("+".join(temp)))
if permutation[n] == "-":
split_data = exp.split("-")
temp = []
for s in split_data:
temp.append(calc(permutation, n + 1, s))
print(temp)
return str(eval("-".join(temp)))
def solution(expression):
sum_list = []
operator = re.findall(r'\D', expression)
search = list(set(operator))
permutation = permutations(search, len(search))
for p in permutation:
print(p)
cal = abs(int(calc(p, 0, expression)))
print(cal)
sum_list.append(cal)
answer = max(sum_list)
return answer