Longest Word
In Doc2Doc, we have a search function to find the longest word in a document.
Assignment
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_wordExplanation
Last updated