发现自己写python的空格split还挺多坎的,尤其是最后一个是空格的情形:
def split(s):
i = 0
ans = []
while i < len(s):
start = i
# find space
while i < len(s) and s[i] != ' ':
i += 1
ans.append(s[start:i])
i += 1
if s and s[-1] == " ":
ans.append("")
return ans
assert split("") == []
assert split(" ") == ["", ""]
assert split(" ") == ["", "", ""]
assert split("a") == ["a"]
assert split("a b") == ["a", "b"]
assert split(" a") == ["", "a"]
assert split("a ") == ["a", ""]
assert split(" a b") == ["", "a", "b"]
assert split("a b ") == ["a", "b", ""]
assert split("ac bcd") == ["ac", "bcd"]
标签:ac,space,python,assert,split,ans,append From: https://blog.51cto.com/u_11908275/6380974