Back

Longest words

Introductory tasks
You could have a quick look at some of the methods available for strings and lists, including the Python documentation for these methods. Include the string method split and the list method append

The problem
You must enter in a sentence or two. You program has to search through each word in your sentences and find the longest word. If the longest length occurs for two or more words, then you need to find all those words. For example, if you entered, One flew over the cookoo's nest, you program should return cookoo's but if you entered One flew over the next nest then your program should return flew over next nest.  

Overview of our solution
function to return a list of words from the user's input
function to return a list of the longest words
function for main

main() 

Pseudo-code

def getList:
    enter input
    return input and list of words in input

def longest(from list of words):
    newList = []
    put the first word in newList
    for each word the list of words (starting at the second word):
        if length of the word > length of newList[0]:
            empty the newList
            append this word to newList
        elif length of the word is the same as newList[0]:
            append this word to newList
        return newList

def main():
    myList = getList()
    print the original sentence and a list of the words that have the longest length.

main() 

Solution
length

Extension
If a sentence is entered with a full stop or other punctuation, are they part of a word? What punctuation is a problem? Any issues with punctuation should be dealt with. Students could add all kinds of functionality to this problem, from rearranging and printing the words entered in order of increasing and descreasing length, to removing any words that begin with a vowel. 

Back