+ 1
Python palindrome challenge.
I'm doing a challenge. I have to find the largest palindrome (explained below) from the product of 3-digit numbers. Palindrome - is a word/number that reads the same in forward/backward direction. e.g. 0110 , madam. The largest 2-digit palindrom is 9009. Resulted in a calculation of: 91*99 = 9009. I have the following code, any suggestions? https://code.sololearn.com/cnqcPaOMXbpK/?ref=app
9 odpowiedzi
+ 7
Just a "different" school of thought...
You could save time and only compare half.
If the first half of the word matches the first half of the reversed word, it's a match. Comparing the whole thing is redundant.
+ 7
Lamron ,
i did some tests in case of performance and memory consumption. see some comments and the code in the file:
https://code.sololearn.com/c5Sbq5KqDdZW/?ref=app
+ 7
Ausgrindtube ,
how much time can be saved by applying your idea? did you do any tests for this?
+ 3
Slice in reverse is m[ : : -1]
+ 3
Lothar I'm way too lazy to do anything actually useful like that, sorry 😅
I imagine on most machines it wouldn't save much time nor computing resources, how long can a word be, right?
+ 2
Simple mistake, but changes a lot....
Thanks Jayakrishna 🇮🇳
+ 1
Lothar , yes your code is optimised well indeed, and I see how you did it. Thanks for showing optimised alternative
0
Thanks Mirielle
0
I took a rest though about it, tried again and it worked. Thanks for guidance!