반응형
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
반응형
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
1022. Area of a Square (0) | 2022.10.22 |
---|---|
1021. USD => CNY (0) | 2022.10.21 |
1019. Is it a palindrome? (0) | 2022.10.19 |
1018. Find the smallest integer in the array (0) | 2022.10.18 |
1017. Enumerable Magic - Does My List Include This? (0) | 2022.10.17 |