백준-17413

이서현·2021년 4월 18일
0

Algorithm

목록 보기
5/76

단어뒤집기2

04.17에 푼 문제입니다🌷

결과는 잘 나오나 런타임에러 발생...

import sys
slist=list(map(str,input().split('<')))

def soul(slist):
  word=[]
  result=[]
  reverse=[]
  if len(slist)>1:
    for i in slist:
      if not i:
        continue
      result.append('<')
      word=list(i.split('>'))
      result.append(word[0])
      result.append('>')
      if word[1]:
        reverse=[]
        word=list(word[1].split(' '))
        for j in word:
          reverse.append(j[::-1])
        result.append(' '.join(reverse))
      
  else:
    slist=list(slist[0].split(' '))
    for i in slist:
      result.append(i[::-1])
      result.append(' ')
    result.pop()
  return(''.join(result))

print(soul(slist))

다시 푼 코드


import sys
input=sys.stdin.readline

sinput=list(input())
tag=False
word=''
result=''

for i in sinput:
  if tag==False:
    if i=='<':
      tag=True
      word=word+i
    elif i==' ':
      word=word+i
      result=result+word
      word=''
    else:
      word=i+word

  elif tag==True:
    word=word+i
    if i=='>':
      tag=False
      result=result+word
      word=''

print(result+word)

알고리즘을 최대한 단순화해서 풀어야겠다.🐰🐰

profile
안녕하세요. 이서현입니다( ღ'ᴗ'ღ )

0개의 댓글