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
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)
Comments
Leave a comment