Sep-25-2020, 04:32 PM
the range is 0 to int(string length/2) -- stops after middle of string is reached
it then checks to see if current character (i) is equal to the character at the same distance from end
example: say word is 'civic'
the loop will fail on first iteration and return False.
it then checks to see if current character (i) is equal to the character at the same distance from end
example: say word is 'civic'
Output:length of word / 2 is 5/2 = int(2.5) = 2
range = 0 - 2
..........................................................
| Iter. | i | s[i] | len(s)-i-1 | s[len(s)-i-1] | RetVal |
..........................................................
| 1 | 0 | 'c' | 5-0-1 = 4 | 'c' | True |
..........................................................
| 2 | 1 | 'i' | 5-1-1 = 3 | 'i' | True |
..........................................................
s is a palindrome
if you add a 'k' ro civic ('civick')the loop will fail on first iteration and return False.