Question

PYTHON --create a program that accepts a list of words and returns the longest word inside...

PYTHON

--create a program that accepts a list of words and returns the longest word inside that list

can only use --- append, len, str, float, range, strip, split, int

--create a program that accepts a list of words and a specific length. function will returns a new list that contains the words found with the specific length, and return an empty list if none are found

can only use --- append, len, str, float, range, strip, split, int

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In this Python program,

- We have defined 2 functions namely LongestWord and WordsList.
- The function LongestWord will accept a list of words and returns the longest word inside that list.
- The function WordsList will accept a list of words and a specific length. the function will return a new list that contains the words found with the specific length, and return an empty list if none are found.
- We have added the validation cases for both the functions to check the functionality and printing the output onto the console.

(I believe that I made the code simple and understandable. If you still have any query, Feel free to drop me a comment)

Code-1:

#This function will accepts a list of words and returns the longest word inside that list
def LongestWord(words):
  
maximum=0 #To store the index of the longest word
  
#Iterating through all the words in the list
for i in range(0,len(words)):
#Compare the length of that index with the current word, update the maximum if so
if(len(words[i])>len(words[maximum])):
maximum=i
  
#Now maximum holds the LongestWord index so return the word using that index
return words[maximum]
  
  
#Validation Part
print( LongestWord(["A","BB","CCC","DDDD","EEEEE"]) )
print( LongestWord(["123456789","1234","12","1","123"]) )

Code-2:

#This function accepts a list of words and a specific length. function will
#returns a new list that contains the words found with the specific length, and
#return an empty list if none are found
def WordsList(words,length):
  
result=[] #To store the resultant words
  
#Itearate over the words in the list
for i in words:
#Compare the length if it is equal then add it to the new list
if(len(i)==length):
result.append(i)
  
#Return the result
return result


print( WordsList(["A","BB","CCC","DDDD","EEEEE","ABC"],3) )
print( WordsList(["1234","12","1","123","123456789","A"],1) )
print( WordsList(["1234","12","1","123","123456789","A"],10) )

Please check the compiled program and its output for your reference:

Code-1:
main.py 1 #This function will accepts a list of words and returns the Longest word inside that list 2. def LongestWord(words)
Output:
input EEEEE 123456789 ...Program finished with exit code 0 Press ENTER to exit console.

Code-2:
main.py 1 #This function accepts a list of words and a specific Length. function will 2 #returns a new list that contains the
Output:
input [CCC, ABC] [1, A] [ ... Program finished with exit code o Press ENTER to exit console.

Hope this Helps!!!
Please upvote as well, If you got the answer?
If not please comment, I will Help you with that...

Add a comment
Know the answer?
Add Answer to:
PYTHON --create a program that accepts a list of words and returns the longest word inside...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • python program- ---create a function that returns the longest word inside a list of words ---create...

    python program- ---create a function that returns the longest word inside a list of words ---create a function with two parameters (phrase,wordList) that returns a count of how many words are spelled incorrectly that cannot be found in wordList (a list of words) for both you can only use these functions --- append, len, str, float, range, strip, split, int.

  •    PYTHON --create a function that accepts a list of numbers and an int that returns...

       PYTHON --create a function that accepts a list of numbers and an int that returns index of 2nd occurrence of the int in list, otherwise returns None if # does not repeat more 2 or more times EX: [10,24,3,45,10,49,4,5], 10) returns 4 --create a function that accepts a string that returns true if every letter of the alphabet can be found at least one time in the string, (has to be the lowercase alphabet), and false otherwise.    for...

  • python function will Return True if string x contains 3 vowels in a row, in consecutive...

    python function will Return True if string x contains 3 vowels in a row, in consecutive locations, false otherwise. assuming that 'vowels' refer to the following lowercase lttrs: a,e,i,o,u programs fails partially, only allowed to use float, str, int, appen, split, strip, len, range def vowels_three(x): for i in range (o, len(x), 2): if x[i] not in ('a,e,i,o,u'): return False return True

  • I cant get this python program to read all the unique words in a file. It...

    I cant get this python program to read all the unique words in a file. It can only read the number of the unique words in a line. import re import string from collections import Counter # opens user inputted filename ".txt" and (w+) makes new and writes def main(): textname = input("Enter the file to search: ") fh = open(textname, 'r', encoding='utf-8' ) linecount = 0 wordcount = 0 count = {} print("Sumary of the", fh) for line in...

  • python code that Returns a LIST that contains ALL WORDS in word_list that have the specified...

    python code that Returns a LIST that contains ALL WORDS in word_list that have the specified length and Returns an empty list if there are no such words

  • Using Python. You will create two classes and then use the provided test program to make sure your program works This m...

    Using Python. You will create two classes and then use the provided test program to make sure your program works This means that your class and methods must match the names used in the test program You should break your class implementation into multiple files. You should have a car.py that defines the car class and a list.py that defines a link class and the linked list class. When all is working, you should zip up your complete project and...

  • ** Language Used : Python ** PART 2 : Create a list of unique words This...

    ** Language Used : Python ** PART 2 : Create a list of unique words This part of the project involves creating a function that will manage a List of unique strings. The function is passed a string and a list as arguments. It passes a list back. The function to add a word to a List if word does not exist in the List. If the word does exist in the List, the function does nothing. Create a test...

  • Python: Create a function count_target_in_list that takes a list of integers and the target value then...

    Python: Create a function count_target_in_list that takes a list of integers and the target value then counts and returns the number of times the target value appears in the list. Write program in that uses get_int_list_from_user method to get a list of 10 numbers, then calls the count_target_list method to get the count then prints the number of times the target was found in the list. def get_int_list_from_user(): lst=[] y = int(input("Enter number of numbers")) for x in range(y): lst.append(int(input("Enter...

  • write a program which include a class containing an array of words (strings).The program will search...

    write a program which include a class containing an array of words (strings).The program will search the array for a specific word. if it finds the word:it will return a true value.if the array does not contain the word. it will return a false value. enhancements: make the array off words dynamic, so that the use is prompter to enter the list of words. make the word searcher for dynamic, so that a different word can be searched for each...

  • Python 3.7 Coding assignment This Program should first tell users that this is a word analysis...

    Python 3.7 Coding assignment This Program should first tell users that this is a word analysis software. For any user-given text file, the program will read, analyze, and write each word with the line numbers where the word is found in an output file. A word may appear in multiple lines. A word shows more than once at a line, the line number will be only recorded one time. Ask a user to enter the name of a text file....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT