Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

2.2 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
68ffb91507a5b645769328c5 Challenge 102: Longest Word 29 challenge-102

--description--

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

  • Words are separated by a single space.
  • Only letters (a-z, case-insensitive) count toward the word's length.
  • If there are multiple words with the same length, return the first one that appears.
  • Return the word as it appears in the given string, with punctuation removed.

--hints--

longest_word("The quick red fox") should return "quick".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(longest_word("The quick red fox"), "quick")`)
}})

longest_word("Hello coding challenge.") should return "challenge".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(longest_word("Hello coding challenge."), "challenge")`)
}})

longest_word("Do Try This At Home.") should return "This".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(longest_word("Do Try This At Home."), "This")`)
}})

longest_word("This sentence... has commas, ellipses, and an exclamation point!") should return "exclamation".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(longest_word("This sentence... has commas, ellipses, and an exclamation point!"), "exclamation")`)
}})

longest_word("A tie? No way!") should return "tie".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(longest_word("A tie? No way!"), "tie")`)
}})

longest_word("Wouldn't you like to know.") should return "Wouldnt".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(longest_word("Wouldn't you like to know."), "Wouldnt")`)
}})

--seed--

--seed-contents--

def longest_word(sentence):

    return sentence

--solutions--

import re
def longest_word(sentence):
    words = sentence.split(" ")
    longest = ""

    for word in words:
        cleaned = re.sub(r'[^a-zA-Z]', '', word)
        if len(cleaned) > len(longest):
            longest = cleaned

    return longest