non adjacent combination o two words
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
Comments
Leave a comment