Palindrome Pairs:
Given a list of unique words, write a program to print all the pairs of the distinct indices (i,j) in the given list, so that the concatenation of two words at the indices i and j is a palindrome.
Input: Input will be a single line containing a sentence.
Output: The output should be printing each distinct pair of indices (i,j) each in a line in ascending order. If there are no distinct pairs found, print "-1"
Note: Consider (i,j) and (j,i) as two different pairs.
def indx_of_poly(sentense):
words = sentense.split()
res = []
for i in range(len(words)-1):
for j in range(i+1, len(words)):
tmp = words[i].lower() + words[j].lower()
if tmp == tmp[::-1]:
res.append((i,j))
tmp = words[j].lower() + words[i].lower()
if tmp == tmp[::-1]:
res.append((j,i))
if len(res) == 0:
print(-1)
else:
res = sorted(res, key=lambda s: s[0])
print(*res, sep='\n')
indx_of_poly(input())
Comments
Leave a comment