Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Playing with Magic Words PYTHON CODE Here a word 'S' of length 'n' is said to be a magic word if it satisfies the following

Playing with Magic Words PYTHON CODE

Here a word 'S' of length 'n' is said to be a magic word if it satisfies the following conditions:

All letters of S are lowercase letters of the English alphabet

Si , the character in the i-th position, is lexicographically smaller than Sn-1-i for all even i from 0 to [n/2]

Si is lexicographically greater than Sn-1-i for all odd i from 0 to [n/2]

(Let us assume that the indices of the letters in S range from 0 to n-1, Si (0 i < n) denotes the ith character of S). For example, the word "difference" is a magic word, while "similar" is not.

Stop words are words that do not give meaning when it stands alone. Stopwords to be considered for this program are {'a', 'an', 'and', 'the', 'this', 'is', 'in','was', 'that', 'this', 'are', 'were'}.

Given a sentence, design an algorithm and write the Python code to do the following:

1. Preprocessing : Remove stop words from the given sentence and check if the remaining words in the sentence are magic words.

2. Make Magic: If a word is not a magic word then convert that word into a magic word by removing the lexicographically large letter, when there is a mismatch

3. Form Matrix: After the operations : preprocessing and Make Magic, all the words in the sentence would be magic words. Determine the minimal length 'L' among the words. Form a matrix of dimension L X L, by taking 'L' letters from each word, row wise.

4. Rotate: Rotate the outermost letters of the matrix, 'L' times in anticlockwise direction. When the rows and columns are numbered from 0 to L-1, the rotation must begin from the element in position (0,0).

5. Count Magic: Print the list of magic words that are formed from each column, considered from top to bottom.

For example, consider a sentence 'order a laptop today'. In this sentence, 'a' is a stop word, order and today are magic but laptop is changed to magic by rules. So the new sentence would be 'order lap today'. The word with minimal length in the sentence is 3 so a 3 X 3 matrix is formed as shown below:

o r d

l a p

t o d

After rotating the matrix to minimal length times:

p d o

d a t

r o l

List of magic words that are formed by combining letters columnwise are ['pdr', 'dao'].

Input Format

A sentence

Output Format

List of magic words that is formed in Step

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

Students also viewed these Databases questions