mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-06 10:31:29 +08:00
Added to maths and strings (#1642)
* Added to maths and strings * added changes suggest by cclauss
This commit is contained in:

committed by
Christian Clauss

parent
e849578e59
commit
a26ae00b24
33
strings/split.py
Normal file
33
strings/split.py
Normal file
@ -0,0 +1,33 @@
|
||||
def split(string: str, seperator: str = " ") -> list:
|
||||
"""
|
||||
Will split the string up into all the values seperated by the seperator (defaults to spaces)
|
||||
|
||||
>>> split("apple#banana#cherry#orange",seperator='#')
|
||||
['apple', 'banana', 'cherry', 'orange']
|
||||
|
||||
>>> split("Hello there")
|
||||
['Hello', 'there']
|
||||
|
||||
>>> split("11/22/63",seperator = '/')
|
||||
['11', '22', '63']
|
||||
|
||||
>>> split("12:43:39",seperator = ":")
|
||||
['12', '43', '39']
|
||||
"""
|
||||
|
||||
split_words = []
|
||||
|
||||
last_index = 0
|
||||
for index, char in enumerate(string):
|
||||
if char == seperator:
|
||||
split_words.append(string[last_index:index])
|
||||
last_index = index + 1
|
||||
elif index + 1 == len(string):
|
||||
split_words.append(string[last_index : index + 1])
|
||||
return split_words
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from doctest import testmod
|
||||
|
||||
testmod()
|
Reference in New Issue
Block a user