-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.py
More file actions
52 lines (35 loc) · 1.32 KB
/
split.py
File metadata and controls
52 lines (35 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""Split astring by splitter and return list of splits.
This should work like that built-in Python .split() method [*].
YOU MAY NOT USE the .split() method in your solution!
YOU MAY NOT USE regular expressions in your solution!
For example:
>>> split("i love balloonicorn", " ")
['i', 'love', 'balloonicorn']
>>> split("that is which is that which is that", " that ")
['that is which is', 'which is that']
>>> split("that is which is that which is that", "that")
['', ' is which is ', ' which is ', '']
>>> split("hello world", "nope")
['hello world']
* Note: the actual Python split method has special behavior
when it is not passed anything for the splitter -- you do
not need to implemented that.
"""
def split(astring, splitter):
"""Split astring by splitter and return list of splits."""
results = []
index = 0
while index <= len(astring):
curr_index = index
index = astring.find(splitter, index, len(astring))
if index != -1:
results.append(astring[curr_index:index])
index += len(splitter)
else:
results.append(astring[curr_index:])
break
return results
if __name__ == '__main__':
import doctest
if doctest.testmod().failed == 0:
print "\n*** ALL TESTS PASSED. FINE SPLITTING!\n"