Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please solve it by Python 3 Imagine that we have the following list of words: ['Mirror', 'Mirror', 'on', 'the', 'wall'] There are 5 words in

Please solve it by Python 3

Imagine that we have the following list of words:

['Mirror', 'Mirror', 'on', 'the', 'wall']

There are 5 words in this list, with the middle word being 'on'. When the length of the list is an odd number, there is exactly one middle word. If the length is even, on the other hand, then there are two middle words. For example, in the case of:

['Baa', 'baa', 'black', 'sheep', 'have', 'you', 'any', 'wool']

the two middle words are 'sheep' and 'have'.

Your task is to write a function middle_words(word_list) that returns the middle word(s) from the non-empty list-of-strings word_list. If the length of word_list is an odd number, you should return a list containing the single middle word; and if the length of word_list is an even number, you should return a list containing the two middle words, in the same order as they occurred in the word_list. For example:

>>> middle_words(['Mirror', 'Mirror', 'on', 'the', 'wall'])
["on"]
>>> middle_words(['Baa', 'baa', 'black', 'sheep', 'have', 'you', 'any', 'wool'])
["sheep", "have"]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What are the APPROACHES TO HRM?

Answered: 1 week ago