Two Words Combination
Given a sentence as input, print all the unique combinations of two words in lexicographical order.Input
The input will be a single line containing a sentence.Output
The output should be multiple lines, each line containing the unique combination of two words in lexicographical order.Explanation
For example, if the given sentence is "raju plays cricket", the possible unique combination of two are (cricket, plays), (cricket, raju), (plays, raju). So the output should be
cricket plays
cricket raju
plays raju
Sample Input 1
raju plays cricket
Sample Output 1
cricket plays
cricket raju
plays raju
Sample Input 2
python is a programming language
Sample Output 2
a is
a language
a programming
a python
is language
is programming
is python
language programming
language python
programming python
s = input()
w1 = s.split()
wr = []
wa =[]
for i in range(len(w1)-1):
wa.append(sorted([w1[i],w1[i+1]]))
for i in range(len(w1)):
for j in range(i+2,len(w1)):
w = sorted([w1[i],w1[j]])
if w not in wr and w not in wa :
wr.append(w)
if wr:
for item in sorted(wr):
print(*item)
else:
print("No Combinations")
Comments
Leave a comment