AI & Python: Checking the White Whale

What if I told you there are two poems about Moby Dick where both poems are palidromes, and the two poems are anagrams?

It’s called The White Whale by Anthony Etherin.

Palidromes, sure. Any Santa at NASA can write palidromes. But how can we be sure that they are indeed anagrams? Print them out and cross off each letter? Each poem is 500 some odd characters. That could take upwards of 5 minutes! (I tried it, and in 5 minutes, I got to “I rage.”) So let’s use Python instead, and let ChatGPT tell me the code to use.

Strategy

Anagrams have the same letters, just rearranged. To check that each poem has the same letters by hand, I stored the first letter (and eventually chunked by a syllable of 3 letters or a small word) and searched the other poem for that letter. With Python, let’s sort each poem alphabetically, then subtract one from the other. If there’s anything left, they aren’t anagrammatic.

Data Prep

I uploaded the jpg above to an online OCR converter and got a plain text file, and made that into two text files: moby1.txt and moby2.txt with only each poem, respectively, without the title, but with punctuation.

Prompts

  1. First, I’ll import the text files.

2. Once the strings are stored, they can be sorted.

Close, but it’s keeping the words and sorting those. I’ll remove “words” from the prompt.

There we go: it returns just the letters, sorted alphabetically. Now, subtract one from the other.

Putting it all together

Pasting all the sample code together, changing filenames and whatnot, and some trial runs, I get this:

Are they anagrams?

They are! Are they palindromes?


Previous
Previous

Red Curly Tail

Next
Next

3D Printed Oyster Shell