4

So my task is to see and check a positive integer if its a palindrome. I've done everything correctly but need help on the final piece. And that the task of generating a new a palindrome from the one given given by the user. Am i on the right track with the while loop or should i use something else? So the result is if you put 192 it would give back Generating a palindrome.... 483 867 1635 6996

"""Checks if the given, positive number, is in fact a palindrome"""

def palindrome(N):
    x = list(str(N))
    if (x[:] == x[::-1]):
        return True
    else: return False 

"""Reverses the given positive integer"""

def reverse_int(N):
    r = str(N)
    x = r[::-1]
    return int(x)


def palindrome_generator():
    recieve = int(input("Enter a positive integer. "))
    if (palindrome(recieve) == True):
        print(recieve, " is a palindrome!")
    else:
        print("Generating a palindrome...")
        while palindrome(recieve) == False:
            reverse_int(recieve) + recieve
Newb18
  • 167
  • 2
  • 11
  • 2
    `receive = reverse_int(recieve) + recieve` – inspectorG4dget Jan 23 '15 at 19:26
  • What is (according to your function), the reverse of 100? – Jasper Jan 23 '15 at 19:28
  • it would give back 1 – Newb18 Jan 23 '15 at 19:31
  • right, and do you see the problem this causes if you try to build a palindrome out of 100? (I assume you want to get 100001? (Well, now I see even another problem) – Jasper Jan 23 '15 at 19:46
  • 3
    You do not need to continually swap back and forth between strings and ints - you can work with the user's string input till you get a final result and convert to an int at the end if needed. For conditional statements, if an object evaluates to a boolean, just use it - ```if true_object:``` instead of ```if true_object == True```. You can *slice* a string - there is no need to convert it to a ```list``` first. – wwii Jan 23 '15 at 19:58
  • the function has to add the original number given by its reverse so if i put in 192 it should give back Generating a palindrome.... 483 867 1635 6996 – Newb18 Jan 23 '15 at 20:03
  • That last comment is crucial for the implementation, please include the way how you are supposed to get the palindrome in the question! – Jasper Jan 23 '15 at 20:14

3 Answers3

4

If I understand your task correctly, the following should do the trick:

def reverse(num):
    return num[::-1]

def is_pal(num):
    return num == reverse(num)

inp = input("Enter a positive number:")

if is_pal(inp):
    print("{} is a palindrome".format(inp))
else:
    print("Generating...")
    while not is_pal(inp):
        inp = str(int(inp) + int(reverse(inp)))
        print(inp)

The variable inp is always a string and only converted to int for the arithmetic.

Jasper
  • 3,939
  • 1
  • 18
  • 35
  • Thanks a lot friend!!!! I've just started programming so it seams i wasn't to off :D – Newb18 Jan 23 '15 at 22:01
  • 1
    No, you were close, but I'd recommend that you read http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ and follow his suggestions. Especially the rubber duck part! – Jasper Jan 24 '15 at 11:33
1

I've been using this solution for many years now to check for palindromes of numbers and text strings.

    def is_palindrome(s):
        s = ''.join(e for e in str(s).replace(' ','').lower() if e.isalnum())
        _len = len(s)
        if _len % 2 == 0:
            if s[:int(_len/2)] == s[int(_len/2):][::-1]:
                return True
        else:
            if s[int(_len/2+1):][::-1] == s[:int(_len/2)]:
                return True
        return False
ART GALLERY
  • 540
  • 2
  • 8
-1

This one is using Complement bitwise and Logical AND and OR operators

_input = 'Abba'                    # _input = 1221

def isPalindrome(_):
    in_str = str(_).casefold()      # Convert number to string + case insensitive
    for _ in range(int(len(in_str) / 2)): # Loop from 0 till halfway
        if in_str[_] != in_str[~_]:
            return False
        return True

print(_input, isPalindrome(_input) and ' is palindrome' or ' is not palindrome')

Abba is palindrome

u-betcha
  • 1
  • 2