Welcome back! I'm happy to report that I "definitely, definitely" solved the latest Sunday Puzzle from NPR. Here's the puzzle, and you can scroll to the bottom of this post for the solution:
This week's challenge comes from listener Matthew Leal of San Francisco. Write down the name of a country plus its capital, one after the other. Hidden in consecutive letters inside this is the name of a film that won an Academy Award for Best Picture. Name the country, capital, and film.
In the preview, I mentioned that we would need:
- CC: a list of countries and capitals;
- I prepared a CSV of countries and capitals, available here;
- BP: a list of Best Picture winners;
You'll need to the files linked above in order to run my solver script, or you may want to use them in your own script. My solver script is available here on the companion GitHub repository for this blog.
As discussed in this week's preview post, my approach works like this:
As usual, we'll need to clean and format our data a little to make it suitable for the kind of comparison we're doing. For each country, capital, and movie, we'll want to lowercase all letters, remove any non-letters (spaces, apostrophes, hyphens). I also like to use a python package called Slugify for this kind of task; slugify converts unicode letters into their nearest ASCII equivalent. This means that "Curaçao" becomes "Curacao", etc. We'll also want to concatenate the country+capital, with no spaces, and we'll want to do the same for all the words in the movie title.For example:
- Domincican Republic, Santo Domingo --> dominicanrepublicsantodomingo
- Côte d'Ivoire, Yamoussoukro --> cotedivoireyamoussoukro
- Ben-Hur --> benhur
- The Shape of Water --> theshapeofwater
Then we simply need to iterate through these lists and see if we find a movie title inside a country+capital string.
- for cstring in countries_capitals:
- for mstring in movies:
- if mstring in cstring:
- print(cstring)
- print(mstring)
Solution:
Bahrain, Manama --> Rain Man
See you for the next puzzle!
--Levi King