Hi sir for the question:
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.
you answered like this:
((https://www.assignmentexpert.com/homework-answers/programming-and-computer-science/python/question-177142))
here in the solution we are inserting "be be" at index 0, but if we give other input, then also "be be" will be the first line of output right sir?
So can you please verify and answer it again?
s = input()
l = s.split()
ans = []
buzz = []
d = {}
for i in range(len(l)):
foo = []
for j in range(len(l)):
if l[i]==l[j]:
if j+1<len(l): foo.append(l[j+1])
if j-1>=0: foo.append(l[j-1])
d[l[i]] = foo
for i in range(len(l)):
for j in range(len(l)):
if l[j] not in d[l[i]] and i!=j:
temp = [l[j], l[i]]
temp.sort()
buzz.append(' '.join(temp))
buzz = set(buzz)
buzz = list(buzz)
buzz.sort()
for i in buzz:
print(i)
Sample Input 1:
raju always plays cricket
Sample Output 1:
always cricket
cricket raju
plays raju
Sample Input 2:
python is a programming language
Sample Output 2:
a language
a python
is language
is programming
language python
programming python
Comments
Leave a comment