Skip to content

Instantly share code, notes, and snippets.

@rendall
Last active October 1, 2020 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rendall/ab537cb8b3b301bb9b72b4e9c5c5d7da to your computer and use it in GitHub Desktop.
Save rendall/ab537cb8b3b301bb9b72b4e9c5c5d7da to your computer and use it in GitHub Desktop.
isPalindrome(str) returns true if str is a palindrome
// Use it like:
// isPalindrome("Anna") // true
// isPalindrome("not a palindrome") // false
const isPalindrome = (p, normalized) =>
normalized === undefined?
isPalindrome(null, p.replace(/\W/g,"").toLowerCase())
: normalized.length <= 1? true
: normalized.slice(-1) === normalized.slice(0,1)?
isPalindrome(null, normalized.slice(1,normalized.length-1))
: false
// isPalindrome first strips 'p' of symbols, spaces and converts to lowercase
// return true if 'normalized' is 1 or 0 letters which are trivial palindromes
// rejects if 'normalized' first and last letters are not the same
// because this means 'normalized' (and 'p') are not palindromes
// recurses, passing 'normalized' with first and last letters removed
// Test
["A but tuba.",
"A car, a man, a maraca.",
"A dog, a plan, a canal: pagoda.",
"A dog! A panic in a pagoda!",
"A lad named E. Mandala",
"A man, a plan, a canal: Panama.",
"A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal-Panama!",
"A new order began, a more Roman age bred Rowena.",
"A nut for a jar of tuna.",
"A Santa at Nasa.",
"A Santa dog lived as a devil God at NASA.",
"A slut nixes sex in Tulsa.",
"A tin mug for a jar of gum, Nita.",
"A Toyota! Race fast, safe car! A Toyota!",
"A Toyota’s a Toyota.",
"Able was I ere I saw Elba.",
"Acrobats stab orca.",
"Aerate pet area.",
"Ah, Satan sees Natasha!",
"Air an aria.",
"Al lets Della call Ed Stella.",
"alula",
"Amen icy cinema.",
"Amore, Roma.",
"Amy, must I jujitsu my ma?",
"Ana",
"Animal loots foliated detail of stool lamina.",
"Anna",
"Anne, I vote more cars race Rome to Vienna.",
"Are Mac ‘n’ Oliver ever evil on camera?",
"Are we not drawn onward to new era?",
"Are we not drawn onward, we few, drawn onward to new era?",
"Are we not pure? “No sir!” Panama’s moody Noriega brags. “It is garbage!” Irony dooms a man; a prisoner up to new era.",
"Art, name no tub time. Emit but one mantra.",
"As I pee, sir, I see Pisa!",
"Avid diva."].every(p => isPalindrome(p))
// A but tuba. => true
// A car, a man, a maraca. => true
// A dog, a plan, a canal: pagoda. => true
// A dog! A panic in a pagoda! => true
// A lad named E. Mandala => true
// A man, a plan, a canal: Panama. => true
// A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal-Panama! => true
// A new order began, a more Roman age bred Rowena. => true
// A nut for a jar of tuna. => true
// A Santa at Nasa. => true
// A Santa dog lived as a devil God at NASA. => true
// A slut nixes sex in Tulsa. => true
// A tin mug for a jar of gum, Nita. => true
// A Toyota! Race fast, safe car! A Toyota! => true
// A Toyota’s a Toyota. => true
// Able was I ere I saw Elba. => true
// Acrobats stab orca. => true
// Aerate pet area. => true
// Ah, Satan sees Natasha! => true
// Aibohphobia (fear of palindromes) => false
// Air an aria. => true
// Al lets Della call Ed Stella. => true
// alula => true
// Amen icy cinema. => true
// Amore, Roma. => true
// Amy, must I jujitsu my ma? => true
// Ana => true
// Animal loots foliated detail of stool lamina. => true
// Anna => true
// Anne, I vote more cars race Rome to Vienna. => true
// Are Mac ‘n’ Oliver ever evil on camera? => true
// Are we not drawn onward to new era? => true
// Are we not drawn onward, we few, drawn onward to new era? => true
// Are we not pure? “No sir!” Panama’s moody Noriega brags. “It is garbage!” Irony dooms a man; a prisoner up to new era. => true
// Art, name no tub time. Emit but one mantra. => true
// As I pee, sir, I see Pisa! => true
// Avid diva. => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment