what a hack! I LIKE this idea! It's neat!
Look at this code I wrote for Project Euler. it checks if a sequence is "palindromic". A "palindromic" sequence reads the same both from left and from right. So the simple idea is to check from the left of the sequence to the middle, and compare each element during the process with ones read from the right. Note here I use Python 2.5 integer division (//) to ensure the index is integer.
Using official Python index syntax, I have to write "s[i] == s[-i-1]" to compare elements on symmetric positions in the sequence.
def ispalindromic(s):
return all(s[i] == s[-i-1] for i in range(len(s)//2))
However, using the "bitwise or" trick proposed above, I just write "s[i] == [s~i]", really neat!
def ispalindromic(s):
return all(s[i] == s[~i] for i in range(len(s)//2))