Answer to Question #225547 in Python for binnu

Question #225547

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




1
Expert's answer
2021-08-12T16:28:56-0400
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")

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