Answer to Question #186411 in Python for bharath

Question #186411

Given a sentence as input, print all the unique combinations of three 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 three words in lexicographical order.Explanation


For example, if the given sentence is "apple is a fruit", the possible unique combination of three are (a, apple, fruit), (a, apple, is), (a, fruit, is), (apple, fruit, is). So the output should be

a apple fruit
a apple is
a fruit is
apple fruit is

Sample Input 1

apple is a fruit

Sample Output 1

a apple fruit

a apple is

a fruit is

apple fruit is

Sample Input 2

raju plays cricket

Sample Output 2

cricket plays raju




1
Expert's answer
2021-04-27T19:07:17-0400
s = input('Enter your string: ')
words = s.split()
words_uniq =[]
for item in words:
    if item not in words_uniq:
        words_uniq.append(item)
words_uniq.sort()
for i in range(len(words_uniq)):
    for j in range(i+1,len(words_uniq)):
        for z in range(j+1,len(words_uniq)):
            print(words_uniq[i],words_uniq[j],words_uniq[z])

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