Question

In: Computer Science

THONNY: This project focusses on dealing with strings of text. The starting point for this is...

THONNY: This project focusses on dealing with strings of text. The starting point for this is a variable, ‘text’, that contains a random collection of words. The aim of this assignment is to iterate over the words and find the length of the longest word.

text = ("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam rhoncus facilisis consequat. "
"Nam ultricies quis dolor vitae placerat. Integer in ante sit amet eros egestas porttitor. Phasellus "
"semper lectus dapibus volutpat consequat. Duis gravida sit amet ipsum eget maximus. Mauris condimentum "
"sem porta, aliquet mi ut, fringilla erat. Nunc maximus, magna ac volutpat mattis, tellus mi dignissim "
"dolor, vel rutrum nulla risus quis quam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices "
"posuere cubilia Curae; Integer ut urna nunc. Morbi id iaculis augue. "
"Proin malesuada scelerisque massa et tristique. Sed blandit nisi nunc, nec euismod nulla dignissim eu. "
"Pellentesque consequat mauris eu augue blandit, quis consectetur urna convallis. Phasellus eget molestie "
"libero, ac hendrerit eros. Quisque ut tellus sed metus finibus dictum. Nullam rhoncus, purus ac finibus "
"placerat, sapien sapien maximus nunc, a tempor lorem mauris eu eros. Integer gravida est eget lacinia "
"aliquam. Phasellus sem urna, varius ac quam a, fringilla vehicula neque. Duis faucibus purus arcu, ac "
"vehicula mauris lacinia quis. Nunc vel dolor non ex luctus efficitur quis in leo. Sed vehicula lectus sit "
"amet ante commodo luctus. Etiam ac ullamcorper turpis, ac ornare urna. "
"Nulla sit amet posuere eros, eget viverra dolor. Suspendisse potenti. Mauris scelerisque mauris id lectus "
"auctor ultrices. Vivamus sodales malesuada erat, in ornare felis auctor id. Curabitur at quam pellentesque, "
"pharetra sem a, pulvinar magna. Integer nisl mi, rhoncus eget urna eget, posuere accumsan risus. In nisi est, "
"condimentum vel ornare sit amet, semper ut nunc. Nam risus leo, maximus eu aliquet non, fringilla id ligula. In "
"metus ipsum, gravida in lectus eget, aliquam egestas ipsum. Fusce placerat cursus mi ultrices luctus. Maecenas eu "
"ipsum diam. Nullam nec augue a diam rhoncus lobortis at id sapien. Ut in erat laoreet, porta nunc quis, luctus "
"justo. Pellentesque dictum sit amet mauris et sodales. Morbi fermentum sem hendrerit, hendrerit ex eu, laoreet sem. "
"Nam at tristique quam. Aliquam erat volutpat. Quisque rhoncus augue a luctus hendrerit. Pellentesque at eros "
"ut nulla volutpat ullamcorper. Curabitur varius nisl hendrerit est rhoncus suscipit. Duis gravida erat sit "
"amet nunc maximus laoreet. Cras dictum quam eu pellentesque commodo. Phasellus non mauris non dolor tincidunt "
"ultrices et in eros. Aenean quis enim mollis, aliquet lorem a, commodo leo. Phasellus elementum quis quam "
"eget condimentum. Nam convallis, enim id faucibus ornare, augue erat rhoncus eros, at vehicula lacus mi sit amet "
"leo. Mauris libero mi, commodo blandit consectetur et, bibendum id magna. In hac habitasse platea dictumst. "
"Sed in diam ut neque pretium gravida. "
"Nullam venenatis elit nec quam lobortis, nec rutrum justo ullamcorper. Pellentesque eleifend lacus elit, "
"nec gravida purus lacinia in. Cras imperdiet, dui in elementum feugiat, est mauris dignissim quam, in facilisis "
"purus lorem vel velit. Suspendisse potenti. Duis a venenatis enim. Vestibulum vitae neque odio. Ut eget ex at "
"libero vehicula faucibus. "
"Integer placerat lobortis sem, a ullamcorper nunc pharetra quis. Integer id consequat mauris, ac rhoncus "
"purus. Nunc sit amet sodales ante, eget rutrum odio. Nullam elementum pretium velit et facilisis. Duis "
"porttitor quam a pharetra ullamcorper. Donec sollicitudin blandit sem, ut rhoncus leo feugiat in. Morbi quis "
"dui a massa sagittis mattis. Curabitur malesuada, eros nec pharetra fermentum, diam nulla euismod nulla, "
"rutrum hendrerit mi nisi feugiat est. Vivamus diam est, iaculis et orci non, fringilla hendrerit nulla. Etiam "
"consectetur eros libero, a ultrices magna blandit quis. Proin eu augue vel ante convallis viverra et sodales "
"magna. Nullam ullamcorper arcu at justo lobortis, at malesuada risus gravida. Curabitur sapien est, tristique "
"in varius eget, consequat a leo. ")

Solutions

Expert Solution

GIVEN AIM: Aim of this assignment is to iterate over the words and find the length of the longest word.

If we look at the data given in the variable text we can see that it has sentences, commas and full stop. So let's read the sentences first in one go, which will convert all the text in a single string, Further we'll remove the commas and full stop because it should not be counted in the length of the word. At last we'll split the whole date with space as a delimiter.

Once we are done with the above processing we are ready for applying the logic to get the length of the longest word. Please note that here we'll just get the length for the longest word, not all the words which are of longest size. Below are all the steps and codes for the same.

NOTE: Python is used for the implementation of the assignment.

STEP 1 - Reading the data : Closed the data in square brackets to have a list format. NOTE: all text data will be at index 0 of the list named "text".

text = ["Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam rhoncus facilisis consequat. "
"Nam ultricies quis dolor vitae placerat. Integer in ante sit amet eros egestas porttitor. Phasellus "
"semper lectus dapibus volutpat consequat. Duis gravida sit amet ipsum eget maximus. Mauris condimentum "
"sem porta, aliquet mi ut, fringilla erat. Nunc maximus, magna ac volutpat mattis, tellus mi dignissim "
"dolor, vel rutrum nulla risus quis quam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices "
"posuere cubilia Curae; Integer ut urna nunc. Morbi id iaculis augue. "
"Proin malesuada scelerisque massa et tristique. Sed blandit nisi nunc, nec euismod nulla dignissim eu. "
"Pellentesque consequat mauris eu augue blandit, quis consectetur urna convallis. Phasellus eget molestie "
"libero, ac hendrerit eros. Quisque ut tellus sed metus finibus dictum. Nullam rhoncus, purus ac finibus "
"placerat, sapien sapien maximus nunc, a tempor lorem mauris eu eros. Integer gravida est eget lacinia "
"aliquam. Phasellus sem urna, varius ac quam a, fringilla vehicula neque. Duis faucibus purus arcu, ac "
"vehicula mauris lacinia quis. Nunc vel dolor non ex luctus efficitur quis in leo. Sed vehicula lectus sit "
"amet ante commodo luctus. Etiam ac ullamcorper turpis, ac ornare urna. "
"Nulla sit amet posuere eros, eget viverra dolor. Suspendisse potenti. Mauris scelerisque mauris id lectus "
"auctor ultrices. Vivamus sodales malesuada erat, in ornare felis auctor id. Curabitur at quam pellentesque, "
"pharetra sem a, pulvinar magna. Integer nisl mi, rhoncus eget urna eget, posuere accumsan risus. In nisi est, "
"condimentum vel ornare sit amet, semper ut nunc. Nam risus leo, maximus eu aliquet non, fringilla id ligula. In "
"metus ipsum, gravida in lectus eget, aliquam egestas ipsum. Fusce placerat cursus mi ultrices luctus. Maecenas eu "
"ipsum diam. Nullam nec augue a diam rhoncus lobortis at id sapien. Ut in erat laoreet, porta nunc quis, luctus "
"justo. Pellentesque dictum sit amet mauris et sodales. Morbi fermentum sem hendrerit, hendrerit ex eu, laoreet sem. "
"Nam at tristique quam. Aliquam erat volutpat. Quisque rhoncus augue a luctus hendrerit. Pellentesque at eros "
"ut nulla volutpat ullamcorper. Curabitur varius nisl hendrerit est rhoncus suscipit. Duis gravida erat sit "
"amet nunc maximus laoreet. Cras dictum quam eu pellentesque commodo. Phasellus non mauris non dolor tincidunt "
"ultrices et in eros. Aenean quis enim mollis, aliquet lorem a, commodo leo. Phasellus elementum quis quam "
"eget condimentum. Nam convallis, enim id faucibus ornare, augue erat rhoncus eros, at vehicula lacus mi sit amet "
"leo. Mauris libero mi, commodo blandit consectetur et, bibendum id magna. In hac habitasse platea dictumst. "
"Sed in diam ut neque pretium gravida. "
"Nullam venenatis elit nec quam lobortis, nec rutrum justo ullamcorper. Pellentesque eleifend lacus elit, "
"nec gravida purus lacinia in. Cras imperdiet, dui in elementum feugiat, est mauris dignissim quam, in facilisis "
"purus lorem vel velit. Suspendisse potenti. Duis a venenatis enim. Vestibulum vitae neque odio. Ut eget ex at "
"libero vehicula faucibus. "
"Integer placerat lobortis sem, a ullamcorper nunc pharetra quis. Integer id consequat mauris, ac rhoncus "
"purus. Nunc sit amet sodales ante, eget rutrum odio. Nullam elementum pretium velit et facilisis. Duis "
"porttitor quam a pharetra ullamcorper. Donec sollicitudin blandit sem, ut rhoncus leo feugiat in. Morbi quis "
"dui a massa sagittis mattis. Curabitur malesuada, eros nec pharetra fermentum, diam nulla euismod nulla, "
"rutrum hendrerit mi nisi feugiat est. Vivamus diam est, iaculis et orci non, fringilla hendrerit nulla. Etiam "
"consectetur eros libero, a ultrices magna blandit quis. Proin eu augue vel ante convallis viverra et sodales "
"magna. Nullam ullamcorper arcu at justo lobortis, at malesuada risus gravida. Curabitur sapien est, tristique "
"in varius eget, consequat a leo. "]

STEP 2 - Removing commas and full stops & splitting with space as delimiter.  NOTE: All data is at index 0 of list names "text". Saving all the words in list format in a variable named "words".

words = text[0].replace(",","").replace(".","").split(" ")

STEP 3 - Applying the logic: Assuming variable index is the index for the longest length word. Now iterating over all the words and comparing if the word in index is longer or shorter. If shorter than assigning current index in variable index.

index = 0
for i in range(len(words)):
    if(len(words[index]) < len(words[i])):
        index = i

Now lets print the length of the longest word.

print("Length of longest word is :",len(words[index]))

Below is the output for the above line of code:

With this, we have completed the aim of the assignment. I hope you got the logic to go ahead with the problem. Thanks & have a nice day!

Below, I have attached all the screenshots for the codes used above.


Related Solutions

Design a text-based game. The game should involve starting from some point in the universe and...
Design a text-based game. The game should involve starting from some point in the universe and seeking or exploring. For example, you could start at the mouth of a cave and seek gold in the halls and caverns. Write out the logic in English and/or pseudocode. Write several lines of JavaScript code that will start the game going, containing control flow statement(s).
In this assignment we will be working with both text strings and a user input integer...
In this assignment we will be working with both text strings and a user input integer array from the data segment. The goal of the assignment is to have the user choose the number of items that they want to have in an array (we will keep it a small number for now, 1-10) and then enter those values which we will store into our array. We then want to allow them to search for a specific item in the...
Lab Text Manipulation Inside the main method, do the following: Create an ArrayList of strings and...
Lab Text Manipulation Inside the main method, do the following: Create an ArrayList of strings and call it parks. Read in the names of national parks from the user until the user enters done(or DONE, or dOnE, .. ) Keep in mind, that the names of some national parks consist of more than one word, for example, Mesa Verde. As you read in the national parks, add them to the list. Next, we are going to build a string based...
________ is the molecule that is the starting point for aerobic and anaerobic respiration.
________ is the molecule that is the starting point for aerobic and anaerobic respiration.
Project 1 - Arrays - Grader The purpose of this assignment is to practice dealing with...
Project 1 - Arrays - Grader The purpose of this assignment is to practice dealing with arrays and array parameters. Arrays are neither classes nor objects. As a result, they have their own way of being passed as a parameter and they also do not support the dot operator ( .), so you cannot determine how full they are. This results in some pain and suffering when coding with arrays. It is this awareness that I am trying to get...
1. discuss the government's role in dealing with inflation. 2. Evaluate the difference s between starting...
1. discuss the government's role in dealing with inflation. 2. Evaluate the difference s between starting a new business from scratch and buying an existing business.
r = range(10, 40, 3) def print_lv(strings): strings = strings if isinstance(strings, list) else [strings] for...
r = range(10, 40, 3) def print_lv(strings): strings = strings if isinstance(strings, list) else [strings] for string in strings: st = "f'" + string + ": {" + string + "}'" print_stmt = 'print(' + st + ', end="; ")' # Uncomment the following print statement to see the statement to be executed. # Each will appear on a separate line. # print(f'\n{print_stmt}') exec(print_stmt) print() print_lv(['list(r)', 'r[-2:3:-1]', 'list(r[-2:3:-1])']) OUTPUT: list(r): [10, 13, 16, 19, 22, 25, 28, 31, 34, 37];...
The text defines the Malthusian trap as: “A point at which the world is no longer...
The text defines the Malthusian trap as: “A point at which the world is no longer able to meet the food requirements of the population, and starvation becomes the primary check to population growth.” In other words, population will outpace food production. Discuss whether you believe this trap has been avoided for the next 100 years. Consider government policies (like China’s one-child policy), the use of genetically engineered crops, social patterns of family size, and environmental factors.
The text defines the Malthusian trap as: “A point at which the world is no longer...
The text defines the Malthusian trap as: “A point at which the world is no longer able to meet the food requirements of the population, and starvation becomes the primary check to population growth.” In other words, population will outpace food production. Discuss whether you believe this trap has been avoided for the next 100 years. Consider government policies (like China’s one-child policy), the use of genetically engineered crops, social patterns of family size, and environmental factors. Please answer in...
The text defines the Malthusian trap as: “A point at which the world is no longer...
The text defines the Malthusian trap as: “A point at which the world is no longer able to meet the food requirements of the population, and starvation becomes the primary check to population growth.” In other words, population will outpace food production. Discuss whether you believe this trap has been avoided for the next 100 years. Consider government policies (like China’s one-child policy), the use of genetically engineered crops, social patterns of family size, and environmental factors.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT