-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_split.py
43 lines (37 loc) · 1.36 KB
/
my_split.py
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
def mysplit(s):
"""Exemplar-related R&D 6/17/18"""
# Collect the starting position of all the spaces and words in s, ignoring
# consecutive spaces and non-space characters, respectively, into lists
# `spaces` and `words`.
in_spaces = False
in_word = False
spaces = []
words = []
for i in range(len(s)):
if s[i]==" ":
in_word = False
if not in_spaces:
spaces.append(i)
in_spaces = True
else: # In a word.
in_spaces = False
if not in_word:
words.append(i)
in_word = True
print(' input: "' + s + '"')
if len(words) and words[0] == 0:
print(' words: ' + str(words) + '\nspaces: ' + str(spaces))
i = 0 # Use first space position as an `s` delimiter.
else:
print('spaces: ' + str(spaces) + '\n words: ' + str(words))
i = 1 # Skip first space position as an `s` delimiter.
# Loop thru the words of `s`, appending each to return_list.
spaces.append(len(s)) # A hack allowing last print of `s`.
return_list = []
for pos in words:
print(s[pos:spaces[i]])
return_list.append(s[pos:spaces[i]])
i += 1
return return_list
s = ' hello world ! '
print(mysplit(s)) # output: ['hello', 'world', '!']