4
$\begingroup$

I'm trying to find the palindromes in the dictionary, but my output isn't giving me all of them.

I have this so far

Palindrome[] := DictionaryLookup[y_ /; (y == StringReverse[y])]
Palindrome[]

And my the output just it gives me {a,I}. What's wrong with my input? I don't see it.


Thanks.

$\endgroup$
7
  • 1
    $\begingroup$ Two underscores after your y should help $\endgroup$ Oct 22, 2013 at 0:37
  • 2
    $\begingroup$ Palindrome[] := DictionaryLookup[y__ /; (y == StringReverse[y])] $\endgroup$ Oct 22, 2013 at 0:38
  • $\begingroup$ Ah okay thanks. Why did I need two underscores? I read the reference on wolfram, but I'm not sure how it applies here. $\endgroup$
    – jas
    Oct 22, 2013 at 0:51
  • $\begingroup$ A single underscore is for a single character, and double underscore is for one or more. $\endgroup$ Oct 22, 2013 at 0:55
  • $\begingroup$ So by character, you mean the double underscore is applied to the y and that would give me more than a one letter word? $\endgroup$
    – jas
    Oct 22, 2013 at 0:58

2 Answers 2

11
$\begingroup$

As mentioned in the comments, to search for multi-letter palindromes, two underscores are needed as described in the help for string patterns:

Palindrome[] := DictionaryLookup[y__ /; (y == StringReverse[y])]

Now, Mathematica is probably not good enough to come up with lyrical palindromes.

$\endgroup$
0
5
$\begingroup$

Mathematica 10.3 introduced the function PalindromeQ, which can be utilized here

palindrome[] := DictionaryLookup[y__ /; PalindromeQ[y]]

palindrome[]
{"a", "aha", "aka", "bib", "bob", "boob", "bub", "CFC", "civic", "dad", "deed", "deified",
 "did", "dud", "DVD", "eke", "ere", "eve", "ewe", "eye", "gag", "gig", "huh", "I", 
 "kayak", "kook", "level", "ma'am", "madam", "mam", "MGM", "minim", "mom", "mum", "nan", 
 "non", "noon", "nun", "oho", "pap", "peep", "pep", "pip", "poop", "pop", "pup", "radar", 
 "redder", "refer", "repaper", "reviver", "rotor", "sagas", "sees", "seres", "sexes", 
 "shahs", "sis", "solos", "SOS", "stats", "stets", "tat", "tenet", "TNT", "toot", "tot", 
 "tut", "wow", "WWW"}

Unfortunately this is much slower (≈ 10 times slower) than using

DictionaryLookup[y__ /; y === StringReverse[y]]

but can be also used with the option IgnoreCase -> True:

DictionaryLookup[y__ /; PalindromeQ[y, IgnoreCase -> True]]
{"a", "Abba", "Ada", "aha", "aka", "Ana", "Anna", "Ava", "bib", "bob", "Bob", "boob", 
 "bub", "CFC", "civic", "dad", "deed", "deified", "did", "dud", "DVD", "Ede", "eke", 
 "ere", "eve", "Eve", "ewe", "eye", "gag", "gig", "Hannah", "huh", "I", "kayak", "kook", 
 "Laval", "level", "ma'am", "madam", "Malayalam", "mam", "MGM", "minim", "mom", "mum", 
 "nan", "Nan", "non", "noon", "nun", "oho", "Ono", "Otto", "pap", "peep", "pep", "pip", 
 "poop", "pop", "pup", "radar", "redder", "refer", "repaper", "reviver", "rotor", "sagas",
 "Salas", "sees", "seres", "sexes", "shahs", "sis", "solos", "SOS", "stats", "stets", 
 "tat", "tenet", "Tet", "Tevet", "TNT", "toot", "tot", "tut", "Tut", "wow", "WWW"}

However, for this using

DictionaryLookup[y__ /; ToLowerCase@y === StringReverse[ToLowerCase@y]]

is much faster.

$\endgroup$
3
  • $\begingroup$ interesting. why does the official implementation perform worse than the user-defined one? $\endgroup$
    – sunt05
    Oct 16, 2015 at 5:18
  • $\begingroup$ @sunt05 PalindromeQ has additional overheads, especially for strings. $\endgroup$
    – Karsten7
    Oct 16, 2015 at 5:24
  • $\begingroup$ @sunt05 and it seems, many new functions, at least at first, perform worse, than what could be user-defined in previous versions. One example is ContainsAll and related functions. $\endgroup$
    – LLlAMnYP
    Oct 16, 2015 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.