Algorithm/COS
[COS Pro 1급, Python] 6차 2번 : 단어를 순서대로 적으세요
JMcunst
2022. 2. 28. 17:39
728x90
반응형
문제 유형
코딩
난이도
normal
Note
1. 임계값 K를 업데이트 하면서 필요한 라인 계산.
2. 띄워쓰기 때문에 is_first로 첫 라인의 첫 단어는 띄워쓰기를 빼는 것으로 계산.
Code
# -*- coding: utf-8 -*-
# UTF-8 encoding when using korean
def solution(K, words):
answer = 1
rest_len = K
is_first = True
for word in words:
if is_first:
length = len(word)
else:
length = len(word) + 1
if 0<= rest_len - length <= K:
rest_len -= length
is_first = False
else:
answer += 1
rest_len = K-length
is_first = True
return answer
K = 10
words = ["nice", "happy", "hello", "world", "hi"]
ret = solution(10, words)
print("solution 함수의 반환 값은", ret, "입니다.")
※ 가끔 코드 중 print(~)가 있습니다. 정리 못한 점 죄송합니다.
728x90
반응형