Back to Freecodecamp

Challenge 50: Longest Word

curriculum/challenges/english/blocks/daily-coding-challenges-python/68b7cadffed0e75a517da66f.md

latest1.3 KB
Original Source

--description--

Given a sentence, return the longest word in the sentence.

  • Ignore periods (.) when determining word length.
  • If multiple words are ties for the longest, return the first one that occurs.

--hints--

get_longest_word("coding is fun") should return "coding".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_longest_word("coding is fun"), "coding")`)
}})

get_longest_word("Coding challenges are fun and educational.") should return "educational".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_longest_word("Coding challenges are fun and educational."), "educational")`)
}})

get_longest_word("This sentence has multiple long words.") should return "sentence".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_longest_word("This sentence has multiple long words."), "sentence")`)
}})

--seed--

--seed-contents--

py
def get_longest_word(sentence):

    return sentence

--solutions--

py
def get_longest_word(sentence):
    words = sentence.split()
    longest = ''
    for word in words:
        clean_word = word.replace('.', '')
        if len(clean_word) > len(longest):
            longest = clean_word
    return longest