Examples of three methods for judging palindrome number by Python

  • 2021-10-13 08:15:59
  • OfStack

Requirements:

Enter a 5-digit number from the console and print "Yes palindrome number" if it is palindrome number, or "No palindrome number" if it is palindrome number, for example: 11111 12321 12221

"Palindrome" is a sentence that can be read through correct reading and reverse reading. It is a rhetorical device and word game that exists in ancient and modern China and foreign countries, such as "I am for everyone, everyone is for me" and so on. In mathematics, there is also such a class of numbers with such a feature, which is called palindrome number (palindrome number).

Let n be an arbitrary natural number of 1. If the natural number n1 obtained by arranging the digits of n in reverse is equal to n, n is called 1 palindrome number. For example, if n=1234321, n is called 1 palindrome number; But if n=1234567, n is not a palindrome number.

Hundreds of degrees of palindrome encyclopedia

Analysis:

Input is pure number, length is 5

The number of digits is the same as that of ten thousand digits, and the number of digits is the same as that of ten thousand digits

Method 1: Arithmetic operation

Thought: Through the arithmetic operation of divisive and modular calculation, the numbers on each digit are obtained, and then the conclusion is drawn by comparing before and after (comparison between single digit and ten thousand digits, 10 digits and thousands of digits).


a = input(' Please enter 1 A 5 Number of digits: ')
#  Judge whether the length is 5 Bit, whether it is composed of pure numbers 
if len(a) == 5 and a.isdigit():
	#  Will str Convert to int
 a = int(a)
 #  (Ten thousand and everyone)  and  (Thousands and 10 Bits) 
 if (a//10000 == a%10) and (a%10000//1000 == a%100 // 10):
 print(f'{a} Is palindrome number ')
 else:
 print(f'{a} Not palindrome number ')
else:
	print(' Input error ')
	

Method 2: Index value

Idea: Index the value through the input string, compare it before and after, and draw a conclusion.


a = input(' Please enter 1 A 5 Number of digits: ')
#  Judge whether the length is 5 Bit, whether it is composed of pure numbers 
if len(a) == 5 and a.isdigit():
	#  Right a[0] And a[4] , a[1] And a[3] , for comparison 
	if (a[0] == a[4]) and (a[1] == a[3]):
		print('%f Is palindrome number ' % (a))
	else:
 print('%f Not palindrome number ' % (a))
else:
	print(' Input error ')
	
 

Method 3: Slice reverse order

Thought: The type of data input by input () function is str, and str is ordered, so we can slice str and take the result of reverse order, compare the result of reverse order with the original str, and draw a conclusion.


a = input(' Please enter 1 A 5 Number of digits: ')
#  Judge whether the length is 5 Bit, whether it is composed of pure numbers 
if len(a) == 5 and a.isdigit():
	#  The results of reverse slicing and a Make comparisons 
 if a[::-1] == a:
  print(f'{a} Is palindrome number ')
 else:
  print(f'{a} Not palindrome number ')
else:
 print(' Input error ')

Summary:

Familiar with the characteristics of each basic data structure can help us solve the problem better.


Related articles: