0

Help, there is a case that my code is wrong in C++

Is about the function isPalindrome. I thing this working well in integer positive and zero, but not pass a hidden testing in the course project. https://code.sololearn.com/c1Do5jXLTDK6/?ref=app.

5th Jul 2022, 11:23 AM
Daniel DĂĄvalos
Daniel DĂĄvalos - avatar
6 Answers
+ 2
Ipang yes, I'm maybe over-complicating for this, but it was first thing that ocurred to me.
5th Jul 2022, 12:45 PM
Daniel DĂĄvalos
Daniel DĂĄvalos - avatar
+ 1
Seems to me you're over-complicating it. Use the common way to reverse a number using division and modulo, then compare original number with its reversed version. If they were similar, the original number is a palindrome number.
5th Jul 2022, 11:53 AM
Ipang
+ 1
I understand negative numbers are not palindromic, and the code seems to get this correctly. It probably is failing some edge case I can't identify. I'd check if the log10 always returns as expected for ints.
5th Jul 2022, 12:36 PM
Emerson Prado
Emerson Prado - avatar
+ 1
It starts failing at 9999999
5th Jul 2022, 12:40 PM
Emerson Prado
Emerson Prado - avatar
0
Emerson Prado wow, you're right. For n>=9999999 returns is Not a palindrome. Why is this?
5th Jul 2022, 12:47 PM
Daniel DĂĄvalos
Daniel DĂĄvalos - avatar
0
Daniel DĂĄvalos Probably some rouding issue. It works for higher numbers, but only with lots of 9's.
5th Jul 2022, 12:57 PM
Emerson Prado
Emerson Prado - avatar