42
\$\begingroup\$

The Task

In this challenge, your task is to write a program or function which takes in a String and outputs a truthy or falsey value based on whether the first character and the last character of the input String are equal.

Input

You may take input in any way reasonable way. However, assuming that the input is present in a predefined variable is not allowed. Reading from a file, console, command line, input field etc., or taking input as a function argument is allowed.

Output

You may output in any reasonable format, except for assigning the result to a variable. Writing to a file, console, command line, modal box, function return statements etc. is allowed.

Additional Rules

  • The input can be empty String as well, for which you should return a falsey value.

  • Single-Char Input Strings should have a truthy result.

  • Your program should be case-sensitive. helloH should output a falsey value.

  • You can only have a single Truthy value and a single Falsey value. For example, outputting false for an Input String and 0 for another input String as Falsey values is not allowed.

  • Standard loopholes are not allowed.

Test Cases

Input    ->    Output

"10h01"        Truthy
"Nothing"      Falsey
"Acccca"       Falsey
"eraser"       Falsey
"erase"        Truthy
"wow!"         Falsey
"wow"          Truthy
"H"            Truthy
""             Falsey

This is , so the shortest code in bytes wins!

\$\endgroup\$
5
  • \$\begingroup\$ What characters can appear in the input? Printable ASCII? \$\endgroup\$ May 13, 2017 at 19:35
  • \$\begingroup\$ @MartinEnder Printable ASCII. Although, I don't think it matters much. \$\endgroup\$
    – Arjun
    Jun 3, 2017 at 17:29
  • \$\begingroup\$ Of course it matters. Some languages can't process non-ASCII characters or null bytes, and in a regex I can match any printable ASCII character with ., but it wouldn't match linefeeds. In general, if you find yourself using the string tag, specify exactly what characters can appear in the input. \$\endgroup\$ Jun 3, 2017 at 17:31
  • \$\begingroup\$ @MartinEnder Okay. Will take care in future. \$\endgroup\$
    – Arjun
    Jun 4, 2017 at 7:16
  • \$\begingroup\$ Suggested test case: AbAb => false \$\endgroup\$ Oct 31, 2017 at 14:40

113 Answers 113

21
\$\begingroup\$

Python 3, 23 bytes

s=input()
s[0]!=s[-1]<e

Output is via exit code, so 0 (success) is truthy and 1 (failure) is falsy. If this is acceptable, a byte can be saved.

Try it online!

How it works

First of all, if s is an empty string, s[0] will raise an IndexError, causing the program to fail.

For non-empty s, if the first and last characters are equal, s[0]!=s[-1] will evaluate to False, so the program exits cleanly and immediately.

Finally, if the characters are different, s[0]!=s[-1] will evaluate to True, causing the compairson s[-1]<e to be performed. Since e is undefined, that raises a NameError.

If backwards compatibility with Python 2 is not desired,

s[0]!=s[-1]<3

works as well, since comparing a string with an integer raises a TypeError.

\$\endgroup\$
4
  • \$\begingroup\$ Save 1 byte with lambda \$\endgroup\$
    – AAM111
    May 14, 2017 at 23:07
  • 1
    \$\begingroup\$ Yes, a regular function would also save a byte. While output via exit code is an established consensus, non-error/error for a function is not though. I've linked to the proposal in my answer. \$\endgroup\$
    – Dennis
    May 14, 2017 at 23:12
  • \$\begingroup\$ What about using Python REPL? \$\endgroup\$
    – AAM111
    May 14, 2017 at 23:13
  • \$\begingroup\$ I don't think that helps. It's still not an exit code. \$\endgroup\$
    – Dennis
    May 15, 2017 at 0:25
15
\$\begingroup\$

JavaScript, 19 bytes

a=>a.endsWith(a[0])
\$\endgroup\$
2
  • \$\begingroup\$ Wow. I didn't even know that there exists an endsWith method of String object. Nice! :) \$\endgroup\$
    – Arjun
    May 15, 2017 at 2:45
  • \$\begingroup\$ How did I forget about endsWith()?! I've been waiting for an opportunity to use it. \$\endgroup\$
    – Shaggy
    May 15, 2017 at 16:38
8
\$\begingroup\$

Brachylog, 4 bytes

h~t?

Try it online!

Explanation

h       The head of the Input...
 ~t?    ...is the tail of the Input
\$\endgroup\$
1
  • 2
    \$\begingroup\$ I really need to get round to implementing those constraint variables for the input; that'd mean we'd be able to do this in two. \$\endgroup\$
    – user62131
    May 13, 2017 at 17:25
8
\$\begingroup\$

05AB1E, 4 bytes

S¬Q¤

Try it online! or Try All Tests

S    # Split the input into individual characters
 ¬   # Get the first character
  Q  # Check all characters for equality to the first
   ¤ # Get the last value i.e. head == tail
\$\endgroup\$
3
7
\$\begingroup\$

Jelly, 3 bytes

=ṚḢ

Try it online!

\$\endgroup\$
0
7
\$\begingroup\$

Mathematica, 15 bytes

#&@@#===Last@#&

Takes an array of chars. Throws errors when the input is empty but can be ignored.

\$\endgroup\$
1
  • 4
    \$\begingroup\$ Nice job spotting the fact that === handles the empty case :) \$\endgroup\$ May 13, 2017 at 19:01
7
\$\begingroup\$

Haskell, 21 bytes

c takes a String and returns a Bool.

c s=take 1s==[last s]

Try it online!

  • If not for empty strings, this could have been 16 bytes with c s=s!!0==last s.
  • take 1s gives a list that is just the first element of s unless s is empty, in which case it's empty too.
  • last s would error out on an empty string, but Haskell's laziness saves it: A string with a single element is always different from the empty string, without evaluating its element.
\$\endgroup\$
0
7
\$\begingroup\$

Retina, 13 12 bytes

^(.)(.*\1)?$

Try it online! Includes test suite. Edit: Saved 1 byte thanks to @Kobi.

\$\endgroup\$
0
7
\$\begingroup\$

C++ (gcc), 40 bytes

[](auto s){return s[0]&&s[0]==s.back();}

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I'm not the best in C++ (I typically use C), but could you change the instances of s[0] to *s to save two bytes each? \$\endgroup\$
    – MD XF
    May 14, 2017 at 19:43
  • 1
    \$\begingroup\$ @MDXF, that will only work with C type arrays. \$\endgroup\$
    – jdt
    May 15, 2017 at 11:05
6
\$\begingroup\$

Java, 81 77 bytes

  • -4 bytes, thanks @KevinCruijssen

Try Online

boolean f(String s){int l=s.length();return l>0&&s.charAt(l-1)==s.charAt(0);}
  • Returns true if they're equal, otherwise false, false for empty string

Array Version, 60 bytes

boolean f(char[]s){int l=s.length;return l>0&&s[0]==s[l-1];}
\$\endgroup\$
15
  • \$\begingroup\$ Why long instead of int? \$\endgroup\$
    – corvus_192
    May 14, 2017 at 10:30
  • \$\begingroup\$ @corvus_192 a unicode character can be 1-6 bytes. \$\endgroup\$
    – Khaled.K
    May 14, 2017 at 10:33
  • \$\begingroup\$ The difference between two chars can be at most Charcter.MAX_VALUE - Character.MIN_VALUE, which is 65535 \$\endgroup\$
    – corvus_192
    May 14, 2017 at 10:39
  • \$\begingroup\$ @corvus_192 I see, I've fixed it now \$\endgroup\$
    – Khaled.K
    May 14, 2017 at 11:21
  • 1
    \$\begingroup\$ @KevinCruijssen For the last, s.charAt(l-1)==s.charAt(0) would save two bytes. \$\endgroup\$
    – JollyJoker
    May 15, 2017 at 10:56
6
\$\begingroup\$

Python 2, 26 25 24 bytes

Thanks to @Dennis for saving a byte!

lambda x:""<x[:1]==x[-1]

Try it online!

\$\endgroup\$
0
5
\$\begingroup\$

brainfuck, 43 bytes

+>,[<,[>[->+<<->],]]<[[-]-<]-[----->+<]>--.

Try it online!

Explanation

The main loop is [>[->+<<->],]. After each iteration, the cell to the right of the current position is the first byte of the string, and the cell to the left is the difference between the most recently handled character and the first. <[[-]-<] converts the final result to -1 if nonzero, and the rest converts -1 and 0 to 48 and 49 ("0" and "1") respectively.

\$\endgroup\$
0
5
\$\begingroup\$

APL (Dyalog), 4 bytes

⊃⌽=⊃

Try it online!

Explanation

  =                     Compare
   ⊃                    The first element of the right argument with
 ⌽                      The right argument reversed
                        This will return an array of the length of the reversed argument. Each element in the resulting array will be either 0 or 1 depending on whether the element at that position of the reversed argument equals the first element of the original right argument
                        So with argument 'abcda', we compare 'a' with each character in 'adcba' which results in the array 1 0 0 0 1
⊃                       From this result, pick the first element.

Here is the reason this works on empty strings. Applying to an empty string returns a space . But reversing an empty string still returns an empty string, so comparing an empty string with a non-empty string (in this case ) gives an empty numerical vector. And applying to an empty numerical vector returns 0. Hence passing an empty string returns 0.

\$\endgroup\$
7
  • \$\begingroup\$ This is actually really cool answer, but your explanation is not right. It would be right for (⊃⌽)=⊃ or ⊢/=⊃, but neither of those give the right result. Instead ⌽=⊃ compares the reversed string to its first character, and then picks the first element of that. If the string is empty, it ends up comparing a space to an empty string, which gives an empty Boolean list, of which the (coerced) first element is 0 – the correct answer for empty strings. Your expression is equivalent to ⊃⊃=⌽ because = is commutative. \$\endgroup\$
    – Adám
    May 15, 2017 at 11:34
  • \$\begingroup\$ @Adám Thank you for helping me see the mistake in my explanation. \$\endgroup\$
    – user41805
    May 15, 2017 at 11:41
  • \$\begingroup\$ You're welcome. Now your note is not correct. ⊃⌽=⊃ is not the same as (⊃⌽)=⊃. It is more expensive, as it compares all the elements instead of just the first and last. Also it wouldn't work had the OP used numbers instead of strings. \$\endgroup\$
    – Adám
    May 15, 2017 at 11:44
  • \$\begingroup\$ The first argument reversedThe right argument reversed \$\endgroup\$
    – Adám
    May 15, 2017 at 11:44
  • \$\begingroup\$ You may also want to explain why this works on empty strings. \$\endgroup\$
    – Adám
    May 15, 2017 at 11:45
5
\$\begingroup\$

MATL, 5 bytes

&=PO)

Try it at MATL Online!

Explanation

       % Implicitly grab input as a string (of length N)
&=     % Perform an element-wise equality check yielding an N x N matrix
P      % Flip this matrix up-down
O)     % Get the last value in the matrix (column-major ordering)
       % Implicitly display the result

In the case, that an empty input string must be handled, then something like the following (8 bytes) would work

&=POwhO)

This solution simply prepends a 0 to the front of the N x N matrix such that for an empty input, when the matrix is 0 x 0, there's still a 0 value that is then grabbed by 0)

Try it at MATL Online

\$\endgroup\$
4
  • \$\begingroup\$ Very clever approach! \$\endgroup\$
    – Luis Mendo
    May 13, 2017 at 18:09
  • \$\begingroup\$ Also 5 bytes: 5L)d~. \$\endgroup\$
    – Sanchises
    May 14, 2017 at 15:24
  • 2
    \$\begingroup\$ Just a heads-up: neither my comment nor your answer handle empty input. This has (in my opinion convincincly) been argued against in the comments, so I expect this requirement to change. However, as it stands, this entry is invalid. \$\endgroup\$
    – Sanchises
    May 14, 2017 at 15:29
  • 1
    \$\begingroup\$ (of course, you could do tn?&=PO)}F to deal with empty input; not sure if there is a more efficient way) \$\endgroup\$
    – Sanchises
    May 14, 2017 at 15:33
4
\$\begingroup\$

Japt, 6 bytes

tJ ¥Ug

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Hmm, doesn't work for the empty string (should give false). I think you can fix this with tJ ¥Ug \$\endgroup\$ May 13, 2017 at 19:31
4
\$\begingroup\$

Java, 52 43 bytes

s->!s.isEmpty()&&s.endsWith(""+s.charAt(0))

To make it work, feed this into a function such as the following that makes a lambda "go":

private static boolean f(Function<String, Boolean> func, String value) {
  return func.apply(value);
}
\$\endgroup\$
4
  • 1
    \$\begingroup\$ You can shave off 9 chars using s.endsWith(""+s.charAt(0)) instead of s.charAt(0)==s.charAt(s.length()-1) \$\endgroup\$
    – SpaceBison
    May 15, 2017 at 8:32
  • \$\begingroup\$ s->""!=s&&s.endsWith(""+s.charAt(0)) \$\endgroup\$
    – JollyJoker
    May 15, 2017 at 11:06
  • 1
    \$\begingroup\$ @JollyJoker that does not work: try feeding new String() into the lambda. It will throw an exception. Reference semantics do not work here. \$\endgroup\$
    – user18932
    May 15, 2017 at 20:58
  • 2
    \$\begingroup\$ @KevinCruijssen The short circuiting effect of && is necessary to avoid an index out of bounds exception on the charAt(0) for an empty string \$\endgroup\$
    – PunPun1000
    May 22, 2017 at 13:03
4
\$\begingroup\$

Ruby, 26 24 bytes

Saved two bytes thanks to @philomory!

->e{!!e[0]>0&&e[0]==e[-1]}

First post on codegolf -))

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ May 15, 2017 at 14:36
  • 1
    \$\begingroup\$ Welcome to PPCG! Nice first golf. Good luck for future! \$\endgroup\$
    – Arjun
    May 15, 2017 at 17:17
  • 1
    \$\begingroup\$ You could save 4 bytes by just doing e[0]&&e[0]==e[-1], since if e is empty, e[0] will be nil. Actually, come to think of it, nil is no good since it's falsey but not the same falsey that the comparison returns; still, after adding !! you're still saving 2 characters. \$\endgroup\$
    – philomory
    May 16, 2017 at 2:49
3
\$\begingroup\$

GNU grep, 12 bytes

^(.)(.*\1)?$

Run in extended or PCRE mode.

I don't know if this is considered cheating or not.

\$\endgroup\$
2
  • \$\begingroup\$ Does this handle the empty string case? \$\endgroup\$
    – clapp
    May 15, 2017 at 15:55
  • \$\begingroup\$ @ConfusedMr_C Yep, empty string ⇒ code 1. \$\endgroup\$
    – eush77
    May 15, 2017 at 16:44
3
\$\begingroup\$

JavaScript, 20 bytes

Add f= at the beginning and invoke like f(arg).

_=>_[0]==_.slice(-1)

f=_=>_[0]==_.slice(-1)

i.oninput = e => o.innerHTML = f(i.value);
<input id=i><pre id=o></pre>

Explanation

This function takes in an argument _. In the function body, _[0]==_.slice(-1) checks whether the first element of _ (at 0th index) equals the last element of it, and returns the appropriate true or false boolean.

\$\endgroup\$
3
\$\begingroup\$

PHP>=7.1, 23 Bytes

prints 1 for equal and nothing if the character is different

<?=$argn[0]==$argn[-1];
\$\endgroup\$
0
3
\$\begingroup\$

C#, 38 30 bytes

s=>s!=""&&s[0]==s[s.Length-1];

Saved 8 bytes thanks to @raznagul.

\$\endgroup\$
8
  • 1
    \$\begingroup\$ Instead of checking the length of s just compare it with "". Also you don't need the ?:-Operator. Using && has the same result. \$\endgroup\$
    – raznagul
    May 15, 2017 at 15:28
  • \$\begingroup\$ @raznagul Good spots thanks, I can't check if it works at the moment so hopefully it does! Also wouldn't & have the same effect too? \$\endgroup\$ May 15, 2017 at 15:32
  • \$\begingroup\$ @TheLeathalCoder: No just & doesn't work. With && the second expression is not validated if the first expression is false. With & the seconds expression is always validated and fails with a IndexOutOfRangeException on the empty string test case. \$\endgroup\$
    – raznagul
    May 15, 2017 at 15:38
  • \$\begingroup\$ @raznagul Oh yeah... brain fart. \$\endgroup\$ May 15, 2017 at 15:40
  • \$\begingroup\$ Perhaps its a bit late but you can save 5 bytes if you use s.Last() instead of s[s.Length-1] \$\endgroup\$
    – Bojan B
    May 16, 2017 at 11:04
3
\$\begingroup\$

R, 40 bytes

function(x)x>""&&rev(y<-charToRaw(x))==y

Thanks to Nitrodon for -2 bytes.

Thanks to MickyT for -8 bytes.

Test:

f=function(x)x>""&&rev(y<-charToRaw(x))==y
test <- c("10h01", "Nothing", "Acccca", "wow!", "wow", "H", "")
sapply(test, f)
all(sapply(test, f) == c(T, F, F, F, T, T, F))

Output:

> f=function(x)x>""&&rev(y<-charToRaw(x))==y
> test <- c("10h01", "Nothing", "Acccca", "wow!", "wow", "H", "")
> sapply(test, f)
  10h01 Nothing  Acccca    wow!     wow       H         
   TRUE   FALSE   FALSE   FALSE    TRUE    TRUE   FALSE 
> all(sapply(test, f) == c(T, F, F, F, T, T, F))
[1] TRUE
\$\endgroup\$
3
  • 2
    \$\begingroup\$ You can remove one set of parentheses with rev(y<-el(strsplit(x,"")))==y. \$\endgroup\$
    – Nitrodon
    May 15, 2017 at 3:57
  • 1
    \$\begingroup\$ also unnamed functions are acceptable, so you can remove the f= \$\endgroup\$
    – MickyT
    May 15, 2017 at 20:35
  • 1
    \$\begingroup\$ and charToRaw can be used to split the string for comparison function(x)x>""&&rev(y<-charToRaw(x))==y \$\endgroup\$
    – MickyT
    May 15, 2017 at 20:50
3
\$\begingroup\$

><>, 39 33 bytes

 2i&01. >~&-?v1v
  i:1+?!^01. >0>n;

This is my first time both using ><> and playing code golf, so helpful suggestions would be appreciated.

The code is in three basic sections.

2i&01. Pushes an arbitrary number (2 in this case, this causes an empty string to print 0) onto the stack and puts the input's first character in the register.

>i:1+?!^01. Main loop. Pushes the next character onto the stack. If the string has been read completely, then go to the last section

>~&-?v1v
     >0>n;  Compare the first and last characters. Print 1 if they're the same, 0 if not
\$\endgroup\$
1
  • \$\begingroup\$ Hello! Welcome to PPCG! Nice first golf! Good luck for future! :) \$\endgroup\$
    – Arjun
    May 17, 2017 at 9:46
3
\$\begingroup\$

shortC, 27 26 25 24 bytes

f(C*s){T*s&&*s==s[Ss)-1]

How it works:

f(C*s){                     declare int-returning function that takes a string
       T                    return
        *s                  provided string has any length
          &&                and
            *s==s[Ss)-1]    the first character equals the last one

Try it online!

\$\endgroup\$
6
  • 2
    \$\begingroup\$ Wouldn't it be T instead of R? \$\endgroup\$
    – Okx
    May 13, 2017 at 20:34
  • \$\begingroup\$ @Okx Wow, can't believe I missed that!! Thanks! \$\endgroup\$
    – MD XF
    May 13, 2017 at 20:34
  • 1
    \$\begingroup\$ @MDXF, I like your shortC concept! I will be sending you some suggestions. \$\endgroup\$
    – jdt
    May 15, 2017 at 18:23
  • \$\begingroup\$ Yes, will chat soon. \$\endgroup\$
    – jdt
    May 15, 2017 at 18:32
  • \$\begingroup\$ For me it is not ok for gain just one char make possible "Ss)-1" it has to be "S(s)-1"...(if I understand well...) \$\endgroup\$
    – user58988
    May 20, 2017 at 9:20
3
\$\begingroup\$

Google Sheets, 33 Bytes

Takes input from cell [A1] and outputs 1 for truthy input and 0 for falsey input.

=(A1<>"")*Exact(Left(A1),Right(A1

It is noted that the parentheticals in Exact( and Right( are left unclosed as Google Sheets automatically corrects this as soon as the user has input the formula text and pressed enter to leave that cell.

Output

GS Version

\$\endgroup\$
3
  • \$\begingroup\$ Does the version of Excel matter? In my copy of 2013, this fails because you can't use & like that. Also, it considers A=a to be true. The shortest I can get is 38 bytes: =AND(EXACT(LEFT(A1),RIGHT(A1)),A1<>"") or the alternative =IFERROR(CODE(A1)=CODE(RIGHT(A1)),1=0). \$\endgroup\$ Mar 26, 2018 at 18:43
  • \$\begingroup\$ I tried it in Excel Online (16.0.9222.5051) and it returns TRUE for any non-error input. (screenshot) Does it work in your copy for all test cases? ExcelGuy has an answer that ends up like mine above for the same reasons. \$\endgroup\$ Mar 27, 2018 at 14:24
  • 1
    \$\begingroup\$ @EngineerToast you are completely correct, I should have been using * instead & for the binary and statement, but that still leaves the "A"="a" issue, which I had completely overlooked. All of that and a bit of syntax corrections leads me to =EXACT(LEFT(A1),RIGHT(A1))*(A1<>"") for 35, but I have switched the language to Google Sheets, which allowed me to drop the terminal double parenthetical in the Exact statement, rendering =(A1<>"")*Exact(Left(A1),Right(A1 for 33 bytes \$\endgroup\$ Mar 28, 2018 at 17:06
3
\$\begingroup\$

R, 50 43 41 40 64

Second solution with 41 bytes for a callable function - thanks to @niczky12 & @Giuseppe - amended for x=""

r=function(x,y=utf8ToInt(x))ifelse(x=="","FALSE",(y==rev(y))[1])

First with 50 bytes but not for the challenge

function(x){charToRaw(x)[1]==rev(charToRaw(x))[1]}
\$\endgroup\$
6
  • \$\begingroup\$ You can replace charToRaw with utf8ToInt to produce NAs when the string is empty. \$\endgroup\$
    – niczky12
    Mar 27, 2018 at 10:09
  • \$\begingroup\$ You can also remove the curly braces {} around the function body. \$\endgroup\$
    – Giuseppe
    Mar 27, 2018 at 10:13
  • \$\begingroup\$ I think (y==rev(y))[1] is shorter by a byte \$\endgroup\$
    – Giuseppe
    Mar 28, 2018 at 0:44
  • \$\begingroup\$ This challenge requires using only one Truthy and one Falsey value, but this produces NA for empty string but FALSE for "ab". Try it online!. \$\endgroup\$ Mar 28, 2018 at 17:21
  • \$\begingroup\$ @ØrjanJohansen thanks for your comment, so "ab" should not give FALSE? \$\endgroup\$ Mar 28, 2018 at 20:26
3
\$\begingroup\$

Mornington Crescent, 2585 bytes

Take Northern Line to Bank
Take Circle Line to Hammersmith
Take District Line to Mile End
Take District Line to Bank
Take Northern Line to Charing Cross
Take Northern Line to Charing Cross
Take Northern Line to Bank
Take Circle Line to Hammersmith
Take District Line to Upminster
Take District Line to Hammersmith
Take Circle Line to Victoria
Take District Line to Parsons Green
Take District Line to Bank
Take Circle Line to Hammersmith
Take District Line to Upney
Take District Line to Hammersmith
Take District Line to Bow Road
Take District Line to Acton Town
Take Piccadilly Line to Heathrow Terminals 1, 2, 3
Take Piccadilly Line to Acton Town
Take District Line to Acton Town
Take District Line to Parsons Green
Take District Line to Bank
Take Circle Line to Hammersmith
Take District Line to Barking
Take District Line to Hammersmith
Take District Line to Mile End
Take District Line to Bank
Take Circle Line to Bank
Take Northern Line to Charing Cross
Take Northern Line to Charing Cross
Take Northern Line to Bank
Take Circle Line to Bank
Take Circle Line to Notting Hill Gate
Take Circle Line to Notting Hill Gate
Take District Line to Upminster
Take District Line to Upminster
Take District Line to Temple
Take District Line to Barking
Take District Line to Bank
Take Circle Line to Hammersmith
Take District Line to Upney
Take District Line to Barking
Take District Line to Hammersmith
Take Circle Line to Aldgate
Take Circle Line to Aldgate
Take Metropolitan Line to Chalfont & Latimer
Take Metropolitan Line to Aldgate
Take Circle Line to Hammersmith
Take District Line to Upminster
Take District Line to Bank
Take Circle Line to Bank
Take Circle Line to Aldgate
Take Circle Line to Aldgate
Take Metropolitan Line to Chalfont & Latimer
Take Metropolitan Line to Moorgate
Take Circle Line to Moorgate
Take Northern Line to Angel
Take Northern Line to Bank
Take Circle Line to Temple
Take District Line to Bow Road
Take District Line to Upney
Take District Line to Bank
Take Circle Line to Hammersmith
Take District Line to Bow Road
Take District Line to Hammersmith
Take Circle Line to Aldgate
Take Circle Line to Aldgate
Take Metropolitan Line to Chalfont & Latimer
Take Metropolitan Line to Aldgate
Take Circle Line to Victoria
Take Circle Line to Aldgate
Take Circle Line to Aldgate
Take Metropolitan Line to Chalfont & Latimer
Take Metropolitan Line to Moorgate
Take Circle Line to Moorgate
Take Northern Line to Angel
Take Northern Line to Bank
Take District Line to Upney
Take District Line to Bank
Take Circle Line to Bank
Take Northern Line to Mornington Crescent

Try it online!

I thougt, this was easier...

Outputs 0 for false and 1 for true

// Prepare input for right substring
Take Northern Line to Bank
Take Circle Line to Hammersmith
Take District Line to Mile End

// Get char code of first character
Take District Line to Bank
Take Northern Line to Charing Cross
Take Northern Line to Charing Cross

// Put it in Upminster for later calculation
Take Northern Line to Bank
Take Circle Line to Hammersmith
Take District Line to Upminster

// Also save it for later check for null string # Victoria = char code of first
Take District Line to Hammersmith
Take Circle Line to Victoria

// Save 0 # Upney = 0, Bow Road = 0 (for second loop)
Take District Line to Parsons Green
Take District Line to Bank
Take Circle Line to Hammersmith
Take District Line to Upney
Take District Line to Hammersmith
Take District Line to Bow Road

// Save 1 # Barking = 1
Take District Line to Acton Town
Take Piccadilly Line to Heathrow Terminals 1, 2, 3
Take Piccadilly Line to Acton Town
Take District Line to Acton Town
Take District Line to Parsons Green
Take District Line to Bank
Take Circle Line to Hammersmith
Take District Line to Barking

// Get char code of last letter of input
Take District Line to Hammersmith
Take District Line to Mile End
Take District Line to Bank
Take Circle Line to Bank
Take Northern Line to Charing Cross
Take Northern Line to Charing Cross

// Bitwise NOT, so we get -(char count)-1
Take Northern Line to Bank
Take Circle Line to Bank
Take Circle Line to Notting Hill Gate
Take Circle Line to Notting Hill Gate

// Add it to the char code of first letter. If they match, the result is -1.
Take District Line to Upminster

// Save the result in Upminster
Take District Line to Upminster

// ********************************************************************************
// * Add some logic. This is quite puzzling because the only control structure    *
// * in Mornington Crescent is a post-test loop. You don't have something like    *
// * an if-else. You can't skip code. You can only define how often a loop should *
// * be repeated.                                                                 *
// * My solution to this is: When checking if the first and last character        *
// * are the same (first char code - last char code = 0), I swap the positions    *
// * of the 0 and 1 variables, then multiply the result of the equality check     *
// * with the new position of 1. If the characters are the same, the loop will    *
// * exit after the first run. If not, it will run another time, swapping the     *
// * 0 and 1 back and exiting after the second run, because it multiplies the     *
// * new equality check with 0.                                                   *
// ********************************************************************************

// Set starting line of loop
Take District Line to Temple

// Swap 0 and 1 # Upney = 1, Barking = 0 (on the first run)
Take District Line to Barking
Take District Line to Bank
Take Circle Line to Hammersmith
Take District Line to Upney
Take District Line to Barking

// Save 1 in Chalfont & Latimer for later multiplication
Take District Line to Hammersmith
Take Circle Line to Aldgate
Take Circle Line to Aldgate
Take Metropolitan Line to Chalfont & Latimer

// Add 1 to Upminster, to check for equality
Take Metropolitan Line to Aldgate
Take Circle Line to Hammersmith
Take District Line to Upminster

// Multiply the result with 1 in the first run, or 0 in the second run
Take District Line to Bank
Take Circle Line to Bank
Take Circle Line to Aldgate
Take Circle Line to Aldgate
Take Metropolitan Line to Chalfont & Latimer

// If the result is not 0, run the loop again
Take Metropolitan Line to Moorgate
Take Circle Line to Moorgate
Take Northern Line to Angel

// **********************************************************************
// * Another control loop to check for empty string.                    *
// * The previous loop will return true if an empty string was entered. *
// **********************************************************************

// Set start of loop
Take Northern Line to Bank
Take Circle Line to Temple

// Swap 0 and 1 # Upney = 0, Bow Road = 1 (on the first run, if previous loop ran once)
//              # Upney = 0, Bow Road = 0 (if previous loop ran twice)
Take District Line to Bow Road
Take District Line to Upney
Take District Line to Bank
Take Circle Line to Hammersmith
Take District Line to Bow Road

// store 1 in Chalfont & Latimer for later multiplication
Take District Line to Hammersmith
Take Circle Line to Aldgate
Take Circle Line to Aldgate
Take Metropolitan Line to Chalfont & Latimer

// Multiply by char code of first char (0 if empty string)
Take Metropolitan Line to Aldgate
Take Circle Line to Victoria
Take Circle Line to Aldgate
Take Circle Line to Aldgate
Take Metropolitan Line to Chalfont & Latimer

// Loop again if input not empty
Take Metropolitan Line to Moorgate
Take Circle Line to Moorgate
Take Northern Line to Angel

// Fetch result and go home for output
Take Northern Line to Bank
Take District Line to Upney
Take District Line to Bank
Take Circle Line to Bank
Take Northern Line to Mornington Crescent
\$\endgroup\$
3
\$\begingroup\$

BRASCA, 3 bytes

This looks like a job for M!

M=n

Try it online!

Explanation

M        - Move the bottom of the stack to the top
 =       - Are they the same? Push 1 if it is, 0 if it isnt.
  n      - Output as number.
\$\endgroup\$
3
\$\begingroup\$

Knight, 20 bytes

O<!=xP?AxA Gx-Lx 1 1

Try it online!

Takes a line from stdin and prints true if equal and false otherwise.

O                           Output the result of:
  < ! (= x P)                 x = input; x is not null and
    ? A x                       the first char's ord is equal to
      A (G x (- L x 1) 1)       the (length-1)th char's ord
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You could save a few bytes by going Gx-Lx 1 1 to Gx-LxT1 \$\endgroup\$
    – Sampersand
    Aug 15, 2022 at 0:42
3
\$\begingroup\$

Swift, 46 bytes

var s=readLine()!,a=Array(s);a[0]==a.last ?1:0
\$\endgroup\$
3
  • \$\begingroup\$ Edited the code. \$\endgroup\$
    – Leena
    May 14, 2017 at 17:23
  • \$\begingroup\$ Welcome to PPCG! Is the space after a.last necessary? \$\endgroup\$
    – hyper-neutrino
    May 14, 2017 at 22:11
  • \$\begingroup\$ Either I can add brackets around a.last or I can add space after a.last \$\endgroup\$
    – Leena
    May 15, 2017 at 16:55

Your Answer

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

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