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

1020. Convert a string to an array

daco2020 2022. 10. 20. 21:13
반응형

Write a function to split a string and convert it into an array of words.

Examples (Input ==> Output):

"Robin Singh" ==> ["Robin", "Singh"]

"I love arrays they are my favorite" ==> ["I", "love", "arrays", "they", "are", "my", "favorite"]



Solution:

from typing import List


def string_to_array(s: str) -> List[str]:
    return split(s)

def split(s: str, c: str = " ") -> List[str]:
    list, ci = [], 0
    for i, v in enumerate(s):
        if v == c:
            list.append(s[ci:i])
            ci = i+1
    list.append(s[ci:])
    return list


반응형