728x90
반응형
https://programmers.co.kr/learn/courses/30/lessons/67257#
def multiply(list):
ans = 1
for l in list:
ans *= l
return ans
def minus(list):
ans = list[0]
for l in list:
ans -= l
return ans+list[0]
def solution(expression):
# *-+, *+-
ans = 0
temp = [] # *-+
for t in expression.split("+"):
temp.append(minus([ eval(x) for x in t.split("-")]))
ans = max(ans, abs(sum(temp)))
temp = [] # *+-
for t in expression.split("-"):
temp.append(sum([ eval(x) for x in t.split("+")]))
ans = max(ans, abs(minus(temp)))
temp = [] # -+*
for t in expression.split("*"):
temp.append(sum([ eval(x) for x in t.split("+")]))
ans = max(ans, abs(multiply(temp)))
temp = [] # +-*
for t in expression.split("*"):
temp.append(minus([ eval(x) for x in t.split("-")]))
ans = max(ans, abs(multiply(temp)))
temp = [] # -*+
for t in expression.split("+"):
temp.append(multiply([ eval(x) for x in t.split("*")]))
ans = max(ans, abs(sum(temp)))
temp = [] # +*-
for t in expression.split("-"):
temp.append(multiply([ eval(x) for x in t.split("*")]))
ans = max(ans, abs(minus(temp)))
return ans
정리
def multiply(list):
ans = 1
for l in list:
ans *= l
return ans
def minus(list):
ans = list[0]
for l in list:
ans -= l
return ans+list[0]
def which(s,t):
if s == "*":
return multiply(t)
elif s == "+":
return sum(t)
return minus(t)
def solution(expression):
ans = 0
for op in ["+-*","-+*","*+-","*-+","-*+","+*-"]:
temp = []
for t in expression.split(op[0]):
t = [ eval(x) for x in t.split(op[1])]
temp.append(which(op[1], t))
ans = max(ans, abs(which(op[0], temp)))
return ans
728x90
반응형
'Algorithm Problems' 카테고리의 다른 글
Dynamic Programming (0) | 2021.12.26 |
---|---|
DFS/BFS(2차원) 설명 (0) | 2021.10.24 |
[프로그래머스][Python] 124 나라의 숫자 (0) | 2021.09.21 |
[카카오_인턴][Python] 불량 사용자 - DFS/정규식 (0) | 2021.09.17 |
[백준][Python] 19538번 루머 - 그래프,BFS (0) | 2021.09.17 |