나는 이렇게 학습한다/Algorithm & SQL

0911. Add Length

daco2020 2022. 9. 11. 18:28
반응형

What if we need the length of the words separated by a space to be added at the end of that same word and have it returned as an array?

Example(Input --> Output)

"apple ban" --> ["apple 5", "ban 3"]
"you will win" -->["you 3", "will 4", "win 3"]

Your task is to write a function that takes a String and returns an Array/list with the length of each word added to each element .

Note: String will have at least one element; words will always be separated by a space.



Solution:

from typing import List


def get_words(func):
    def wrapper(sentence: str):
        return func([word for word in sentence.split()])
    return wrapper

@get_words
def add_length(words: List[str]):
    return [f"{word} {len(word)}" for word in words]


반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

알고리즘 포스팅 관련 공지  (0) 2022.09.13
0912. Fake Binary  (0) 2022.09.13
0910. Quarter of the year  (0) 2022.09.10
0909. Will there be enough space?  (0) 2022.09.09
0908. Sorted? yes? no? how?  (0) 2022.09.08