Answer to Question #280286 in Python for kaavya

Question #280286

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.


1
Expert's answer
2021-12-16T04:45:43-0500
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())

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