목차

문제제목

ps
링크programmers.co.kr/…
출처프로그래머스
문제 번호67257
문제명수식 최대화
레벨Level 2
분류

브루트포스

시간복잡도O(n)
인풋사이즈n<=100
사용한 언어Python
해결날짜2022/01/10

풀이

코드

"""Solution code for "Programmers 67257. 수식 최대화".

- Problem link: https://programmers.co.kr/learn/courses/30/lessons/67257
- Solution link: http://www.teferi.net/ps/problems/programmers/67257
"""

import functools
import itertools
import operator

OPERATORS = (('*', operator.mul), ('+', operator.add), ('-', operator.sub))


def solve(expression, operators, i):
    if expression.isdigit():
        return int(expression)
    symbol, op = operators[i]
    sub_expressions = expression.split(symbol)
    return functools.reduce(
        op, (solve(x, operators, i + 1) for x in sub_expressions))


def solution(expression):
    return max(
        abs(solve(expression, p, 0)) for p in itertools.permutations(OPERATORS))