Answer to Question #227176 in Python for venkat

Question #227176

non adjacent combination o two words


1
Expert's answer
2021-08-18T06:51:26-0400
def non_adjacent(words:str):



    words_list = words.split()
    result = []
    l = len(words_list)
    for i in range(l-2):
        for j in range(i+2, l):
            tmp1 = words_list[i]
            tmp2 = words_list[j]
            index_tmp1 = [k for k in range(l) if words_list[k]==tmp1]
            index_tmp2 = [m for m in range(l) if words_list[m]==tmp2]
            not_adj = True
            for el1 in index_tmp1:
                for el2 in index_tmp2:
                    if abs(el1 - el2) == 1:
                        not_adj = False
            tmp = sorted([tmp1, tmp2])
            if tmp not in result and not_adj:
                result.append(tmp)
    for el in sorted(result):
        print(*el)

non_adjacent('boys girls children man')
boys children
boys man
girls man

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