Can anyone help me get a more clear understanding for what it is exactly that the assignment is asking me to do?? For example, what exactly is supposed to be in the first function, am i doing the testing in the first function or the generator? It's not clear to me, and the professor hasn't answered my email yet. The basic premise of the assignment is to generate a Palindrome

Ex. Given the integer 192, which is not a palindrome, we obtain a palindrome by applying the above steps four times:

1. Add 192 and its reverse, 291, to get 483. 
2. Add 483 and its reverse, 384, to get 867. 
3. Add 867 and its reverse, 768, to get 1635. 
4. Add 1635 and its reverse, 5361, to get 6996, which is a palindrome!
Write a program, containing the following three functions, to generate palindromes using the above procedure. The str and int built-in functions, which convert back and forth between integers and strings, will come in handy when implementing these functions.

[B]1[/B] • A function called palindrome that has a single parameter N (a positive integer) and returns True if N is a palindrome and False otherwise.

[B]2[/B] • A function called reverse int that has a single parameter N (a positive integer) and returns its reverse.

[B]3[/B] • A function called palindrome generator without any parameters which reads in a pos- itive integer from the user and informs him if the integer is a palindrome or not. If it is not, apply the procedure described above to obtain a palindrome. This should be done by making appropriate calls to the functions palindrome and reverse int. The function should print out every intermediate integer generated by the procedure until the final palindrome. It should also state the number of iterations that were required to go from the original integer to the palindrome. You should allow the user to repeatedly enter integers. See the sample runs for some examples.

Recommended Answers

All 4 Replies

Example of what the run should look like:

Enter a positive integer: 192 

192 is not a palindrome.
Generating a palindrome.... 

483 
867 
1635
6996 

4 iterations were needed to get to a palindrome.

Do it again? [y/n] y 
Enter a positive integer: 1206

1206 is not a palindrome.

Generating a palindrome.... 
7227

1 iterations were needed to get to a palindrome.

Do it again? [y/n] y 
Enter a positive integer: 13425652431

13425652431 is a palindrome.

Do it again? [y/n] n 
Goodbye!

What i have so far is:

def palindrome (N):
    for i in N:
        if (N[0] == N[-1]):
            print(N, "Is a palindrome" )
            return True
    return False

def reverse_int(N):
    if len(N) == 0:
        return ""
    else:
        return reverse_int(N[1:] + N[0])

def palindrome_generator():
    s= input ("Enter a postive integer: ")
    palindrome(s)

You are not reversing integer, you are processing a string, for integer use function divmod (or // and % operators). You do not call the palindrome_generator function, so nothing will happen.

I'm not sure what you mean with me not calling the palindrome_generator...But my new reverse_int function now is:

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

The palindrome function only compares the first and last numbers. You have to compare all of them. To reverse an integer without converting to a string you divide by ten.

def palindrome(num_in):
    reversed_num = 0
    whole = num_in
    while whole > 0:
        whole, remain = divmod(whole, 10)
        reversed_num = reversed_num*10 + remain

    print reversed_num,
    if reversed_num == num_in:
        print "is palindrome"
    else:
        print "NOT a palindrome"

palindrome(12345)
palindrome(12321)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.