22
\$\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\$
  • \$\begingroup\$ Sandbox (deleted) \$\endgroup\$ – Stephen May 31 '17 at 20:50
  • \$\begingroup\$ Would it be acceptable to take n as a code-point? (for esoteric languages such as brain-flak) \$\endgroup\$ – DJMcMayhem May 31 '17 at 21:29
  • \$\begingroup\$ @DJMcMayhem sure. \$\endgroup\$ – Stephen May 31 '17 at 21:30

35 Answers 35

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\$
  • 1
    \$\begingroup\$ I'm pretty sure you can remove the spaces at int i){ and s[i]==s[ to make it 54 bytes. \$\endgroup\$ – Kevin Cruijssen Jun 1 '17 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 Jun 1 '17 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 Jun 1 '17 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 Jun 1 '17 at 13:10
  • \$\begingroup\$ shouldn't it be s[s.size()-i-1] or at least s[s.size()+~i]? \$\endgroup\$ – Felipe Nardi Batista Jun 1 '17 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\$
  • 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 Jun 1 '17 at 10:09
  • 1
    \$\begingroup\$ s[n]==s[~n] saves 2 bytes \$\endgroup\$ – Felipe Nardi Batista Jun 1 '17 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 Jun 2 '17 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 Jun 2 '17 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 Jun 2 '17 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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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