Longest Word
In Doc2Doc, we have a search function to find the longest word in a document.
Assignment
Complete the find_longest_word
function without a loop. It accepts string inputs, document
, and optional longest_word
, which is the current longest word and defaults to an empty string.
Check if the first word is longer than the current
longest_word
, then recur for the rest of the document.Ensure there are no potential index errors.
Assume that a "word" means a series of any consecutive non-whitespace characters. For example, longest_word("How are you?")
should return the string "you?"
.
Solution
def find_longest_word(document, longest_word=""):
# Case block, if the len of document is 0, return the longest word
if len(document) == 0:
return longest_word
# Split doc to give us first word, and then rst of string
words = document.split(maxsplit=1)
if len(words)>=1:
if len(words[0]) > len(longest_word):
longest_word = words[0]
if len(words) > 1:
return find_longest_word(words[1], longest_word)
return longest_word
Explanation
Function Signature:
def find_longest_word(document, longest_word=""):
Purpose:
Searches through the document string to determine the longest word.
Parameters:
document
: The remaining text to search through.longest_word
: The current longest word found (defaults to an empty string).
Base Case:
if len(document) == 0: return longest_word
Explanation:
If the document is empty, there's nothing left to search.
Callout: The recursion stops here and returns the longest word found so far.
Splitting the Document:
words = document.split(maxsplit=1)
Explanation:
Splits the document into a list of words, but only into two parts:
The first word.
The rest of the document (everything after the first word).
Callout: This ensures we only work with one word at a time and pass the remainder to the next recursive call, avoiding loops.
Checking and Updating the Longest Word:
if len(words) >= 1: if len(words[0]) > len(longest_word): longest_word = words[0]
Explanation:
If there is at least one word:
Compare the length of the first word (
words[0]
) with the currentlongest_word
.Update
longest_word
if the first word is longer.
Callout: This conditional is the key comparison step to determine which word is longer.
Recursion for the Rest of the Document:
if len(words) > 1: return find_longest_word(words[1], longest_word)
Explanation:
If there is more than one element in
words
, it means there is a remainder of the document.Recursively call
find_longest_word
on the remaining text (words[1]
), passing along the updatedlongest_word
.
Callout: This is the recursive step that processes the rest of the document one word at a time.
Final Return:
return longest_word
Explanation:
If no remainder is found (i.e.,
words
contains only one word), return the currentlongest_word
.
Callout: This covers the case where the document was just one word, or when we've processed all words.
Last updated