Answer to Question #182449 in Python for adhi chinna

Question #182449

Non-Adjacent Combinations of Two Words

Given a sentence as input, find all the unique combinations of two words and print the word combinations that are not adjacent in the original sentence in lexicographical order.Input


The input will be a single line containing a sentence.Output


The output should be multiple lines, each line containing a valid unique combination of two words. The words in each line should be lexicographical order and the lines as well should be in lexicographical order. A valid combination will not contain the words that appear adjacent in the given sentence. Print "No Combinations" if there are no valid combinations.Explanation


For example, if the given sentence is "python is a programming language",

So the output should be

a language

a python

is language

is programming

language python

programming python

Sample Input 1

raju always plays cricket


Sample Output 1

always cricket

cricket raju

plays raju




1
Expert's answer
2021-04-17T03:08:18-0400
words = list(input().split())
data = []
length = len(words)
for i in range(length):
    for j in range(i+2,length):
        if words[i] < words[j]:
            data.append(words[i] + ' ' + words[j])
        else:
            data.append(words[j] + ' ' + words[i])
for line in sorted(data):
    print(line)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS