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

#Python: A palindrome is a sequence of characters that reads the same backwards as forwards. For...
#Python: A palindrome is a sequence of characters that reads the same backwards as forwards. For example, ‘Eve’, ‘madam’, and 20502, are palindromes. Write a function called testPalindrome() that asks the user to input a string and returns if that string is a palindrome with the output as follows, without red: >>> Please enter a string: eve Your string "eve" is a palindrome. >>> testPalindrome() Please enter a string: end Your string "end" is not a palindrome. >>> testPalindrome() Please...
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...
2. Palindromes A palindrome is a word that reads the same forwards and backwards. For example,...
2. Palindromes A palindrome is a word that reads the same forwards and backwards. For example, \aibohphobia" (the irrational fear of palindromes) is a word that reads the same forwards and backwards. Write a function called isPalindrome() that accepts a string as a parameter and returns True if the string is a palindrome, False otherwise. Using the function you created, write a program Python home work Python
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...
(Palindrome number - A number is a palindrome if it reads the same from right to...
(Palindrome number - A number is a palindrome if it reads the same from right to left and from left to right, for example 676 is a palindrome number) Write a program that prompts the user to enter a three-digit integer number and determines whether it is a palindrome number or not In Java Please
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 string that reads the same forward and backward, i.e., the letters are...
A palindrome is a string that reads the same forward and backward, i.e., the letters are the same whether you read them from right to left or from left to right.      Examples: radar à is a palindrome Able was I ere I saw Elba à is a palindrome good à not a palindrome Write a java program to read a line of text and tell if the line is a palindrome. Use a stack to read each non-blank character...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT