26
\$\begingroup\$

Inspired by Does the start equal the end

Given a string s and an integer n, output a truthy/falsey as to whether the nth char in s equals the nth from the end char in s.

Input

A non-empty string and an integer. You can use 0-based indexing or 1-based indexing. The integer is guaranteed to be valid based on the string. For example, if the string is "supercalifragalistic123", the integer can be from 1 to 23 for 1-based indexing, and 0 to 22 for 0-based indexing. Please note that n can be larger than half the length of s.

Input is limited to printable ASCII.

Output

A truthy/falsey value based on whether the nth value in s equals the nth from last value in s.

Please note that the last char is in position 0 for 0-based indexing and position 1 for 1-based indexing. Think of it as comparing the string to its reverse.

Test Cases

0-indexed

"1", 0         Truthy 1 == 1
"abc", 1       Truthy b == b
"aaaaaaa", 3   Truthy a == a
"[][]", 1      Falsey ] != [
"[][]", 0      Falsey [ != ]
"ppqqpq", 2    Truthy q == q
"ababab", 5    Falsey a != b
"12345", 0     Falsey 1 != 5
"letter", 1    Truthy e == e
"zxywv", 3     Falsey w != x

1-indexed

"1", 1         Truthy 1 == 1
"abc", 2       Truthy b == b
"aaaaaaa", 4   Truthy a == a
"[][]", 2      Falsey ] != [
"[][]", 1      Falsey [ != ]
"ppqqpq", 3    Truthy q == q
"ababab", 6    Falsey a != b
"12345", 1     Falsey 1 != 5
"letter", 2    Truthy e == e
"zxywv", 4     Falsey w != x
\$\endgroup\$
3
  • \$\begingroup\$ Sandbox (deleted) \$\endgroup\$
    – Stephen
    Commented May 31, 2017 at 20:50
  • \$\begingroup\$ Would it be acceptable to take n as a code-point? (for esoteric languages such as brain-flak) \$\endgroup\$
    – DJMcMayhem
    Commented May 31, 2017 at 21:29
  • \$\begingroup\$ @DJMcMayhem sure. \$\endgroup\$
    – Stephen
    Commented May 31, 2017 at 21:30

45 Answers 45

1
2
1
\$\begingroup\$

QBIC, 18 bytes

?_s;,:,1|=_sA,-a,1

Explanation

?        =     PRINT -1 if equal, 0 otherwise, between
 _s     |      A substring of
   ;,:,1          A$ string (read from cmd line), from the n'th pos, length 1
 _sA,-a,1      And a substring of A$, n'th pos from the right, also 1 length
               The second Substring is auto-terminated because EOF.
\$\endgroup\$
1
\$\begingroup\$

CJam, 11 bytes

q~_2$=@@~==

Bad. Too much stack manipulation.

\$\endgroup\$
1
\$\begingroup\$

><> (with this interpreter), 25 bytes

i:0(?v
]&=n;>~{:}[:}]&r[}

It doesn't work in TIO: the TIO interpreter doesn't reverse the new stack when doing the [ instruction, but the fish playground does — compare "abcde"5[ooooo; run here and here, for example.

The string input is taken from STDIN, and we assume n is already on the stack. Uses 1-indexing.

The fish gets the nth character with [:}]&, which siphons off the first n things on the stack into a new, reversed stack, manipulates that a bit, then puts the things back and saves the nth character in the register. It then reverses the whole stack and does the same again, and returns 1 if the two characters are equal, and 0 otherwise.

This seems to work at TIO, for 26 bytes:

i:0(?v
]&=n;>~{:}[{:}]&r[{
\$\endgroup\$
1
\$\begingroup\$

C, 73 bytes

Compiles as-is with GCC 6.3.1 (no flags). Some unnecessary obfuscation included.

main(c,v)char**v;{c=atoi(v[2]);putchar((*++v)[c]-(*v)[strlen(*v+1)-c]);}

Usage

$./a.out abcdcba 6

Truthy = nothing, falsey = garbage.

\$\endgroup\$
1
\$\begingroup\$

Lua, 35 bytes

Uses 1-indexing.

s,n=...print(s:byte(n)==s:byte(-n))

Try it online!

\$\endgroup\$
1
\$\begingroup\$

C++, 57 Bytes

Assuming header includes don't count.

bool c(std::string s,int i){return s[i]==s[s.size()-i];}
\$\endgroup\$
5
  • 1
    \$\begingroup\$ I'm pretty sure you can remove the spaces at int i){ and s[i]==s[ to make it 54 bytes. \$\endgroup\$ Commented Jun 1, 2017 at 9:04
  • \$\begingroup\$ I don't think this is valid. The first parameter needs to be given an identifier, otherwise you cannot refer to it. But you don't need the space between the closing parenthesis of the function signature and the opening brace of its definition, so this would only increase your code size by 1 byte. You'd get a net decrease by removing the spaces around the equals sign, as Kevin suggested. \$\endgroup\$
    – Cody Gray
    Commented Jun 1, 2017 at 11:05
  • \$\begingroup\$ In addition to removing spaces and including s in the parameter list you could also replace bool by int. \$\endgroup\$
    – sigvaldm
    Commented Jun 1, 2017 at 13:01
  • \$\begingroup\$ #include<string> int c(std::string s,int i){return s[i]==s[s.size()-i];}(line break of course) is the smallest I get to compile. If you find a compiler that includes <string> on default you may drop it. \$\endgroup\$
    – Christoph
    Commented Jun 1, 2017 at 13:10
  • \$\begingroup\$ shouldn't it be s[s.size()-i-1] or at least s[s.size()+~i]? \$\endgroup\$ Commented Jun 1, 2017 at 13:31
1
\$\begingroup\$

REXX, 41 bytes

parse arg n,=(n)l+1''-(n)r+1 .
return l=r

Arguments are given in n,s order since n is required for parsing s.

\$\endgroup\$
1
\$\begingroup\$

Crystal, 26 24 Bytes

def c(s,n)s[n]==s[~n]end

-2 Bytes from Felipe Nardi Batista, thanks.

Try it online!

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Welcome to the site! A nice solution, to improve your answer I would recommend adding a "Try it online" link that shows an implementation of your answer. \$\endgroup\$
    – Notts90
    Commented Jun 1, 2017 at 10:09
  • 1
    \$\begingroup\$ s[n]==s[~n] saves 2 bytes \$\endgroup\$ Commented Jun 1, 2017 at 11:09
  • \$\begingroup\$ @Notts90, i don't think there are any online crystal-interpreters. :( The homepage provides one, but I don't think it has an API? \$\endgroup\$
    – Domii
    Commented Jun 2, 2017 at 7:32
  • \$\begingroup\$ @Domii look at the try it online link in my previous comment, it's lists crystal as one of the languages it supports. \$\endgroup\$
    – Notts90
    Commented Jun 2, 2017 at 7:51
  • \$\begingroup\$ @Notts90 Oh sry. I missed that link because the color is almost the same as the rest. Thanks :D \$\endgroup\$
    – Domii
    Commented Jun 2, 2017 at 22:44
1
\$\begingroup\$

shortC, 23 bytes

Df(s,n)s[n]==s[Ss)-1-n]

Try it online!

Substitutions in this program:

  • D -> #define
  • S -> strlen(
  • No auto-inserted closing stuff :(
\$\endgroup\$
1
\$\begingroup\$

Perl 5, 23 + 3 (-lF) = 26 bytes

say$F[$n=<>]eq$F[-1-$n]

Try it online!

Uses 0 indexing

\$\endgroup\$
1
\$\begingroup\$

Thunno 2, 3 bytes

Ḳ=i

Try it online!

Explanation

Ḳ=i  # Implicit input
Ḳ    # Bifurcate the input string
 =   # Check for equality (vectorised)
  i  # Index into this list
     # Implicit output
\$\endgroup\$
2
  • \$\begingroup\$ clever! thank you for styling the Try Thunno page better lol. and no i didn’t entirely forget about the docs site, i’ll get that done soonTM \$\endgroup\$ Commented Aug 9, 2023 at 18:05
  • \$\begingroup\$ That's ok. (Also, the restyling was mainly lyxal telling me what code to put where lol :p) \$\endgroup\$
    – The Thonnu
    Commented Aug 9, 2023 at 18:18
1
\$\begingroup\$

Javascript, 20 bytes

s=>n=>s[n]==s.at(~n)
\$\endgroup\$
1
\$\begingroup\$

PowerShell, 18 bytes

$a[$b]-eq$a[-$b-1]

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Itr, 6 bytes

ä¡Y=à@

0-indexed

online interpreter

Explanation

ä       ; duplicate the first (implicit) input
 ¡      ; reverse it
  Y=    ; zip the two lists with equality
    à@  ; get the element at the index given by the second index
        ; implicit output
\$\endgroup\$
1
\$\begingroup\$

Uiua, 5 bytes

⊡:=⇌.

Try it!

⊡:=⇌.
    .  # duplicate
   ⇌   # reverse
  =    # elementwise equal
 :     # flip
⊡      # pick
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.