SoFunction
Updated on 2024-11-13

Example of three ways Python determines the number of palindromes

Demand:

Enter a five-digit number from the console, if it is an iambic pentameter, print "Yes iambic pentameter", otherwise print "No iambic pentameter", e.g.: 11111 12321 12221

"Palindrome" is a sentence that can be read both positively and negatively, and it is a rhetorical style and word game in both ancient and modern times, such as "all for one, one for all" and so on. In mathematics, there is also a class of numbers that have this characteristic, which becomes the palindrome number.

Let n be an arbitrary natural number. If the natural number n1 obtained by reversing the digits of n is equal to n, then n is said to be a palindrome. For example, if n = 1234321, then n is said to be a palindrome; but if n = 1234567, then n is not a palindrome.

Baidu's encyclopedia of palindromes

Analysis:

The input is purely numeric and has a length of 5

The digit is the same as the number in the ten-thousandths place, and the tens digit is the same as the thousands digit.

Method 1: Arithmetic operations

Idea: The arithmetic operations of division and modulo are used to obtain the number of digits in each place, and then to draw conclusions by comparing before and after (individual digits to ten thousand, tens digits to thousands).

a = input('Please enter a five-digit number:')
# Determine if the length is 5 digits and if it consists of pure numbers
if len(a) == 5 and ():
	# Convert str to int
 a = int(a)
 # (million to each digit) and (thousands to tens)
 if (a//10000 == a%10) and (a%10000//1000 == a%100 // 10):
 print(f'{a}is the number of replies')
 else:
 print(f'{a}不is the number of replies')
else:
	print('Input error')
	

Method 2: Index Fetch

Idea: indexing the input string to take values, comparing before and after, and drawing conclusions.

a = input('Please enter a five-digit number:')
# Determine if the length is 5 digits and if it consists of pure numbers
if len(a) == 5 and ():
	# Compare and contrast a[0] and a[4], a[1] and a[3].
	if (a[0] == a[4]) and (a[1] == a[3]):
		print('%f is the number of palindromes' % (a))
	else:
 print('%f is not the number of palindromes' % (a))
else:
	print('Input error')
	
 

Method 3: Slicing in reverse order

Idea: input() function input data type is str, str is ordered, so you can operate on str slices, and take the result of the reverse order, the result of the reverse order and the original str to do a comparison, and draw conclusions.

a = input('Please enter a five-digit number:')
# Determine if the length is 5 digits and if it consists of pure numbers
if len(a) == 5 and ():
	# The result of slicing in reverse order is compared with a
 if a[::-1] == a:
  print(f'{a}is the number of replies')
 else:
  print(f'{a}不is the number of replies')
else:
 print('Input error')

Summary:

Familiarity with the characteristics of each basic data structure can help us solve problems better.

This article on Python to determine the number of replies to the three methods of the article is introduced to this, more related to Python to determine the number of replies to the content please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!