Does the nth char equal the nth from last char?
JavaScript (ES6), 26 bytes
s=>n=>s[n]==s.substr(~n,1)
Alternatively:
s=>n=>s[n]==s.slice(~n)[0]
This one almost works, but fails when n == 0
(because s.slice(-1,0) == ""
):
s=>n=>s[n]==s.slice(~n,-n)
Another 26-byte solution that @RickHitchcock pointed out:
s=>n=>s[n]==s[s.length+~n]
Jelly, 5 4 bytes
=UƓị
Try it online!
There should be no shorter answers in Jelly. An program would need comparison, reversal/negation, an index call, and a byte for control flow (Ɠ
in this case), which adds up to four bytes.
How it works
=UƓị
- (implicit) input string
= - equals (vectorizing by characters because a string is a charlist)
U - the reversed string
ị - get the element at the index of:
Ɠ - the input index
-1 byte thanks to @ais523, using Ɠ
MATL, 5 bytes
tP=w)
Try it online!
Explanation:
t % Duplicate the input
Stack:
['ppqqpq' 'ppqqpq']
P % Reverse the top element of the stack
Stack:
['ppqqpq' 'qpqqpp']
= % Equals. Push an array of the indices that are equal
Stack:
[[0 1 1 1 1 0]]
w % Swap the top two elements
Stack:
[[0 1 1 1 1 0], 3]
) % Grab the a'th element of b