๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ˆ˜์น˜ ์ตœ๋Œ€ํ™” (Python)

1. ๋ฌธ์ œ ์„ค๋ช…

๋ฌธ์ œ ๋งํฌ

  • input : ์ˆ˜์‹
  • return : ์—ฐ์‚ฐ์ž์˜ ์šฐ์„ ์ˆœ์œ„๋ฅผ ์ค‘๋ณต๋˜์ง€ ์•Š๋„๋ก ์ž„์˜๋กœ ์„ ์ •ํ•˜์—ฌ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜ ์ค‘์—์„œ ์ตœ๋Œ“๊ฐ’ ์ถœ๋ ฅ

2. ์ฝ”๋“œ

import re
from itertools import permutations

def solution(expression):
    operator = [x for x in ['*','+','-'] if x in expression]
    operators = [list(y) for y in permutations(operator)]
    expression = re.split(r'(\D)',expression) #(\D): ์ˆซ์ž๊ฐ€ ์•„๋‹Œ๊ฒƒ ๊ธฐ์ค€์œผ๋กœ split

    answer = []
    for operator in operators:
        ex_copy = expression #๋ณต์‚ฌ
        for op in operator:
            while op in ex_copy: #๋ถ€ํ˜ธ๊ฐ€ ์กด์žฌํ•˜๋Š” ๋™์•ˆ ๋ฐ˜๋ณต
                i = ex_copy.index(op) #์—ฐ์‚ฐ์ž๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ณณ์˜ ์ธ๋ฑ์Šค
                #์—ฐ์‚ฐ์ž ๋ฐ”๋กœ ์ „ ์ž๋ฆฌ์— ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅ
                ex_copy[i-1] = str(eval(ex_copy[i-1] + ex_copy[i] + ex_copy[i+1]))
                ex_copy = ex_copy[:i] + ex_copy[i+2:] #์ด๋ฏธ ๊ณ„์‚ฐํ•œ ๋ถ€๋ถ„ ์ œ๊ฑฐ

        answer.append(ex_copy[0])

    return max(abs(int(x)) for x in answer)

3. ํšŒ๊ณ 

  • str to list๋ฅผ ์ง„ํ–‰ํ•  ๋•Œ, ํŠน์ • ๊ทœ์น™์„ ๊ทธ๋ฃนํ™”ํ•˜๊ธฐ
    • expression = re.split(r'(\D)',expression)
    • '[]' : ๊ด„ํ˜ธ ์•ˆ์˜ ๋ฌธ์ž๋“ค์„ ํ•˜๋‚˜ํ•˜๋‚˜์”ฉ ์ธ์‹ํ•จ. '[abcd]' ๋ผ๋Š”๊ฒŒ ์žˆ์œผ๋ฉด, a ํ˜น์€ b ํ˜น์€ c ํ˜น์€ d ๋กœ ์ธ์‹๋จ.
    • '()' : ํŠน์ˆ˜๋ฌธ์ž ์•ˆ์— ์žˆ๋Š” ๊ธ€์ž๋ฅผ ํ•˜๋‚˜์˜ ๋ฉ์–ด๋ฆฌ๋กœ ์ธ์‹ํ•จ. a ํ˜น์€ b ํ˜น์€ c ํ˜น์€ d ๋กœ ์ธ์‹๋˜๊ฒŒ ํ•˜๋ ค๋ฉด, (a|b|c|d) ์ด๋Ÿฐ์‹์œผ๋กœ ์ž‘์„ฑ์„ ํ•ด์•ผํ•จ
    • ์ฒ˜์Œ์—๋Š” [์ˆซ์ž๊ทธ๋ฃน][๋ถ€ํ˜ธ๊ทธ๋ฃน] ์ด๋ ‡๊ฒŒ ๋‚˜๋ˆ„๋ ค๋‹ค๊ฐ€ ์ž๊พธ ํ•œ ์ž๋ฆฌ ์ˆซ์ž๋ฅผ ์ˆซ์ž ํ•˜๋‚˜๋กœ ๋ณด๋Š” ์—๋Ÿฌ๊ฐ€ ๋‚ฌ์—ˆ๋‹ค. ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ๊ฑด ์—ฌ๋Ÿฌ์ˆซ์ž๋“ค์„ ์ˆซ์ž ํ•˜๋‚˜๋กœ ๋ณด๋Š” ๊ฒƒ์ด์—ˆ๋Š”๋ฐ, ๊ทธ๋ ‡๊ฒŒ ํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š”, ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ฒƒ์„ ๊ธฐ์ค€'(\D)'์œผ๋กœ splitํ•ด์•ผํ•œ๋‹ค.
  • ex_copy์—์„œ ์ด๋ฏธ ๊ณ„์‚ฐํ•œ ๋ถ€๋ถ„ ์ œ๊ฑฐํ•˜๊ธฐ
    • ex_copy = ex_copy[:i] + ex_copy[i+2:]
    • i-1,i,i+1 ์˜ ์—ฐ์‚ฐ๊ฒฐ๊ณผ๋ฅผ ex_copy[i-1]์— ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ๋„ฃ์–ด์ฃผ์—ˆ์œผ๋‹ˆ, i์™€,i+1๋ฒˆ์งธ ์š”์†Œ๋“ค์€ ์ง€์›Œ์•ผ ํ•œ๋‹ค.
    • ๊ณ„์‚ฐํ•œ ๋ถ€๋ถ„์„ ์ œ๊ฑฐํ•  ๋•Œ๋Š” del๋ฅผ ์จ์„œ ํŠน์ •์ธ๋ฑ์Šค๋ฅผ ์ง€์›Œ์ฃผ๋Š” ๊ฒƒ๋ณด๋‹ค ํ•„์š”ํ•œ ๋ถ€๋ถ„๋งŒ ์ž˜๋ผ์„œ ๋ถ™์—ฌ์ฃผ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
  • ์–•์€ ๋ณต์‚ฌ์™€ ๊นŠ์€ ๋ณต์‚ฌ
    • ์–•์€ ๋ณต์‚ฌ:a ๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ํ›„ b = a ์™€ ๊ฐ™์ด ํ• ๋‹นํ•˜๋ฉด ์ด๋Š” a ๊ฐ์ฒด๊ฐ€ ํ†ต์งธ๋กœ b ๋กœ ๋ณต์‚ฌ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ๋‹จ์ง€ b ๋˜ํ•œ a ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐ๋งŒ ํ•  ๋ฟ. ๋”ฐ๋ผ์„œ, ์•„๋ž˜์™€ ๊ฐ™์ด a ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ๋ฒˆ์งธ ๊ฐ’์„ ๋ฐ”๊พธ๋ฉด b ์˜ ์ฒซ๋ฒˆ์งธ ๊ฐ’๋„ ๊ฐ™์€ ๊ฒƒ์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ๋ฐ”๋€Œ์–ด ์žˆ๋‹ค.
    • ๊นŠ์€ ๋ณต์‚ฌ: ๊ฐ์ฒด๋ฅผ ์™„์ „ํ•˜๊ฒŒ ๋ณต์‚ฌ
      • ๋ฆฌ์ŠคํŠธ์ผ ๊ฒฝ์šฐ, b = a[:] ์‚ฌ์šฉ
      • ๋ฆฌ์ŠคํŠธ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ๋Š” copy ๋ชจ๋“ˆ ์‚ฌ์šฉ: (import copy, b = copy.deepcopy(a))

4. ์ฐธ๊ณ 

https://velog.io/@cyanred9/SQL-%EC%A0%95%EA%B7%9C%ED%91%9C%ED%98%84%EC%8B%9D%EC%9D%98-%EC%86%8C%EA%B4%84%ED%98%B8-%EB%8C%80%EA%B4%84%ED%98%B8-%EC%B0%A8%EC%9D%B4

http://cloudrain21.com/python-functions-to-memorize-1