Answer to Question #188148 in Python for CHANDRASENA REDDY

Question #188148

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


Sample Input 1

python is a programming language


Sample Output 1

a language

a python

is language

is programming

language python

programming python


Sample Input 2

raju always plays cricket


Sample Output 2

always cricket

cricket raju

plays raju




1
Expert's answer
2021-05-05T05:47:38-0400
words = input().split()
print()

n = len(words)
pairs = []
for i in range(n-2):
    for j in range(i+2, n):
        L = [words[i], words[j]]
        L.sort()
        pairs.append(L)
pairs.sort()

for w1, w2 in pairs:
    print(w1, w2)

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
APPROVED BY CLIENTS