TheAlgorithms-Python

Форк
0
34 строки · 866.0 Байт
1
def split(string: str, separator: str = " ") -> list:
2
    """
3
    Will split the string up into all the values separated by the separator
4
    (defaults to spaces)
5

6
    >>> split("apple#banana#cherry#orange",separator='#')
7
    ['apple', 'banana', 'cherry', 'orange']
8

9
    >>> split("Hello there")
10
    ['Hello', 'there']
11

12
    >>> split("11/22/63",separator = '/')
13
    ['11', '22', '63']
14

15
    >>> split("12:43:39",separator = ":")
16
    ['12', '43', '39']
17
    """
18

19
    split_words = []
20

21
    last_index = 0
22
    for index, char in enumerate(string):
23
        if char == separator:
24
            split_words.append(string[last_index:index])
25
            last_index = index + 1
26
        elif index + 1 == len(string):
27
            split_words.append(string[last_index : index + 1])
28
    return split_words
29

30

31
if __name__ == "__main__":
32
    from doctest import testmod
33

34
    testmod()
35

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.