In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to use Python to judge whether an integer is a palindrome number". In daily operation, I believe many people have doubts about how to use Python to judge whether an integer is a palindrome number. Xiaobian consulted all kinds of data and sorted out a simple and easy-to-use method of operation. I hope it will be helpful for everyone to answer the question of "how to use Python to judge whether an integer is a palindrome number". Next, please follow the editor to study!
Method 1: judge bit by bit
Principle: use a while loop to take out the first and last bits of a number every time to determine whether they are equal, as long as there is an unequal exit.
The judgment condition of palindromes: add a variable number of digits, if the number is odd, the number of digits is 1, that is, the middle digit, you can exit at this time, similarly, even, when the digit is 0, exit.
Question:
How to judge the number of bits
How to take the value bit by bit
Pros: simple thinking
Resolve:
To determine the number of digits, just follow the program.
Yellowx weishu=0 while x: weishu+=1 x=x//10
First, assign the judged x to y. When x is not 0, it shows that x still has digits, and the digit + 1 is reduced by 10. But remember, use the floor division (/ /) to get int integers directly, otherwise floating-point numbers will appear.
Bit by bit value:
A=y// (10 palindromes * (weishu-1)) Backy if aquired numbers: print ("not palindromes.") Break weishu-=2 y=y//10 yearly% (10**weishu)
Get the first place as long as the floor is divided by the corresponding number of digits, and the number after the first bit is excluded, for example, a five-digit, the floor divided by 10000 (10 to the fourth power, so it is the digit-1), you will get the first place, and the end is relatively simple. As long as you get the remainder of the 10 module, an is the first, b is the last.
After judging the equality between the beginning and the end, first put the number of digits-2, then delete all the numbers, delete the last bit / / 10, delete the first digit: if it is five digits, now delete the last digit is four digits, the floor and 1000 can be modeled.
Then it is to judge how to judge the exit cycle.
As mentioned above, you can judge the last value of the digits.
Finally, you can judge the negative number and the single digit.
The implementation code is as follows:
X=int (input ("Please enter an integer:") if xhou): hou=hou*10+x x _ if x==hou or _ (10) _ (hou//10): print ("is the palindromes.") Else: print ("not palindromes.")
Finally, it is judged that the negative number and the number whose last digit is 0 but not 0, they must not be palindromes, because the beginning cannot be 0.
The number of digits this program can judge, the number behind is this number, the floor except 10 is the same as before 10, that is, palindromes.
Final procedure:
X=int (input ("Please enter an integer:") if xhou): hou=hou*10+x x _ if x==hou or _ (10) _ (hou//10): print ("is the palindromes.") Else: print ("not palindromes.") Method 3: string processing
Principle: a string is actually a sequence, we just need to reverse the string to determine whether it is equal to the original string.
Reversal can be done with x [::-1]
The implementation code is as follows
X=input ("Please enter an integer:") if xintfolx [::-1]: print ("is the palindromes.") Else: print ("not palindromes.")
If you say it can be more advanced, you can also write it in a conditional expression.
X=input ("Please enter an integer:") print ("is the palindromes.") If Xerox [::-1] else print ("not palindromes.")
But if he gives you a 09890, he will also report an error. The specific solution can be first converted into int, and then into str.
The final product:
X=int (input ("Please enter a positive integer:") x=str (x) print ("is the palindromes.") If Xerox [::-1] else print ("not palindromes.") At this point, the study on "how to use Python to judge whether an integer is a palindrome number" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.