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