From the course: Python Data Analysis (2020)
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Solution: Palindromes - Python Tutorial
From the course: Python Data Analysis (2020)
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,…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.