Answered step by step
Verified Expert Solution
Question
1 Approved Answer
(c) 20 points Words, sentences, and numbers that read the same forward as backward are called palindromes. The number 43134 , the date 02/02/2020 (ignoring
(c) 20 points Words, sentences, and numbers that read the same forward as backward are called palindromes. The number 43134 , the date 02/02/2020 (ignoring the slashes), the word "madam", and the sentence "Was it a car or a cat I saw?", all are palindrome examples. Write the function palindrome (n,m) that takes as input a positive integer number n and its number of digits, m, and returns True if the number is palindrome, False otherwise. For example, palindrome (25652,5) returns True, while palindrome (6786,4) returns False. Note that the code that you have written in homework 2 for the function getKthDigit (n,k) can greatly come to help in this case. The function getKthDigit (n,k) returned the k-th digit of the number n, starting from the rightmost digit (corresponding to k equal to 0 ). For instance, getKthDigit (7463,2) returns 4 . To help you writing the function palindrome(), the code of the function getKthDigit() is reported here. Look at its one-line code and use it wisely for writing the palindrome() function (which is expected to be quite short too, maybe 4-6 lines of code). def getKthDigit (n,k) : return (n // (10** k)) \% 10 Hint: inspect the digits from left to right and from right to left, if they are always the same the number is palindrome
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started