# 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"


## The Catalogue

The Stack Snippet at the bottom of this post generates the catalogue from the answers a) as a list of shortest solution per language and b) as an overall leaderboard.

## Language Name, N bytes


where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

## Ruby, <s>104</s> <s>101</s> 96 bytes


If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

## Perl, 43 + 2 (-p flag) = 45 bytes


You can also make the language name a link which will then show up in the snippet:

## [><>](https://esolangs.org/wiki/Fish), 121 bytes


/* Configuration */

var QUESTION_ID = 181627; // Obtain this from the url
// It will be like https://XYZ.stackexchange.com/questions/QUESTION_ID/... on any question page
var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk";
var OVERRIDE_USER = 8478; // This should be the user ID of the challenge author.

/* App */

return "https://api.stackexchange.com/2.2/questions/" +  QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER;
}

}

jQuery.ajax({
method: "get",
dataType: "jsonp",
crossDomain: true,
success: function (data) {
data.items.forEach(function(a) {
});
comment_page = 1;
}
});
}

jQuery.ajax({
method: "get",
dataType: "jsonp",
crossDomain: true,
success: function (data) {
data.items.forEach(function(c) {
if (c.owner.user_id === OVERRIDE_USER)
});
else process();
}
});
}

var SCORE_REG = /<h\d>\s*([^\n,<]*(?:<(?:[^\n>]*>[^\n<]*<\/[^\n>]*>)[^\n,<]*)*),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/;

function getAuthorName(a) {
return a.owner.display_name;
}

function process() {
var valid = [];

var body = a.body;
if(OVERRIDE_REG.test(c.body))
body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>';
});

var match = body.match(SCORE_REG);
if (match)
valid.push({
user: getAuthorName(a),
size: +match[2],
language: match[1],
});
else console.log(body);
});

valid.sort(function (a, b) {
var aB = a.size,
bB = b.size;
return aB - bB
});

var languages = {};
var place = 1;
var lastSize = null;
var lastPlace = 1;
valid.forEach(function (a) {
if (a.size != lastSize)
lastPlace = place;
lastSize = a.size;
++place;

.replace("{{NAME}}", a.user)
.replace("{{LANGUAGE}}", a.language)
.replace("{{SIZE}}", a.size)

var lang = a.language;
lang = jQuery('<a>'+lang+'</a>').text();

languages[lang] = languages[lang] || {lang: a.language, lang_raw: lang, user: a.user, size: a.size, link: a.link};
});

var langs = [];
for (var lang in languages)
if (languages.hasOwnProperty(lang))
langs.push(languages[lang]);

langs.sort(function (a, b) {
if (a.lang_raw.toLowerCase() > b.lang_raw.toLowerCase()) return 1;
if (a.lang_raw.toLowerCase() < b.lang_raw.toLowerCase()) return -1;
return 0;
});

for (var i = 0; i < langs.length; ++i)
{
var language = jQuery("#language-template").html();
var lang = langs[i];
language = language.replace("{{LANGUAGE}}", lang.lang)
.replace("{{NAME}}", lang.user)
.replace("{{SIZE}}", lang.size)
language = jQuery(language);
jQuery("#languages").append(language);
}

}
body {
text-align: left !important;
display: block !important;
}

width: 290px;
float: left;
}

#language-list {
width: 500px;
float: left;
}

font-weight: bold;
}

table td {
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="language-list">
<h2>Shortest Solution by Language</h2>
<table class="language-list">
<tr><td>Language</td><td>User</td><td>Score</td></tr>
<tbody id="languages">

</tbody>
</table>
</div>
<tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr>

</tbody>
</table>
</div>
<table style="display: none">
</tbody>
</table>
<table style="display: none">
<tbody id="language-template">
</tbody>
</table>

• 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. – ბიმო Mar 17 '19 at 19:11
• I vote against closing; it may be trivial, but that doesn't make it offtopic – MilkyWay90 Mar 17 '19 at 22:30
• @MillyWay I think most of the close votes were before the extensive edit by ბიმო – Sanchises Mar 18 '19 at 6:47
• @ბიმო We have a consensus not to edit off-topic questions to make them on-topic which I think would have applied here. – Laikoni Mar 18 '19 at 7:19
• What kind of string? Is it guaranteed to be ASCII only? Or should we handle UTF-8 (and how?) for example? – FireCubez Mar 18 '19 at 18:28

# Attache, 4 bytes

Last


Try it online! (If the input could be a list of characters, &/S could work.)

## Alternatives

5 bytes: @&-1

8 bytes: &/S@List

10 bytes: @«_,-1»

10 bytes: Fold!Right

10 bytes: @<~_,-1~>

10 bytes: ^^&:Right

10 bytes: {Right^^_}

11 bytes: Get«_,-1»

11 bytes: Get<~_,-1~>

12 bytes: @«_,#_-1»

12 bytes: @<~_,#_-1~>

13 bytes: Get«_,#_-1»

13 bytes: Get<~_,#_-1~>

• :| wtf so many alternatives – ASCII-only Mar 18 '19 at 0:19
• @ASCII-only Least I could do on a simple challenge like this :p – Conor O'Brien Mar 18 '19 at 0:41
• Print Last inputted byte. The programs contents fit with the challenge – MilkyWay90 Mar 19 '19 at 23:53

# x86 Machine Code, 2 bytes

As @CodyGray correctly points out, taking input as a string and output to a register removes the bulk of the standalone program version.

Input string is in SI, length in CX and output character is in AL:

F3 AC  REPZ LODSB      ; start at memory location pointer in SI, put next value in AL,
; loop CX number of times. The last char will be in AL when done.


Or 4 bytes as a "Pascal string" (length is prepended to beginning of string):

AC     LODSB           ; first byte is string length
91     XCHG AX, CX     ; move length to CX for loop
F3 AC  REPZ LODSB      ; start at memory location pointer in SI, put next value in AL,
; loop CX number of times. The last char will be in AL when done.


Or 5 bytes as a "C string" (zero/null terminated), input in DI:

F2 AE     REPNZ SCASB     ; scan for value in AL (0), end when found and advance DI
8A 45 FE  MOV AL, [DI-2]  ; DI is now two bytes ahead of last, put value of DI-2 into AL


# IBM PC DOS, 8088 Assembly, 12 11 bytes

Or as complete program as IBM PC DOS executable. Input is from command line, output is to console.

D1 EE   SHR  SI, 1      ; SI to DOS PSP at 80H (SI is 0100H at runtime)
AC      LODSB           ; load command line length in AL
91      XCHG AX, CX     ; move length to CX for loop
F3/ AC  REPZ LODSB      ; load next char into AL, repeat until CX = 0
B4 0E   MOV  AH, 0EH    ; PC BIOS write to screen function
CD 10   INT  10H        ; display
C3      RET             ; exit to DOS


Output:

• Although this certainly gets style points, from a strict golfing point of view, it's worth noting that you are allowed to write functions that return the result in a register. So, this can get a lot shorter. You can trivially eliminate 4 bytes, and a rewrite could shrink it down even further. By the way, is that screenshot from an emulator? Which one? – Cody Gray Mar 19 '19 at 4:31
• @CodyGray, oh absolutely, the code to take input from command line and output to console is nearly all of it. Yeah, I could say "input string in SI, length in CX output char is in AL" and then I think the only code that would be necessary is REPZ LODSB (2 bytes) and we'd be done. Of course this approach wouldn't be how you do it if you were coding for efficiency, not size. Your point is very well taken though, I'll post it also as a function that does the meat of the work. – 640KB Mar 19 '19 at 15:04

# Brainf***, 7 bytes

,[>,]<.

• ideone.com/XoJLD0 It still doesn't work ;( – jean Mar 17 '19 at 20:35
• @jean Try it here. (Honestly, I didn't even know ideone did BF). – SuperJedi224 Mar 17 '19 at 20:38
• Yes, in your link everything works fine. But online judge for this problem uses ideone where it doesn't work ;( – jean Mar 17 '19 at 20:45
• @jean ideone seems to use -1 as the EOF. +[>,+]<-. should work – Jo King Mar 17 '19 at 21:21
• @Jo King Sorry, but the input looks like: 29845812674[enter][EOF] how can I print the last digit? – jean Mar 17 '19 at 21:59

# MATL, 2 bytes

0)


MATL uses 1-based modular indexing so this solution grabs the element in the 0-th position of the input which is the same as the last since the 0 wraps around to the end.

Try it out at MATL Online

Explanation

    % Implicitly grab the input
0   % Push the literal 0 to the stack
)   % Use this zero to grab the character at the end of the string
% Implicitly display the result

• I'd have gone for J)... – Sanchises Mar 18 '19 at 6:48

# Javascript, 14 bytes

a=>a.slice(-1)

• I tried my best to find a shorter way but to my surprise I don't think this can be improved, unless there is a funky way to reverse a string in under 8 bytes, before calling [0]. Or maybe if there is a short way to get array length. Different approach: a=>[...a].pop() (15bytes) – Matsyir Sep 26 '19 at 14:15

# PHP, 13 bytes

<?=$argn[-1];  Try it online! Run with php -nF input is STDIN. Example: $ echo 29845812674|php -nF lost.php


# Python 3, 14 bytes

lambda x:x[-1]


Try it online!

• I feel dumb, how are you calling this without declaring it to a variable? – Nathan Dimmer Mar 22 '19 at 14:12
• I looked at your TIO, but it doesn’t make much sense... What are you doing in your header? – Nathan Dimmer Mar 22 '19 at 14:14
• @Bobawob For your first question, anonymous lambdas are allowed for answers (I call it using by assigning the lambda to the variable e in the header). For your second question, the header is e=\ , which basically means e=lambda x:x[-1] – MilkyWay90 Mar 22 '19 at 20:14
• Note that in my above comment, there is not supposed to be a trailing space in e=\  but Markdown escapes the code character so I have to add a trailing space – MilkyWay90 Mar 22 '19 at 20:16
• That’s really cool! Thank you! – Nathan Dimmer Mar 27 '19 at 14:03

# Bash + coreutils, 8 bytes

tail -c1


Input is from stdin, output is to stdout.

# TI-BASIC (TI-84), 10 bytes

sub(Ans,length(Ans),1


Gets the last character in the input string.
Input is in Ans.
Output is in Ans and is automatically printed out.

last


Try it online!

• Why bother with pure? Isn't last enough? – dfeuer Mar 19 '19 at 7:22
• I assumed that the output must be a string too, but you're right, OP is only talking about "bytes". – flawr Mar 19 '19 at 8:41

# Seed, 11 bytes

5 370394306


Try it online!

The resulting Befunge-98 program ~2j@, was stolen borrowed from Jo King here, so credit to them for that.

# Java 8

Input from STDIN, 71 bytes

v->{int i=0;for(;System.in.available()>0;i=System.in.read());return i;}


Try it online!

Function Argument, 25 bytes

s->s.charAt(s.length()-1)

• You could add the code for program argument to have all three options. :) Too bad OP specifically asks for Strings, otherwise s->s[s.length-1] would have been enough with a char[] parameter-type. – Kevin Cruijssen Mar 18 '19 at 10:47
• – Olivier Grégoire Mar 19 '19 at 13:03

# ><>, 2 bytes

Using command line args

o;


Try it online!

# ><>, 11 bytes

Using stdin

\~o;
/?(0:i


Try it online!

# Cubix, 6 bytes

pA/@po


Try it online!

  p
A / @ p
o


Watch it run

• A Takes all the input
• / Redirect around the cube
• pp bring bottom of the stack to the top twice
• o/@ output as character, redirect and halt

# Befunge-93, 12 15 bytes

:1+_p1-,@>~#


Try it online!

Thanks to @Jo King for golfing off 3 bytes.

Alternate 15 byte version that is less messy:

~:1+#v!_
@,$<  Taking strings as input in Befunge isn't the easiest. If there were a single command to take in multiple characters, it would be as simple as reading the string, popping/printing the top character, and exiting. • Actually, $$ instead of p1 should work without the warning for the same amount of bytes – Jo King Mar 18 '19 at 0:46 # Turing Machine But Way Worse, 391 bytes 1 0 1 1 0 0 0 0 0 0 1 1 0 0 1 1 1 1 0 0 0 0 1 0 1 2 0 0 1 2 1 1 0 0 0 0 2 0 1 3 0 0 1 3 1 1 0 0 0 0 3 0 1 4 0 0 1 4 1 1 0 0 0 0 4 0 1 5 0 0 1 5 1 1 0 0 0 0 5 0 1 6 0 0 1 6 1 1 0 0 0 0 6 0 1 7 0 0 1 7 1 1 0 0 0 0 7 0 1 8 0 0 1 8 1 1 0 0 0 0 8 0 0 9 0 0 0 9 0 0 a 0 0 0 a 0 0 b 0 0 0 b 0 0 c 0 0 0 c 0 0 d 0 0 0 d 0 0 e 0 0 0 e 0 0 f 0 0 0 f 0 0 h 0 0 0 h 0 0 g 0 0 0 g 0 0 0 1 1 1 g 1 0 0 1 1  Try it online! EXPLANATION Detect eight zero bits (which will occur at the end of the input, since TMBWW uses an infinite tape of bits.) 1 1 1 1 0 0 0 0 1 0 1 2 0 0 1 2 1 1 0 0 0 0 2 0 1 3 0 0 1 3 1 1 0 0 0 0 3 0 1 4 0 0 1 4 1 1 0 0 0 0 4 0 1 5 0 0 1 5 1 1 0 0 0 0 5 0 1 6 0 0 1 6 1 1 0 0 0 0 6 0 1 7 0 0 1 7 1 1 0 0 0 0 7 0 1 8 0 0 1 8 1 1 0 0 0 0 8 0 0 9 0 0 ------------- When eight 0 bits are detected, move back to the final byte of the input and print it out while halting the program. 0 9 0 0 a 0 0 0 a 0 0 b 0 0 0 b 0 0 c 0 0 0 c 0 0 d 0 0 0 d 0 0 e 0 0 0 e 0 0 f 0 0 0 f 0 0 h 0 0 0 h 0 0 g 0 0 0 g 0 0 0 1 1 1 g 1 0 0 1 1  # Jelly, 1 byte Ṫ  Try it online! Not the most difficult challenge in Jelly... Note this accepts the input as a string; if the input could be interpreted otherwise (e.g. a number, a list), then it the argument will need to be quoted (e.g. "123456" or "[123,197]"). Alternatively this can be seen as a link that takes a byte array and returns the last member of that array, in accordance with PPCG standard rules. Thanks to @MilkyWay90 and @ბიმო for pointing this out. • -1 Fails for any number (tio.run/##y0rNyan8///hzlX///83MTY1NjE1MQYA) – MilkyWay90 Mar 17 '19 at 22:01 • @MilkyWay90: Doesn't need to be a full program, probably this will work as a Jelly function taking a string. But then again I don't know Jelly, so I might be wrong. – ბიმო Mar 17 '19 at 22:16 • Okay, I'll try seeing whether or not it will work as a link – MilkyWay90 Mar 17 '19 at 22:16 • @ბიმო Seems to work (OP can you edit the answer so I can undo my downvote?) – MilkyWay90 Mar 17 '19 at 22:17 • You can just define the T as a link and make it input a string, removing the need for "" in your input. – MilkyWay90 Mar 18 '19 at 23:00 # Excel, 10 bytes Pretty much equivalent to @remoel's VBA answer: =RIGHT(A1)  # Cascade, 9 bytes ?a|, ;.]^  Pretty happy with this, as it is only 3 bytes longer than my cat program # Expanded  ? ^;. | |a ] | a ,|  This essentially just loops through pushing input characters into the a stack until EOF is reached. Then it outputs the item at the top of the a stack using .a. Try it online! # PowerShell, 11 bytes "$args"[-1]


Try it online!

# SmileBASIC, 16 bytes

INPUT S$?POP(S$)


# 05AB1E, 1 byte

¤


Try it online!

θ or $$$$ would also work.

# Twig, 37 bytes

This just uses a simple "extract 1 char from the end and print it" aproach.

{%macro a(a)%}{{a[-1:1]}}{%endmacro%}


It was really easy to do, and test, but was fun!

To use it, you have to put it on a .twig file and import it:

{% import 'a.twig' as a %}
{{ a.a("string") }} {# should display "g" #}


You can test it on https://twigfiddle.com/aa19wd (testcases included)

# Bash, 13 bytes

echo \${1: -1}


string is passed as argument.

Try it online !

# Emotion, 5 bytes

😶👉😃😨👿


Explanation

😶 Push a copy of the first stack value.
👉 Push the length of the first stack value interpreted as a string.
😃 Push literal 1
😨 Push the difference of the second and first stack values.
👿 Push the character of the second stack value at the index of the top stack value.


Try it online!

• in your readme, 1. i think "to emoji" should be "from emoji" 2. if it should be "from emoji" then emotinomicon and emojicode are a thing already – ASCII-only Mar 18 '19 at 10:24
• Actually what you're looking at is output from a compiler, which can also be accessed via the online interface. – Quantum64 Mar 18 '19 at 14:45
• Isn't each emoji more than one byte? I'd think they'd be two bytes at least. – Kyle Delaney Mar 18 '19 at 20:18
• Emotion uses a custom code page. See quantum64.github.io/EmotionBuilds/1.1.0//… – JPeroutek Mar 18 '19 at 20:53

# VBA (Excel), 14 12 bytes

using Immediate Window and Cell A1 as input

Thanks @tsh

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

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

# Python 3, 1118 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!

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

# IBM/Lotus Notes Formula, 11 bytes

@Right(i;1)


Computed field formula taking its input from editable field i

# 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

• 0 * * r 1/1 * * l 2/1 _ _ l halt/2 * _ r 0? – ASCII-only Mar 18 '19 at 0:22
• oi pls reply :|| – ASCII-only Mar 18 '19 at 0:35
• wouldn't work in what way? I've tested it online – ASCII-only Mar 18 '19 at 1:46
• @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. – SuperJedi224 Mar 18 '19 at 2:42
• Well, this is a simple challenge, don't think it needs more than one answer in any language :P – ASCII-only Mar 18 '19 at 3:22

# 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]);}}

• The challenge mentions "output", so the last solution is probably the right one – Ven Mar 18 '19 at 16:48
• Dang, you outdid my console answer by a lot. How does the ^1 work? – Stackstuck Mar 18 '19 at 20:21
• 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]` – Arcanox Mar 18 '19 at 20:24
• Interesting! I always try to stay up to date with new C# features. Do you have any link / reference on that? – mortb Mar 19 '19 at 9:00
• – mortb Mar 19 '19 at 9:06