[HackerRank][Python3] Simple Text Editor
2018. 9. 2. 23:12 |
프로그래밍/HackerRank
Problem :
https://www.hackerrank.com/challenges/simple-text-editor/problem
My Solution :
#!/usr/bin/env python3
def simple_text_editor(queries):
S = []
job_history = []
for query in queries:
if 1 < len(query):
query = query.split()
if query[0] == '1':
word = list(query[1])
S.extend(word)
job_history.append(('1', word))
elif query[0] == '2':
k = int(query[1])
S, word = S[:-k], S[-k:]
job_history.append(('2', word))
else:
k = int(query[1])
print(S[k-1])
else:
query = job_history.pop()
if query[0] == '1':
k = len(query[1])
S = S[:-k]
else:
word = query[1]
S.extend(word)
Q = int(input())
queries = []
for _ in range(Q):
queries.append(input())
simple_text_editor(queries)
Comment :
처음에는 list.extend 대신 일일이 loop 돌면서 append 해주었는데, 코드를 줄이고자 list_a += list_b 스타일로 변경하였다. 그랬더니 꽤 많은 TC에서 Timeout이 발생하였고, list_a.extend(list_b)로 변경 후 해결되었다. 그동안 별 생각없이 list.extend 대신 += 연산을 사용하였는데, +=는 객채를 새로 생성하기 때문에 훨씬 더 Cost가 높다는 점을 인지하게 되었다.
'프로그래밍 > HackerRank' 카테고리의 다른 글
| [HackerRank][Python3] Weighted Uniform Strings (0) | 2018.09.04 |
|---|---|
| [HackerRank][Python3] HackerRank in a String! (0) | 2018.09.03 |
| [HackerRank][Python3] Truck Tour (0) | 2018.09.03 |
| [HackerRank][Python3] Waiter (0) | 2018.09.03 |
| [HackerRank][Python3] Equal Stacks (0) | 2018.09.02 |
| [HackerRank][Python3] Maximum Element (0) | 2018.09.02 |
| [HackerRank][Python3] Matrix Layer Rotation (2) | 2018.08.08 |
| [HackerRank][Python3] Dijkstra: Shortest Reach 2 (0) | 2018.08.07 |
최근에 달린 댓글 최근에 달린 댓글