Answer to Question #197008 in Python for Hari nadh babu

Question #197008

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.


Output

The output should be multiple lines, each line containing a valid unique combination of two words. The words in each line should be lexicographical order and the lines as well should be in lexicographical order. A valid combination will not contain the words that appear adjacent in the given sentence. Print "No Combinations" if there are no valid combinations.


Test case 1

Input

raju always plays cricket

Output

always cricket

cricket raju

plays raju


Test case 2

Input

python is a programming language

Output

a language

a python

is language

is programming

language python

programming python


Test case3

Input

to be or not to be

Output

be be

be not

or to

to to


We need all three test cases can be came when code was run. I want exact outputs for all test cases


1
Expert's answer
2021-05-23T08:49:50-0400
sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)

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