CodingTest/Baekjoon

[baekjoon] 백준 17413번(파이썬): 단어 뒤집기 2

JunJangE 2021. 9. 7. 12:38

문제

 

17413번: 단어 뒤집기 2

문자열 S가 주어졌을 때, 이 문자열에서 단어만 뒤집으려고 한다. 먼저, 문자열 S는 아래와과 같은 규칙을 지킨다. 알파벳 소문자('a'-'z'), 숫자('0'-'9'), 공백(' '), 특수 문자('<', '>')로만 이루어져

www.acmicpc.net

알고리즘

- 입력에 따라 명령을 수행한다.

코드

import sys

s = list(map(str, sys.stdin.readline().strip()))

res = ""
word = ""
reverse = True

for c in s:

    if c == '<':
        reverse = False
        res += word
        word = c

    elif c == '>':
        reverse = True
        res += (word + '>')
        word = ""

    elif c == ' ':
        res += word + c
        word = ""

    elif reverse:
        word = c + word

    else:
        word += c

res += word
print(res)

github

 

GitHub - junjange/CodingTest: 내가 푼 코딩 테스트 문제와 해결법

내가 푼 코딩 테스트 문제와 해결법. Contribute to junjange/CodingTest development by creating an account on GitHub.

github.com