Last Word Length
Words in a text are separated by one or more spaces. The text may start or end with spaces.
Write last_word_length(text) that returns the length of the last word. If the text has no words (it is empty, or only spaces), return 0.
Understand the problem
Check your understanding of the rules before you code.
What should last_word_length("fly me to the moon") return?
The last word is moon, which has 4 letters.
What should last_word_length("moon ") return? The text ends with three spaces.
Spaces are separators, not part of a word. The last word is still moon, so the answer is 4, not 0. Trailing spaces are the trap in this problem.
What should last_word_length(" ") return?
03- It is an error
There are no words at all, and the task says to return 0 in that case. Whenever a task says "if there is no ...", make that case one of your test inputs.
Try examples by hand
Look at what the obvious tools give on these inputs.
What does "moon ".split(" ") give? (The text is moon followed by two spaces.)
["moon"]["moon", ""]["moon", "", ""]
Splitting on a single space cuts at every space, so two trailing spaces leave two empty strings at the end. The last item is "", which has length 0: wrong.
What does "moon ".split() (no argument) give?
["moon"]["moon", ""]["moon", "", ""]
split() with no argument treats any run of spaces as a single separator and ignores spaces at both ends. For this task it does most of the work.
Write the solution
The tests include extra spaces at both ends, between words, a single word, and no words at all.
def last_word_length(text):
# Write your code here.
pass
Hint 1: a question to ask yourself
What happens to the leading and trailing spaces if you cut the text into words with the right tool?
Hint 2: the shape of the answer
Get the list of words. If the list is empty, return 0. Otherwise measure its last item.
Reflect
Solution
def last_word_length(text):
words = text.split()
if not words:
return 0
return len(words[-1])
Another way: walk backwards, with no list of words
def last_word_length(text):
end = len(text) - 1
while end >= 0 and text[end] == " ":
end -= 1
start = end
while start >= 0 and text[start] != " ":
start -= 1
return end - start
Pattern: test the inputs that make a quick idea fail. text.split(" ") looks right on "fly me to the moon" and fails on trailing spaces. When you have a first idea, ask: what is the weirdest input it must survive? Here: empty text, only spaces, spaces at both ends, several spaces in a row.
Cost: split() reads the text once and builds a list of words, so time and space are both O(n). The backwards walk reads only the end of the text and uses O(1) extra space, which is a good thing to mention if an interviewer asks for it.
What an interviewer might ask next: Do it without building the list of words. (The second solution above does.)
VISIBLE = 3
CASES = [
(('fly me to the moon',), 4, 'a normal sentence'),
(('moon ',), 4, 'spaces at the end'),
((' hello world ',), 5, 'spaces everywhere'),
(('',), 0, 'empty text'),
((' ',), 0, 'only spaces'),
(('a',), 1, 'one letter'),
(('word',), 4, 'one word'),
((' leading',), 7, 'spaces at the start'),
(('one two three ',), 5, 'spaces at the end after several words'),
]