17
\$\begingroup\$

Challenge

You have one string of input bytes, output the last byte in it.

Rules

Your submission may be a program or function outputting the last byte in the input which

  • is either a string, stdin or command-line arguments, and
  • is non-empty.

I was trying to solve this with brainfuck, however all languages are allowed to participate. This is .

Examples

"?" -> "?"
"29845812674" -> "4"
\$\endgroup\$
6
  • 5
    \$\begingroup\$ Welcome, I changed your question to fit our format more properly (note this is what the sandbox is for, usually). However in its current state the challenge is very easy (also in bf), so not sure about that. \$\endgroup\$ – ბიმო Mar 17 '19 at 19:11
  • 12
    \$\begingroup\$ I vote against closing; it may be trivial, but that doesn't make it offtopic \$\endgroup\$ – MilkyWay90 Mar 17 '19 at 22:30
  • 1
    \$\begingroup\$ @MillyWay I think most of the close votes were before the extensive edit by ბიმო \$\endgroup\$ – Sanchises Mar 18 '19 at 6:47
  • 10
    \$\begingroup\$ @ბიმო We have a consensus not to edit off-topic questions to make them on-topic which I think would have applied here. \$\endgroup\$ – Laikoni Mar 18 '19 at 7:19
  • 2
    \$\begingroup\$ What kind of string? Is it guaranteed to be ASCII only? Or should we handle UTF-8 (and how?) for example? \$\endgroup\$ – FireCubez Mar 18 '19 at 18:28

95 Answers 95

2
\$\begingroup\$

VBA (Excel), 14 12 bytes

using Immediate Window and Cell A1 as input

Thanks @tsh

?[RIGHT(A1)] or ?Right([A1],1)

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Is 1 optional? \$\endgroup\$ – tsh Mar 18 '19 at 8:54
  • \$\begingroup\$ not on the second code. Thanks :) \$\endgroup\$ – remoel Mar 18 '19 at 8:59
2
\$\begingroup\$

Python 3, 11 18 34 Bytes

import sys;print(sys.argv[-1][-1])

Usage via running the program as a python script on the command line. Input is provided as the last argument to the program.

Try it online!

\$\endgroup\$
7
  • \$\begingroup\$ This does not print anything or return anything from a function - snippets are not allowed, only functions or full programs. \$\endgroup\$ – Stephen Mar 18 '19 at 0:08
  • \$\begingroup\$ Ah I see, didn’t think about that when I though of the answer. Only thought about running it in the interpreter. \$\endgroup\$ – Mrwerdo Mar 18 '19 at 0:24
  • \$\begingroup\$ 38 \$\endgroup\$ – ASCII-only Mar 18 '19 at 0:41
  • 1
    \$\begingroup\$ 14 \$\endgroup\$ – ASCII-only Mar 18 '19 at 0:44
  • 1
    \$\begingroup\$ How about this? \$\endgroup\$ – Mrwerdo Mar 18 '19 at 0:51
2
\$\begingroup\$

IBM/Lotus Notes Formula, 11 bytes

@Right(i;1)

Computed field formula taking its input from editable field i

enter image description here

\$\endgroup\$
2
\$\begingroup\$

Turing Machine Code, 72 42 bytes

Assumes an input with no empty cells (spaces). Thanks to ASCII-only for saving 30 bytes.

0 * * r 1
1 * * l 2
1 _ _ l halt
2 * _ r 0

Old version in 72 bytes:

0 * * r 0
0 _ * l 1
1 * * l 2
2 * _ l 2
2 _ _ r 3
3 _ _ r 3
3 * * * halt

Try it online.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ 0 * * r 1/1 * * l 2/1 _ _ l halt/2 * _ r 0? \$\endgroup\$ – ASCII-only Mar 18 '19 at 0:22
  • \$\begingroup\$ oi pls reply :|| \$\endgroup\$ – ASCII-only Mar 18 '19 at 0:35
  • \$\begingroup\$ wouldn't work in what way? I've tested it online \$\endgroup\$ – ASCII-only Mar 18 '19 at 1:46
  • \$\begingroup\$ @ASCII-only It turns out you're correct, and I was simply misinterpreting the way your program actually worked. I think it's different enough that you can post it as a different answer if you want to. \$\endgroup\$ – SuperJedi224 Mar 18 '19 at 2:42
  • \$\begingroup\$ Well, this is a simple challenge, don't think it needs more than one answer in any language :P \$\endgroup\$ – ASCII-only Mar 18 '19 at 3:22
2
\$\begingroup\$

C# 8.0, 8 bytes

Requires .NET Core 3.0, which is in beta. This currently crashes the CLR due to a bug, but once the bug is fixed, this will run as expected and fulfill the challenge requirements.

s=>s[^1]

C# 8.0, Runs without crashing at time of writing, 22 bytes

s=>s.ToCharArray()[^1]

C# 8.0, Full Program, 78 bytes

using C=System.Console;class A{static void Main(){C.Write(C.ReadLine()[^1]);}}

\$\endgroup\$
5
  • \$\begingroup\$ The challenge mentions "output", so the last solution is probably the right one \$\endgroup\$ – Ven Mar 18 '19 at 16:48
  • \$\begingroup\$ Dang, you outdid my console answer by a lot. How does the ^1 work? \$\endgroup\$ – Stackstuck Mar 18 '19 at 20:21
  • \$\begingroup\$ It's the new Index type. Starting an index with a caret indicates it's from the end, i.e. array[^n] is the same as array[array.Length - n] \$\endgroup\$ – Arcanox Mar 18 '19 at 20:24
  • \$\begingroup\$ Interesting! I always try to stay up to date with new C# features. Do you have any link / reference on that? \$\endgroup\$ – mortb Mar 19 '19 at 9:00
  • 1
    \$\begingroup\$ Found it: docs.microsoft.com/en-us/dotnet/core/whats-new/dotnet-core-3-0 \$\endgroup\$ – mortb Mar 19 '19 at 9:06
2
\$\begingroup\$

Forth (gforth), 17 bytes

: f 1- + 1 type ;

Try it online!

Explanation

Adds string-length - 1 to the string address and then prints a string of length 1 starting at that address.

Code Explanation

: f        \ start a new word definition
  1-       \ subtract 1 from string length
  +        \ add result to string address
  1 type   \ print string of length 1 starting at the new address
;          \ end word definition
\$\endgroup\$
2
\$\begingroup\$

Retina, 10 9 bytes

(.|¶)*
$1

Try it online!

\$\endgroup\$
2
\$\begingroup\$

LiveScript, 8 bytes

(.[*-1])

Explanation:

(.[*-1])
(.[*-1]) # "BIOP": operator section à la Haskell
 .[   ]  # Index into the implicit argument
   *-1   # In [], "*" refers to the length
\$\endgroup\$
2
\$\begingroup\$

F#, 14 8 bytes

Seq.last

-6 bytes thanks to aloisdg.

Strings are treated as sequences in F#, so you can use the Seq.last function to get the last character in it.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Seq.last is a function. You can remove the let s= \$\endgroup\$ – aloisdg Mar 19 '19 at 8:19
2
\$\begingroup\$

C (gcc), 31 bytes

f(int*s){gets(s),printf("%s");}

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ O_o how does this even work \$\endgroup\$ – ASCII-only Mar 31 '19 at 10:52
  • \$\begingroup\$ I have no idea, it just works. \$\endgroup\$ – Natural Number Guy Mar 31 '19 at 14:07
  • \$\begingroup\$ Impressive list of complaints from the compiler! \$\endgroup\$ – roblogic Sep 27 '19 at 5:00
2
\$\begingroup\$

Caboose, 1514 bytes

Caboose hates me, because it does. There isn't any convenient string-indexing instruction in Caboose!

var a=input();print(a.endsWith(' ')and' 'or a.endsWith('!')and'!'or a.endsWith('"')and'"'or a.endsWith('#')and'#'or a.endsWith('$')and'$'or a.endsWith('%')and'%'or a.endsWith('&')and'&'or a.endsWith("'")and"'"or a.endsWith('(')and'('or a.endsWith(')')and')'or a.endsWith('*')and'*'or a.endsWith('+')and'+'or a.endsWith(',')and','or a.endsWith('-')and'-'or a.endsWith('.')and'.'or a.endsWith('/')and'/'or a.endsWith('0')and'0'or a.endsWith('1')and'1'or a.endsWith('2')and'2'or a.endsWith('3')and'3'or a.endsWith('4')and'4'or a.endsWith('5')and'5'or a.endsWith('6')and'6'or a.endsWith('7')and'7'or a.endsWith('8')and'8'or a.endsWith('9')and'9'or a.endsWith(':')and':'or a.endsWith(';')and';'or a.endsWith('<')and'<'or a.endsWith('=')and'='or a.endsWith('>')and'>'or a.endsWith('?')and'?'or a.endsWith('@')and'@'or a.endsWith('A')and'A'or a.endsWith('B')and'B'or a.endsWith('C')and'C'or a.endsWith('D')and'D'or a.endsWith('E')and'E'or a.endsWith('F')and'F'or a.endsWith('G')and'G'or a.endsWith('H')and'H'or a.endsWith('I')and'I'or a.endsWith('J')and'J'or a.endsWith('K')and'K'or a.endsWith('L')and'L'or a.endsWith('M')and'M'or a.endsWith('N')and'N'or a.endsWith('O')and'O'or a.endsWith('P')and'P'or a.endsWith('Q')and'Q'or a.endsWith('R')and'R'or a.endsWith('S')and'S'or a.endsWith('T')and'T'or a.endsWith('U')and'U'or a.endsWith('V')and'V'or a.endsWith('W')and'W'or a.endsWith('X')and'X'or a.endsWith('Y')and'Y'or a.endsWith('Z')and'Z'or a.endsWith('[')and'['or a.endsWith('\\')and'\\'or a.endsWith(']')and']'or'~');

If I add more constants, then Caboose will say that there are too many constants in the chunk. Fortunately it passes all test cases given. Basically it (tries to) check the last character against all characters in printable ASCII.

TIO

\$\endgroup\$
2
\$\begingroup\$

Scratch 3.0, 7 blocks/68 bytes

enter image description here

or, as scratchblocks syntax

when gf clicked
ask()and wait
say(letter(length of(answer))of(answer

Try it on scratch

Did I mention this was done 100% on mobile? Because it was really hard making this, but I think it was worth it.

\$\endgroup\$
2
\$\begingroup\$

naz, 38 bytes

2a2x1v1x1f1r3x1v2e2x2v1f0x1x2f2v1o0x1f

Works for any input string terminated with the control character STX (U+0002).

Explanation (with 0x commands removed)

2a2x1v             # Set variable 1 equal to 2
1x1f1r3x1v2e2x2v1f # Function 1
                   # Read a byte of input
                   # Jump to function 2 if it equals variable 1
                   # Otherwise, store it in variable 2,
                   # then jump back to the start of the function
1x2f2v1o           # Function 2
                   # Load variable 2 into the register and output it
1f                 # Call function 1
\$\endgroup\$
2
\$\begingroup\$

MAWP, 2 bytes

|;

Outputs top of stack.

Try it!

\$\endgroup\$
2
\$\begingroup\$

dotcomma, 48 bytes

[[[],],][.[[[,.][.[,]].,][[,].[,]].]][,[,].[,.]]

Try it online!

Because dotcomma is queue-based, you can't easily access the last character. My solution is to reverse the input and then output the first character of it.

Code:

                # reverse the input
[[[],],]        mark the end with 0 0
[.[             while there are characters in input
  [[,.]         put a character on the recursion stack
    [.[,]]      go to reverse section
  .,]           save the character in the reverse section
  [[,].[,]]     go to input section
.]]

[               # get first character and delete all others
  ,[,]          delete 0 and append the next character (last of input) to the end of the queue
  .[,.]         delete everything until we reach a 0
]
                implicitly output the queue

dotcomma (experimental), 17 bytes

[,][[],][.[,.]].,

Try it online!

I marked this "experimental" because the language dotcomma is still very young and I don't know if this is intentional behaviour. If you pass input as a list of strings it reverses each string, so you only need to extract the first letter.

[,]      put first character on recursion stack
[[],]    put 0 (end of queue marker) on queue
[.[,.]]  delete everything from queue
.,       write back character
\$\endgroup\$
5
  • 1
    \$\begingroup\$ Wasn't even sure this was possible! Would you be fine with me copying your code snippet to a github page so I can make a try it online link generator? \$\endgroup\$ – Redwolf Programs Oct 3 '20 at 20:06
  • \$\begingroup\$ I think, the language has everything it needs to be Turing complete. You can build loops, you can implement if- else statements (even easier than in other esolangs), you can calculate numbers, you can read and write data and I/O. Most things will be hard to program, but it should be able to do anything. Of course you can copy my code snippet. I'd be happy to help you gain more attention for your languages. \$\endgroup\$ – Dorian Oct 4 '20 at 8:54
  • \$\begingroup\$ PS: There are different versions of that snippet in my answers, depending on what type of input and output it uses. I didn't yet manage to auto- detect the input type. \$\endgroup\$ – Dorian Oct 4 '20 at 9:13
  • 1
    \$\begingroup\$ I finished my online interpreter. Also sorry it took so long, I had a lot of other projects :p \$\endgroup\$ – Redwolf Programs Oct 16 '20 at 14:37
  • \$\begingroup\$ Nice. There are some minor bugs but I updated my answer using your online interpreter. \$\endgroup\$ – Dorian Oct 19 '20 at 7:28
1
\$\begingroup\$

Wolfram Language (Mathematica), 16 bytes

#~StringTake~-1&

Try it online!

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

C# (.NET Core), 81 bytes, command-line input

class M{static void Main(string[] a){System.Console.Write(a[0][a[0].Length-1]);}}

Try it online!

If you run this from an actual command line, you will need to wrap your string in quotes if it contains spaces.

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

C# (.NET Core), 115 bytes, console input

using C=System.Console;class M{static void Main(string[] a){int c=0,d;while((d=C.Read())>-1)c=d;C.Write((char)c);}}

Try it online!

This feels kinda janky, but it does work. Interestingly, I can't save any bytes with a for loop as the code stands.

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

Perl 6, 11 bytes

*.comb[*-1]

Try it online!

Anonymous Whatever lambda that takes a string, splits it into characters, and returns the last one.

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

Batch, 24 bytes

@set s=%1
@echo %s:~-1%

Takes input as a command-line argument. Note that arguments can't include special characters or spaces, but you can fake arguments with spaces in this case by preceding it with a ", which results in a single argument that begins with ", however there is no easy solution for arguments that include special characters. Batch can't easily read "all of stdin". To read up to but not including the first newline itself would however be a byte shorter:

@set/ps=
@echo %s:~-1%

Edit: A version that handles arbitrary characters in a (quoted) argument for 92 bytes:

@set s="%~1"
@set "s=%s:~-2,1%
@if "%s%"=="" (echo ^")else for %%s in ("%s%")do @echo %%~s

Explanation: The first line makes a copy of the argument in a variable and ensures that it is quoted. The second argument then takes the second last character (because the quote is now the last character). However, if that was also a quote then this results in an empty variable, so we need to special-case that and output a (quoted) quote. Otherwise, we still need to quote the character in case it is a special character as echoing "%s%" will echo the quotes and echoing %s% will actually interpret special characters, so the variable needs to be quoted to allow it to be parsed but then immediately unquoted so it can be printed. This is achieved using the for command. 86 bytes to read up to but not including the first newline from stdin while supporting special characters:

@set/ps=
@set "s=%s:~-1%
@if "%s%"=="" (echo ^")else for %%s in ("%s%")do @echo %%~s
\$\endgroup\$
1
\$\begingroup\$

Scratch 3.0, scratchblocks3 syntax

As a function, 61 bytes

define l
ask[]and wait
say(letter(length of(answer))of(answer

As a full program, 68 bytes

when gf clicked
ask[]and wait
say(letter(length of(answer))of(answer

Try both online

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

Befunge-98, 5 bytes

~2j@,

Try it online!

Explanation:

~           Take input
 2j         Skip next two instructions
~           Repeat until EOF, where it reflects
   @,       Print the last character and exit
\$\endgroup\$
1
\$\begingroup\$

C# (Visual C# Interactive Compiler), 11 bytes

x=>x.Last()

Try it online!

\$\endgroup\$
8
  • \$\begingroup\$ you can use a function :P \$\endgroup\$ – ASCII-only Mar 18 '19 at 0:47
  • \$\begingroup\$ OK - Ill update my answer :) \$\endgroup\$ – dana Mar 18 '19 at 1:21
  • 1
    \$\begingroup\$ uhm why is testcase in STDIN not footer \$\endgroup\$ – ASCII-only Mar 18 '19 at 1:38
  • \$\begingroup\$ Wow - It's been a while. Thanks for pointing that out ;) \$\endgroup\$ – dana Mar 18 '19 at 1:39
  • 1
    \$\begingroup\$ That's not how I understand it. I understand that it should print the last non-empty character. We need to ask OP. \$\endgroup\$ – Ven Mar 18 '19 at 17:33
1
\$\begingroup\$

Japt, 1 byte

Ì

Try it online!

-1 byte thanks to Quintec!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 1 byte \$\endgroup\$ – Quintec Mar 18 '19 at 2:00
1
\$\begingroup\$

Whitespace, 54 bytes

[N
S S N
_Create_Label_LOOP][S S S N
_Push_0][S N
S _Duplicate_0][T   N
T   S _Read_STDIN_as_character][T   T   T   _Retrieve_input][S N
S _Duplicate][S S S T   S T S N
_Push_10][T S S T   _Subtract][N
T   S S N
_If_0_Jump_to_Label_PRINT][N
S N
N
_Jump_to_Label_LOOP][N
S S S N
_Create_Label_PRINT][S N
N
_Discard_top][T N
S S _Print_as_character]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Since Whitespace can only take input as integer or character, we must add a trailing character to indicate we're done with the input-string after reading it character by character, for which I've used a newline.

Try it online (with raw spaces, tabs and new-lines only).

Example run: input = A2#

Command    Explanation                   Stack                Heap    STDIN STDOUT STDERR

NSSN       Create Label_LOOP             []
 SSSN      Push 0                        [0]
 SNS       Duplicate top (0)             [0,0]
 TNTS      Read STDIN as character       [0]                  {0:65}  A
 TTT       Retrieve at heap address (0)  [65]                 {0:65}
 SNS       Duplicate top (65)            [65,65]              {0:65}
 SSSTSTSN  Push 10                       [65,65,10]           {0:65}
 TSST      Subtract top two (65-10)      [65,55]              {0:65}
 NTSSN     If 0: Jump to Label_PRINT     [65]                 {0:65}
 NSNN      Jump to Label_LOOP            [65]                 {0:65}
 
 SSSN      Push 0                        [65,0]
 SNS       Duplicate top (0)             [65,0,0]
 TNTS      Read STDIN as character       [65,0]               {0:50}  2
 TTT       Retrieve at heap address (0)  [65,50]              {0:50}
 SNS       Duplicate top (50)            [65,50,50]           {0:50}
 SSSTSTSN  Push 10                       [65,50,50,10]        {0:50}
 TSST      Subtract top two (50-10)      [65,50,40]           {0:50}
 NTSSN     If 0: Jump to Label_PRINT     [65,50]              {0:50}
 NSNN      Jump to Label_LOOP            [65,50]              {0:50}

 SSSN      Push 0                        [65,50,0]
 SNS       Duplicate top (0)             [65,50,0,0]
 TNTS      Read STDIN as character       [65,50,0]            {0:35}  #
 TTT       Retrieve at heap address (0)  [65,50,35]           {0:35}
 SNS       Duplicate top (35)            [65,50,35,35]        {0:35}
 SSSTSTSN  Push 10                       [65,50,35,35,10]     {0:35}
 TSST      Subtract top two (35-10)      [65,50,35,25]        {0:35}
 NTSSN     If 0: Jump to Label_PRINT     [65,50,35]           {0:35}
 NSNN      Jump to Label_LOOP            [65,50,35]           {0:35}

 SSSN      Push 0                        [65,50,35,0]
 SNS       Duplicate top (0)             [65,50,35,0,0]
 TNTS      Read STDIN as character       [65,50,35,0]         {0:10}  \n
 TTT       Retrieve at heap address (0)  [65,50,35,10]        {0:10}
 SNS       Duplicate top (10)            [65,50,35,10,10]     {0:10}
 SSSTSTSN  Push 10                       [65,50,35,10,10,10]  {0:10}
 TSST      Subtract top two (10-10)      [65,50,35,10,0]      {0:10}
 NTSSN     If 0: Jump to Label_PRINT     [65,50,35,10]        {0:10}

NSSSN      Create Label_PRINT            [65,50,35,10]        {0:10}
 SNN       Discard top                   [65,50,35]           {0:10}
 TNSS      Print as character to STDOUT  [65,50]              {0:10}        #
                                                              {0:10}               error

Stops with the error: Exit not defined.

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

Japt -h, 1 byte

U

Run it online

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

Runic Enchantments, 5 bytes

i1Z%@

Try it online!

Note that input handling in Runic has implicit conversion and breaks on spaces. \ denotes a literal space (works on newlines too) and numerical values are never strings.

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

Binary-Encoded Golfical, 17 bytes

Hex-dump of binary encoded file:

00 60 02 1b 1a 08 01 14
16 14 24 1d 0a 01 14 18
14

Original image:

enter image description here

Magnified 45x with colors labeled:

enter image description here

The original image (the tiny one, not the magnified version) can be run using the interpreter normally. The binary encoded file (of which a hexdump is included above) can either be transpiled back to the image version with the Encoder program included in the github repo, or run directly using the interpreter by adding the -x flag.

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

Chip -z, 41 bytes

S
>vvvvvv~t
ABCDEFG
|Zz||Zz
zbcZzfg
a  de

Try it online!

Assumes that either the byte string does not contain zero (\0), or that it designates the end of the string.


Alternate solution (45 bytes):

azABZbczCDZdezEFZfgzG
S-^^----^^----^^----^~t

Try it online!

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

R, 35 bytes

Takes the input, splits it in to a list, outputs the last element of the list.

tail(strsplit(scan(,''),'')[[1]],1)

Try it online!

\$\endgroup\$
2

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.