단어 뒤집기2


Define input, output

  • Input: Strings, special token “<”, “>” “<…>” is tag, and don’t reverse the tag
  • Output: reversed string with tags

source code

 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
import sys


string = sys.stdin.readline().strip()
stack = []
reverse = ""

is_tag = False
for s in string:
    if s == " ":
        while stack:
            reverse += stack.pop()
        reverse += s
    elif s == "<":
        while stack:
            reverse += stack.pop()
        is_tag = True
        reverse += s
    elif s == ">":
        is_tag = False
        reverse += s
    elif is_tag:
        reverse += s
    else:
        stack.append(s)

while stack:
    reverse += stack.pop()
print(reverse)
Built with Hugo
Theme Stack designed by Jimmy