Answer to Question #197161 in Python for Hari nadh babu

Question #197161

In this python code there are three test cases. The three test cases are not coming expected output.

The three test cases are in below link

https://drive.google.com/file/d/1pcEniOexSN8TMkXxXU05FDKoPlYYbLCe/view?usp=sharing

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)
1
Expert's answer
2021-05-25T00:08:28-0400
s = input().split()
n = len(s)


st = set()
for i in range(n):
    for j in range(n):
        if i != j-1 and i != j and i != j+1:
            if i > 0 and s[i-1] == s[j]:
                continue
            if i < n-1 and s[i+1] == s[j]:
                continue
            if j > 0 and s[i] == s[j-1]:
                continue
            if j < n-1 and s[i] == s[j+1]:
                continue
            st.add(s[i]+' '+s[j] if s[i]<s[j] else s[j]+' '+s[i])
            
for item in sorted(st):
    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