Question

In: Computer Science

making a python code for this: A palindrome is a sequence that reads the same backwards...

making a python code for this:

A palindrome is a sequence that reads the same backwards as forwards. Numbers can also be palindromes if we consider their digits as a sequence, for example 12121 and 8228 are palindromes.

We can find palindromes from an initial seed number using the reverse and add method: choose a number, reverse its digits and add it to the original. If the sum is not a palindrome (which means, it is not the same number from left to right and right to left), repeat this procedure.

For example:

195 Initial number

+591

—–—

786

+687

—–—

1473

+3741

—–—–

5214

+4125

—–—–

9339 Resulting palindrome

In this particular case the palindrome ‘9339’ appeared after the 4th addition. This method leads to palindromes in a few iterations for almost all integers. But there are interesting exceptions. 196 is the first number for which no palindrome has been found. It is not proven though, that there is no such a palindrome.

You must write a program that give the resulting palindrome and the number of iterations (additions) to compute the palindrome.

You might assume that all tests data on this problem:

  • will have an answer ,
  • will be computable with less than 1000 iterations (additions),
  • will yield a palindrome that is not greater than 4,294,967,295.

Input

The first line will have a number N (0 < N <= 100), where N is the number of test cases. The next N lines will each have one number, P, which is the initial number from which to compute a palindrome.

The file could contain non-integer data, which shall be processed as indicated below.

Output

For each of the N tests you will output one line as follows:

  • when the line contains an integer, output: number_of_iterations and resulting_palindrome separated by one space
  • when the line contains invalid data (anything other than a single integer) output: the phrase ‘Bad data:’ followed by the input

Sample Input

4

195

265

2.7

750

Sample Output

4 9339

5 45254

Bad data: 2.7

3 6666

Solutions

Expert Solution

Thanks for the question.
Below is the code you will be needing  Let me know if you have any doubts or if you need anything to change.
Thank You !
===========================================================================


# function takes in a number and returns the iterations it took to find a palindrome and the 
# palindrome number as tuple, for all non-integer it returns 'Bad Data:' 

def find_palindrome(number):
    iterations = 0
    try:
        to_int = int(number)
        if to_int != number: return 'Bad data:', number
    except:
        return 'Bad data:', number

    while True:
        reverse = str(number)[::-1]
        reverse_int = int(reverse)
        if number == reverse_int:
            break
        else:
            iterations += 1
            number = reverse_int + number
    return iterations, number


def main():
    iteration,num = find_palindrome(195) # test with 195
    print('{} {}'.format(iteration,num))
    iteration,num = find_palindrome(265) # test with 265
    print('{} {}'.format(iteration,num))
    iteration,num = find_palindrome(2.7) # test with 2.7
    print('{} {}'.format(iteration,num))
    iteration,num = find_palindrome(750) # test with 750
    print('{} {}'.format(iteration,num))


main()

=========================================================================


Related Solutions

IN JAVA - [(1)] A palindrome is a string that reads the same forwards as backwards....
IN JAVA - [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q be a non-empty...
python question A word is a palindrome if it the same read forwards and backwards. We...
python question A word is a palindrome if it the same read forwards and backwards. We will call a word a fuzzy palindrome if it is the same read forwards and backwards, except for possible differences in case. For example, both 'tattarrattat' and 'TaTtArRAttat' are fuzzy palindromes. Define a function is_fuzzy_palindrome that returns True if and only if its argument is a fuzzy palindrome. This method may be useful: S.lower() -> str : Return a copy of the string S...
Please provide Python code that does the following: 2) A palindrome is any sequence of characters...
Please provide Python code that does the following: 2) A palindrome is any sequence of characters that reads the same backwards as forwards. One-word examples include: Dad madam rotor Kayak redder noon For the sake of this exercise, the following may also be considered one-word palindromes: 1881 zap4554paz That is, numeric strings and “nonsense” strings that read the same backwards as forwards should be classified as palindromes. Write a program that takes as input a string and determines if it’s...
In C++: This is the problem: [(1)] A palindrome is a string that reads the same...
In C++: This is the problem: [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q...
1. A is called a palindrome if it reads the same from left and right. For...
1. A is called a palindrome if it reads the same from left and right. For instance, 13631 is a palindrome, while 435734 is not. A 6-digit number n is randomly chosen. Find the probability of the event that (a) n is a palindrome. (b) n is odd and a palindrome. (c) n is even and a palindrome.
A is called a palindrome if it reads the same from left and right. For instance,...
A is called a palindrome if it reads the same from left and right. For instance, 13631 is a palindrome, while 435734 is not. A 6-digit number n is randomly chosen. Find the probability of the event that (a) n is a palindrome. (b) n is odd and a palindrome. (c) n is even and a palindrome.
A palindrome is a word or phrase, which reads the same backward or forward. Write a...
A palindrome is a word or phrase, which reads the same backward or forward. Write a program that prompts the user for a string of characters terminated by a period and determines whether the string (without the period) is a palindrome. IMP: Assume that the input contains only letters and blanks. Assume also that the input is at most 30 characters long. Use an array of characters of size 30 to store the input! Disregard blanks when deciding if the...
A palindromic number reads the same both ways (left-to-right and right-to-left). The largest palindrome made from...
A palindromic number reads the same both ways (left-to-right and right-to-left). The largest palindrome made from the product of two 2-digit numbers is 9,009 = 91 × 99. The largest palindrome made from the product of two 3-digit numbers is 906,609 = 913 × 993. The largest palindrome made from the product of two 4-digit numbers is 99,000,099 = 9,901 × 9,999. 1. Write a function IN JAVASCRIPT to find the largest palindrome made from the product of two 7-digit...
Using Python. Write a program that reads a sequence (unknown number of inputs) of integer inputs...
Using Python. Write a program that reads a sequence (unknown number of inputs) of integer inputs and prints the number of even and odd inputs in the sequence. please explain. Thanks
Do not use arrays and code a program that reads a sequence of positive integers from...
Do not use arrays and code a program that reads a sequence of positive integers from the keyboard and finds the largest and the smallest of them along with their number of occurrences. The user enters zero to terminate the input. If the user enters a negative number, the program displays an error and continues. Sample 1: Enter numbers: 3 2 6 2 2 6 6 6 5 6 0 Largest Occurrences Smallest Occurrences 6 5 2 3. Do not...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT