Skip to content
Econowmics
Menu
  • Home
  • Economics
    • Econometrics
    • Economics and History
    • Macroeconomics
    • Microeconomics
    • Miscellaneous
      • Awards and Honors
      • Economic Schools of Thought
      • Economic Quotes
      • Economic Videos
    • Terms and Concepts
  • Cognitive Biases
  • Data Analysis
    • Statistics
    • Python Programming
  • Contact
Menu

[Python] Palindrome Checking Function

Posted on

One of Lindon’s amusing word-unit palindrome reads: “Girl, bathing on Bikini, eyeing boy, finds boy eyeing bikini on bathing girl.” Other palindromes are symmetric with respect to back-to-front reading letter by letter-“Able was I ere I saw Elba” (attributed jokingly to Napoleon), or the title of a famous NOVA program: “A Man, a Plan, a Canal, Panama.”

Mario Livio, The Equation That Couldn’t Be Solved: How Mathematical Genius Discovered the Language of Symmetry

 

[Python] Palindrome
Palindrome ΝΙΨΟΝ ΑΝΟΜΗΜΑΤΑ ΜΗ ΜΟΝΑΝ ΟΨΙΝ (Nipson anomemata me monan opsin, Wash your sins, not only your face) inscribed in ancient Greek upon a holy water font. The phrase can be read from left to right or from right to left. (Source)

 

Palindromes are words, numbers, sentences or the like that read the same way backward and forward.  For instance, the number 232 is a palindrome, because no matter if you read it forward or backward, it will always be 232. Similarly, the word ‘madam’ is an English palindrome word, because it reads the same way both backward and forward. Another famous English palindrome is ‘Madam, I’m Adam’, with the sentence being the same, no matter how you read it.

The word ‘palindrome’ is derived from the Greek word ‘palin’ (meaning ‘back’) and dromos (meaning ‘direction’). The whole Greek phrase referred to the backward movement of a crab.

This one is my favorite:

‘Are we not pure? “No, sir!” Panama’s moody Noriega brags. “It is garbage!” Irony dooms a man — a prisoner up to new era.’

So, how is it possible to use Python to check if a given word or number is palindrome? Let’s write a function which will help us do so.

 

[pastacode lang=”python” manual=”%22%22%22Econowmics.com%22%22%22%0A%0A%0Adef%20Palindrome(n)%3A%0A%20%20%20%20%22%22%22This%20function%20checks%20to%20see%20if%20the%20given%20entry%20is%20palindrome%20or%20not%22%22%22%0A%0A%20%20%20%20%23The%20original%20entry%20(A%20number%20or%20a%20word)%0A%20%20%20%20Original%20%3D%20str(n)%0A%0A%20%20%20%20%23Reversing%20the%20entry%0A%20%20%20%20Reverse%20%3D%20Original%5B%3A%3A-1%5D%0A%0A%20%20%20%20%23Checking%20to%20see%20if%20the%20two%20are%20equal%0A%20%20%20%20if%20Original%20%3D%3D%20Reverse%3A%0A%20%20%20%20%20%20%20%20return%20True%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20return%20False” message=”” highlight=”” provider=”manual”/]

 

The intuition behind this function is very easy. It just receives an input from the user (which is a word or a number), reverses it and checks to see if it equals its reverse or not. Let’s check it with a couple of different inputs:

 

Related posts:

[Python] Pandigital numbers
Shakespeare's Nightmare: Monkeys on Typewriters
[Python] Binary Search
[Python] Law of Large Numbers: Dice Roll
[Python] Penalty Kicks Simulator

Anything in here will be replaced on browsers that support the canvas element

  • Amos Tversky
  • Daniel Kahneman
  • cognitive bias
  • Milton Friedman
  • Wealth of Nations
  • Nobel Prize Laureate
  • Adam Smith
  • 20:20 ratio
  • law of small numbers
  • the halo effect
  • wealth inequality
  • income inequality
  • Law of Large Numbers
  • Dow Jones Industrial Average
  • status quo bias
  • Behavioral Economics
  • daniel kahnemann
  • hyperinflation
  • the gambler's fallacy
  • gambler's fallacy
©2023 Econowmics | Design: Newspaperly WordPress Theme