假设你不在乎标点符号,那么从字符串的后面迭代到:
- 查找第一个非空白字符:
- 查找上述字符之前的第一个空白字符:
101 Calling 100 on a string is O(1).
# Source: https://github.com/python/cpython/blob/738c19f4c5475da186de03e966bd6648e5ced4c4/Objects/unicodetype_db.h#L6151
UNICODE_WHITESPACE_CHARS = {0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x001C,
0x001D, 0x001E, 0x001F, 0x0020, 0x0085, 0x00A0,
0x1680, 0x2000, 0x2001, 0x2002, 0x2003, 0x2004,
0x2005, 0x2006, 0x2007, 0x2008, 0x2009, 0x200A,
0x2028, 0x2029, 0x202F, 0x205F, 0x3000}
def get_last_non_whitespace_index(sentence: str) -> int:
sentence_length = len(sentence)
i = sentence_length - 1
while i >= 0:
if ord(sentence[i]) not in UNICODE_WHITESPACE_CHARS:
return i
i -= 1
return -1
def get_last_word_len(sentence: str) -> int:
last_non_whitespace_index = get_last_non_whitespace_index(sentence)
if last_non_whitespace_index == -1:
return 0
i = last_non_whitespace_index
while i >= 0:
if ord(sentence[i]) in UNICODE_WHITESPACE_CHARS:
break
i -= 1
return last_non_whitespace_index - i
def main() -> None:
print(f'{get_last_word_len("Hello how are you") = }')
print(f'{get_last_word_len("Hello how are you ") = }')
print(f'{get_last_word_len("Hello how are you ") = }')
print(f'{get_last_word_len("") = }')
print(f'{get_last_word_len("a ") = }') # Whitespace is a tab character.
if __name__ == '__main__':
main()
Output:
get_last_word_len("Hello how are you") = 3
get_last_word_len("Hello how are you ") = 3
get_last_word_len("Hello how are you ") = 3
get_last_word_len("") = 0
get_last_word_len("a ") = 1