Is there an easy way to get longest word that contains a certain letter?

Related searches

I am trying to get the largest word that contains the letter 'f' through a for loop, through my code I tried to make it as simple as possible with a if and and statement. It is however not working as it returns the largest word but not the one that includes the letter f, why is it not working?

def main():
    sentence = "Absence makes the heart grow fonder"
    letter = "f"
    longest_word = get_longest_word(sentence, letter)
    print('"' + longest_word + '" is the longest word containing the letter "'
          + letter + '".')

def get_longest_word(sentence, letter):
    words_list = sentence.split()
    largest = words_list[0]
    my_list = []
    for word in words_list:
        if letter in word and len(word) > len(largest):
            largest = word
    return largest 


main()

The reason is that you initialise largest to words_list[0], which is 'Absence', the longest word in the sentence.

Therefore, even when 'fonder' is reached, even though the first part of your if passes (it contains an 'f'), the second part about length does not.

Personally, I would just do this:

from operator import itemgetter

def get_longest_word(sentence, letter):
    words_with_letter = ((word, len(word)) for word in sentence.split() if letter in word)
    return max(words_with_letter, key=itemgetter(1))[0]

This converts the sentence into an iterable of tuples where the first element is the word and the second its length, and where all the words already contain the letter in question.

Then, all we need to do is get the word with the longest length i.e. the highest value in the second position.

Output:

"fonder" is the longest word containing the letter "f".

Longest Word with these Letters Finder - Online Word Maker, Return the length of the longest word in the provided sentence. The split() method splits a String object into an array of strings by If you don't add the space in the parenthesis, you will have this output: In Unicode, numbers come before upper case letters, which come before lower case letters. Yep, the longest word in the dictionary is a joke. The longest well-known nonsense word is 34 letters: Supercalifragilisticexpialidocious. Anyone versed in their Mary Poppins can define this for

You could just do:

sorted([w for w in words if "f" in w], key=len)[-1]

Three Ways to Find the Longest Word in a String in JavaScript, It contains well written, well thought and well explained computer science and programming articles, quizzes and Given a string, we have to find the longest word in the input string and then calculate the number of characters in this word. Examples: The idea is simple, we traverse the given string. People sometimes ask whether a DNA string can be considered as the longest English word, given that they can run to many thousands of letters. The answer is no: they're regarded as chemical names rather than genuine words in the sense of meaningful items of vocabulary. The same is true of the formal names of chemical compounds.

You can approach this much more easily using the in operator:

for word in word_list:
    if 'f' in word:
        longest_words.append(word)

longest_word = sorted(longest_words)[0]

Program for length of the longest word in a sentence, Given a string S and a set of words D , find the longest word in D that is a Word W is a subsequence of S if some number of characters, possibly zero, Since it takes only O(N) time to preprocess S in this way, the total complexity is First, for each word w in D , create a 2-tuple containing w and the number 0 (i.e. (w, 0) ). Given a string, we have to find the longest word in the input string and then calculate the number of characters in this word. Examples: Input : A computer science portal for geeks Output : Longest word's length = 8 Input : I am an intern at geeksforgeeks Output : Longest word's length = 13

Find longest word in dictionary that is a subsequence of a given string, This is the best place to expand your knowledge and get prepared for your next interview. Given a string and a string dictionary, find the longest string in the dictionary that can be All the strings in the input will only contain lower-case letters. Two PointersSort. Similar Questions. Longest Word in Dictionary. Easy � In a given string, I want to find the longest word and print the same. The output I get is the second longest word, i.e "Today", but I should get "Happiest". May I know what I am doing wrong or is there a better/different way to find the longest word in a string?

Longest Word in Dictionary through Deleting, No, you will not find the very longest word in English in this article. That one word would span about fifty-seven pages. It’s the chemical name for the titin protein found in humans. Its full name has 189,819 letters. Would you like to hear it pronounced? One man helpfully sounds it out in a YouTube video, but pop some popcorn before you get started!

This 11-letter word, abstentious, is considered the longest word that features the vowels in alphabetic order exactly (A-E-I-O-U). Sorry, Y. Related to the word abstain, abstentious can characterize someone who doesn't indulge in excesses.

Comments
  • The problem is that you initialise largest to the first word, even if it doesn't have the sought letter. Initialise it to '' and get rid of my_list (which you're not using)