Menu

[Solved]L7 6 Printwordspy Starting Code Opens Reads Text File Words2txt Single String Unlike Words Q37300810

[L7-6] (print_words.py) Thestarting code opens and reads the text filewords_2.txt into a single string. Unlikewords.txt, this file contains words containingsingle quotes ().

Do each of the following by completing the starting functions(names given) to compute and print the requested information. Thefirst function will return a list of all words, which is thenpassed as an argument to the remaining functions for computing andprinting. Note that usually, we should return values from afunction instead of printing them out directly within it, thusincreasing the reusability of our code.

(a) (defsplit_into_words(bstring)) Split the single string into alist all_words. Then print out its length andreturn the entire list. Use str‘ssplit() method.

(b) (defprint_longest_1(all_words)) Iterate over each elementall_words, finding and printing the longest word;if more than one word has the same length, print any one of them.Hint: use an accumulator pattern, tracking both the longest word sofar and length of such a word. Better: try to print out all longestwords (those tying for longest length), by accumulating a list oflongest-so-far words.

(c) (defprint_longest_2(all_words)) Do (b) another way: build alist len_list containing tuples(length(word),word) for each word, sort it usinglist‘s sort() method, then printthe last element.

(d) (defprint_first_2_equals_last_2(all_words)) Print out allwords whose first 2 characters are the same as thelast 2. Hint: use slices and be careful about thelengths of strings.

Expert Answer


Answer to [L7-6] (print_words.py) The starting code opens and reads the text file words_2.txt into a single string. Unlike words.t… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *