The Next Palindrome number

2020-04-01 06:01发布

I am beginner in programming, So can you please tell me what's wrong with my code?

I want to print next palindrome number if the number entered by the user (n) is not palindrome

n = int(input("Enter any number :- "))

reverse = 0
temp = n

while (n!=0):
    reverse = reverse * 10
    reverse = reverse + n%10
    n=n//10 
if(temp==reverse):
    print ("Already palindrome:: ")

if(temp != reverse):
     new_temp = temp
     new_reverse = 0
     for i in range(new_temp,new_temp+10):
        while(temp != 0):
            new_reverse = new_reverse * 10
            new_reverse = new_reverse + temp%10
            temp = temp//10
         if(new_temp==new_reverse):
             print ("Next pallindrome is :- ",new_temp)
             break
         if(new_temp != new_reverse):
             temp = new_temp+1

10条回答
我欲成王,谁敢阻挡
2楼-- · 2020-04-01 06:29

using function to find next palindrome

def nearest_palindrome(number):
if number>0:
    i=1
    while(number):
        n=number+i
        temp=n
        sum=0
        while n>0:
            rem=n%10
            sum=sum*10+rem
            n=n//10
        if temp==sum:

            return temp
            #break
        else:
            sum=0
            i=i+1
        #continue
number=12300
print(nearest_palindrome(number))
查看更多
神经病院院长
3楼-- · 2020-04-01 06:30

I have written this for finding next pallindrome number given a pallindrome number.

def palindrome(num):
    bol=False
    #x=len(str(num))
    num=num+1
    while(bol==False):
        if(check_palindrome(num)):
            bol=True
        else:
            num=num+1
    return num
def check_palindrome(n):
    temp=n
    rev=0
    while(n>0):
        dig=n%10
        rev=rev*10+dig
        n=n//10
    if(temp==rev):
        return True

b=palindrome(8)
print(b)
查看更多
等我变得足够好
4楼-- · 2020-04-01 06:34

There are two problems with your code.

1) Your "for i in range" loop calculates the reverse of the temp variable, but you don't change the temp variable's value. You do

 new_temp = temp
 for i in range(new_temp,new_temp+10):
    [SNIP]
    if(new_temp != new_reverse):
         temp = new_temp+1 #this value never changes.

So you're making 10 iterations with one and the same value.

2) Ten iterations might not be enough to find a palindrome. Keep going until you find a palindrome.

Working code:

def reverse(num):
    reverse= 0
    while num:
        reverse= reverse*10 + num%10
        num= num//10
    return reverse

num= int(input("Enter any number :- "))
if num==reverse(num):
    print ("Already palindrome.")
else:
    while True:
        num+= 1
        if num==reverse(num):
            print ("Next palindrome is : %s"%num)
            break
查看更多
ら.Afraid
5楼-- · 2020-04-01 06:35
def next_palin_drome(n):
    while True:
        n+=1
        if str(n) == str(n)[::-1]:
            return n 

n=12231
print(next_palin_drome(n))

output:12321

查看更多
该账号已被封号
6楼-- · 2020-04-01 06:41

If it helps, I believe it's possible to solve this problem with n/2 iterations where n is the length of the input number. Here's my solution in Python:

def next_palin_number(number):
    # Convert the number to a list of its digits.
    number = list(str(number))
    # Initialize two indices for comparing symmetric digits.
    i = 0
    j = len(number) - 1
    while i < j:
        # If the digits are different:
        if number[i] != number[j]:
            # If the lower-power digit is greater than the higher-power digit:
            if int(number[j]) > int(number[i]):
                number[j - 1] = str(int(number[j - 1]) + 1)
                number[j] = number[i]
            else:
                number[j] = number[i]
        i += 1
        j -= 1
    # Concatenate and return the result.
    return "".join(number)
查看更多
Root(大扎)
7楼-- · 2020-04-01 06:42

This problem has a wonderful number of ways to solve them.

One of them is

def nearest_palindrome(number):
    #start writitng your code here
    while True:
        number+=1
        if str(number) == str(number)[::-1]:
            return number 

number=12300
print(nearest_palindrome(number))

Thanks for your time to read my answer : )

查看更多
登录 后发表回答