Python Forum
getting a word from a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getting a word from a list
#1
I have to get a word which contains the longest word containing "e" from a list. how would I go about this a main function has not been provided. this function has been provided

def test_get_longest_e_word():
    print("1.", get_longest_e_word(["Melissa", "Jessie", "Kath", "Amity", "Raeann"]))
    print("2.", get_longest_e_word(["Jo", "Jessie", "Penelope", "Jin", "Raeann", "Pamelita"]))
    etc....
def get_longest_e_word(word_list):

Im having issues with defining word list how would i in the main function refer to to the test function
Reply
#2
def get_longest_e_word(l):
    def e_len(val):
        return len(val) if "e" in val else 0
    return max(l, key=e_len)
It works but if by any chance this is your assignment then it explicitly states how it should be implemented:

Quote:Define the get_longest_e_word() function which is passed a list of strings as a parameter. The function returns the word in the list which has the most characters (i.e., the longest word) BUT only words which have 6 or more characters and contain the letter 'e' (or 'E') are considered. If two or more words in the list have the same number of characters as the longest word and both contain the letter 'e' (or 'E'), the function should return the last word from the start of the list which has the most characters.

If the parameter list is empty or if there are no 6 letter or longer words in the list which contain the letter 'e' (or 'E'), the function should return the empty string. For example, the following code:

print("1.", get_longest_e_word(["Melissa", "Jessie", "Kath", "Amity", "Raeann"]))
print("2.", get_longest_e_word(["Jo", "Jessie", "Penelope", "Jin", "Raeann", "Pamelita"]))
print("3.", get_longest_e_word(["Alan", "Melita", "Amity", "Rosalia", "Rositta", "LeeAnne"]))
print("4. ", "***", get_longest_e_word(["Jo", "Jai", "Jen", "Jing", "Joey", "Jess"]), "***", sep = "")
print("5. ", "***", get_longest_e_word([]), "***", sep = "")
print("6.", "***" + get_longest_e_word([""]) + "***")

prints:

1. Melissa
2. Pamelita
3. LeeAnne
4. ******
5. ******
6. ******
"""
def get_longest_e_word(word_list):
return ""
Reply
#3
yes this is my assingment yeah i cant ask for code or anything but im really struggling just started coding
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  look at first letter in list and append word for assertion mapypy 3 1,928 Feb-06-2021, 05:45 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020