From the course: Python Data Analysis

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Solution: Palindromes

Solution: Palindromes - Python Tutorial

From the course: Python Data Analysis

Start my 1-month free trial

Solution: Palindromes

(upbeat music) - [Narrator] Here is my solution to the challenge. You were asked to find all palindromic pairs of words in the English dictionary. We start by loading our list of words with the one line of comprehension. We will explore the fact that if two words are palindromic, then they are also anagrams of each other. We will also need the code we wrote in this chapter to compute signatures and to associate words to signatures. In Python, there is no built-in function or method to reverse a string, but we can achieve it easily by slicing. The slicing step will need to be negative backwards. We will omit the slice start and stop to get the entire string. So for Mickela, we do a slice of colon, colon, minus one. We now look over all the word sets, one for each signature, and then overall pairs of words in the word set, checking if one of them equals the reverse of the other. It's a little annoying to write the loops,…

Contents