Summarization of three methods to judge whether integers are palindromes by Python

  • 2021-11-14 06:12:45
  • OfStack

Preface

The so-called palindrome number means that the result of reading a number from the left and from the right is 1 module 1, such as 12321. This article introduces the judgment method in detail through three methods. Let's take a look at one below

Method 1: Bit-by-bit judgment

Principle: Use an while cycle, take out the first and last bits of a number every time, and judge whether it is equal, as long as there is an unequal exit.

The judgment condition of palindrome number: add one variable digit number. If this number is odd, when the digit number is 1, it is the most middle digit, and you can exit at this time. Similarly, even, when the digit number is 0, you can exit.

Question:

How to judge the number of digits How to take values bit by bit

Advantages: Simple thinking

Resolve:

Judge the following program of digits


	y=x
    weishu=0
    while x:
        weishu+=1
        x=x//10

First, assign the judged x to y. When x is not 0, it means that there are still digits in x. The digit is +1, and x/10 is reduced by 1 bit. However, remember that the integer of int is directly obtained by dividing (//) by floor, otherwise floating point numbers will appear by using/

Bitwise value:


		a=y//(10**(weishu-1))
        b=y%10
        if a!=b:
            print(" Not palindromes. ")
            break
        weishu-=2
        y=y//10
        y=y%(10**weishu)

To get the first place, as long as the floor is divided by the corresponding digits, the number behind the first place is discarded, for example, a 5-digit, and the floor is divided by 10000 (10 to the fourth power, so it is digital-1), the first place will be obtained, and the end is relatively simple. As long as 10 is modularized, the remainder is all, a is the first place, and b is the last place.

Judge the first and last equal, first digit-2, and then the first and last digits are deleted, delete the last digit//10, delete the first digit: if it is 5 digits, now delete the last digit is 4 digits, the floor and 1000 can be modular

Then it is to judge how to judge the exit loop

As mentioned above, the last value of the number of bits can be judged

Finally, the negative number and single digit can be judged

The implementation code is as follows:


x=int(input(" Please enter 1 Integers: "))
if x<0:
    print(" Not palindromes. ")
elif not x//10:
    print(" It's palindrome number. ")
else:
    y=x
    weishu=0
    while x:
        weishu+=1
        x=x//10
    while True:
        a=y//(10**(weishu-1))
        b=y%10
        if a!=b:
            print(" Not palindromes. ")
            break
        weishu-=2
        if weishu==1:
            print(" It's palindrome number. ")
            break
        if not weishu:
            print(" It's palindrome number. ")
            break
        y=y//10
        y=y%(10**weishu)

Method 2: Get the inverted number judgment

Principle: Every palindrome number is equal before and after cutting from the middle, so the number behind the palindrome number can be compared with the number before it

Then the question is, if it is an odd number, how to judge it?

Solution: As long as the last number obtained before or after the number except the last digit is equal to another one, because the number in the middle of odd digits does not need to be taken care of, and other numbers are equal, so delete the last digit to judge whether it is equal

Difficulty: How to get the following number?

Multiply the following number by 10 each time, then add the last bit of the previous number, and divide the previous number by 10 to eliminate the last bit

How to judge that the following number has been taken, that is, the judgment condition for exiting the cycle is:

That is, when the following number is greater than or equal to the previous number, you can exit the loop

Advantages: Simple code

The implementation procedure is as follows:


	hou=0
    while(x>hou):
        hou=hou*10+x%10
        x//=10
    if x==hou or x==(hou//10):
        print(" It's palindrome number. ")
    else:
        print(" Not palindromes. ")

Finally, it is judged that the negative number and the number whose last bit is 0 but not 0 are not palindrome numbers, because the beginning cannot be 0

Single digit This procedure can judge, the number behind is this single digit, after the floor is divided by 10 and the first sample is 10, that is, palindrome number

Final procedure:


x=int(input(" Please enter 1 Integers: "))
if x<0 or(x%10==0 and x!=0):
    print(" Not palindromes. ")
elif not x//10:
    print(" Not palindromes. ")
else:
    hou=0
    while(x>hou):
        hou=hou*10+x%10
        x//=10
    if x==hou or x==(hou//10):
        print(" It's palindrome number. ")
    else:
        print(" Not palindromes. ")

Method 3: String processing

Principle: A string is actually a sequence, so we just have to reverse the string to determine whether it is equal to the original string

Inversion can be done using x [::-1]

The implementation code is as follows


x=input(" Please enter 1 Integers: ")
if x==x[::-1]:
    print(" It's palindrome number. ")
else:
    print(" Not palindromes. ")

If you say if you can go one point higher, you can also write it with a conditional expression


x=input(" Please enter 1 Integers: ")
print(" It's palindrome number. ") if x==x[::-1] else print(" Not palindromes. ")

However, if he gives you a 09890, he will also report an error. The specific solution can be converted into int first and then into str

Final product:


x=int(input(" Please enter 1 Positive integers: "))
x=str(x)
print(" It's palindrome number. ") if x==x[::-1] else print(" Not palindromes. ")

Summarize


Related articles: