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

93 Answers 93

13
\$\begingroup\$

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~>

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

x86-16 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

x86-16 machine code, IBM PC DOS, 12 11 10 bytes

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

B3 80   MOV  BL, 80H            ; BX to DOS PSP at 80H 
8A 07   MOV  AL, BYTE PTR[BX]   ; get command line tail length 
D7      XLAT                    ; AL = [BX+AL] 
B4 0E   MOV  AH, 0EH            ; PC BIOS write to screen function  
CD 10   INT  10H                ; display  
C3      RET                     ; exit to DOS

Output:

enter image description here

\$\endgroup\$
2
  • \$\begingroup\$ 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? \$\endgroup\$ – Cody Gray Mar 19 '19 at 4:31
  • \$\begingroup\$ @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. \$\endgroup\$ – 640KB Mar 19 '19 at 15:04
7
\$\begingroup\$

Brainf***, 7 bytes

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

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
\$\endgroup\$
1
  • \$\begingroup\$ I'd have gone for J)... \$\endgroup\$ – Sanchises Mar 18 '19 at 6:48
6
\$\begingroup\$

PHP, 13 bytes

<?=$argn[-1];

Try it online!

Run with php -nF input is STDIN. Example:

$ echo 29845812674|php -nF lost.php
\$\endgroup\$
6
\$\begingroup\$

INTERCAL, 270 bytes

DO,1<-#1PLEASECOMEFROM(2)DOWRITEIN,1DO.1<-,1SUB#1DO.5<-#1$!1~#256'DO.2<-.3DO(1)NEXTPLEASE.2<-'"!3~#1'$!3~#16'"$"!3~#4'$!3~#64'"'$'"!3~#2'$!3~#32'"$"!3~#8'$!3~#128'"'DO(1010)NEXTPLEASE,1SUB#1<-.3PLEASEREADOUT,1DOGIVEUP(1)DO(1002)NEXTDO(1009)NEXTDO.3<-.3~#255(2)DOFORGET#1

Try it online!

Writing this was... interesting. I was thinking I might want to use INTERCAL to INTERCALate, but I'm a bit less sure now.

Ungolfed and commented:

        DO ,1<-#1             PLEASE NOTE We want the input array to only have space for one element, so it will only take one at a time
        DO COME FROM (2)
        DO WRITE IN ,1        PLEASE NOTE If this is the first byte of the input, it'll write its value... but if not, it'll write the
                              previous value minus its value mod 256.
        DO .1<-,1SUB#1
        DO .5<-#1$!1~#256'    PLEASE NOTE .5 is 3 if the input is 256, 2 otherwise
        DO .2<-.3
        DO (1) NEXT

                              PLEASE NOTE If we're here, we've found the end of the input. Now, we need to print it back out... C-INTERCAL's
                              array I/O, in order to determine what it will actually print, subtracts the value it's going to print from the
                              previous one (still mod 256, and with the previous value defaulting to 0), and then reads the bits of the byte
                              backwards. So in order to go from the value we want to display to the value we need to feed into READ OUT, we
                              reverse the bits and then subtract from 256. The nightmarish expression on the following line reverses the
                              bits the best way I could think to: individually select each one out and then mingle them all back
                              together. It may be possible to emulate the method used in cesspool.c, by using mingle and unary AND as a
                              substitute for binary AND where we can't afford for select to rearrange it, but it might end up longer...

        DO .2 <- '"'.3~#1'$'.3~#16'"$"'.3~#4'$'.3~#64'"'$'"'.3~#2'$'.3~#32'"$"'.3~#8'$'.3~#128'"'

        DO (1010) NEXT        PLEASE NOTE .1 already has 256 in it, which is very convenient for when you need to subtract .2 from 256.

        DO ,1SUB#1 <- .3      PLEASE NOTE If we just read .3 out, we'd get a Roman numeral instead of the correct output.
        DO READ OUT ,1

        DO GIVE UP            PLEASE NOTE End of program.

    (1) DO (1002) NEXT        PLEASE NOTE that that line in syslib does 1001 next, which pops .5 entries off the next-stack and returns
                              control flow to the last one, such that if .5 is 2 flow will come back here, but if it's 3 then it'll go back
                              to the line that nexted to this one.

                              Here we add .1 and .2 into .3, then truncate it to a byte before looping back (while managing the next-stack
                              responsibly so the program doesn't disappear into the black lagoon for any input over 79 (?) bytes)

        DO (1009) NEXT
        DO .3<-.3~#255
    (2) DO FORGET #1
\$\endgroup\$
5
\$\begingroup\$

Python 3, 14 bytes

lambda x:x[-1]

Try it online!

\$\endgroup\$
6
  • \$\begingroup\$ I feel dumb, how are you calling this without declaring it to a variable? \$\endgroup\$ – Nathan Dimmer Mar 22 '19 at 14:12
  • \$\begingroup\$ I looked at your TIO, but it doesn’t make much sense... What are you doing in your header? \$\endgroup\$ – Nathan Dimmer Mar 22 '19 at 14:14
  • 2
    \$\begingroup\$ @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] \$\endgroup\$ – MilkyWay90 Mar 22 '19 at 20:14
  • \$\begingroup\$ 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 \$\endgroup\$ – MilkyWay90 Mar 22 '19 at 20:16
  • \$\begingroup\$ That’s really cool! Thank you! \$\endgroup\$ – Nathan Dimmer Mar 27 '19 at 14:03
5
\$\begingroup\$

Javascript, 14 bytes

a=>a.slice(-1)
\$\endgroup\$
1
  • \$\begingroup\$ 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) \$\endgroup\$ – Matsyir Sep 26 '19 at 14:15
4
\$\begingroup\$

Bash + coreutils, 8 bytes

tail -c1

Input is from stdin, output is to stdout.

\$\endgroup\$
4
\$\begingroup\$

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
\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

Haskell, 9 4 bytes

last

Try it online!

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

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.

\$\endgroup\$
4
\$\begingroup\$

Arn, 2 bytes

:}

Pretty simple, the suffix :} gets the last element of an array (implicit casting)

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Another alternative: _\ \$\endgroup\$ – user96495 Aug 24 '20 at 3:01
  • 1
    \$\begingroup\$ +1 for a happy symbol \$\endgroup\$ – user Aug 29 '20 at 15:04
3
\$\begingroup\$

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)
\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$ – Kevin Cruijssen Mar 18 '19 at 10:47
  • \$\begingroup\$ 61 bytes. \$\endgroup\$ – Olivier Grégoire Mar 19 '19 at 13:03
3
\$\begingroup\$

><>, 2 bytes

Using command line args

o;

Try it online!

><>, 11 bytes

Using stdin

\~o;
/?(0:i

Try it online!

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

05AB1E, 1 byte

¤

Try it online!

θ or ` would also work.

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

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
\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Actually, $$ instead of p1 should work without the warning for the same amount of bytes \$\endgroup\$ – Jo King Mar 18 '19 at 0:46
3
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ -1 Fails for any number (tio.run/##y0rNyan8///hzlX///83MTY1NjE1MQYA) \$\endgroup\$ – MilkyWay90 Mar 17 '19 at 22:01
  • \$\begingroup\$ @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. \$\endgroup\$ – ბიმო Mar 17 '19 at 22:16
  • \$\begingroup\$ Okay, I'll try seeing whether or not it will work as a link \$\endgroup\$ – MilkyWay90 Mar 17 '19 at 22:16
  • \$\begingroup\$ @ბიმო Seems to work (OP can you edit the answer so I can undo my downvote?) \$\endgroup\$ – MilkyWay90 Mar 17 '19 at 22:17
  • \$\begingroup\$ You can just define the T as a link and make it input a string, removing the need for "" in your input. \$\endgroup\$ – MilkyWay90 Mar 18 '19 at 23:00
3
\$\begingroup\$

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!

\$\endgroup\$
4
  • \$\begingroup\$ 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 \$\endgroup\$ – ASCII-only Mar 18 '19 at 10:24
  • \$\begingroup\$ Actually what you're looking at is output from a compiler, which can also be accessed via the online interface. \$\endgroup\$ – Quantum64 Mar 18 '19 at 14:45
  • \$\begingroup\$ Isn't each emoji more than one byte? I'd think they'd be two bytes at least. \$\endgroup\$ – Kyle Delaney Mar 18 '19 at 20:18
  • \$\begingroup\$ Emotion uses a custom code page. See quantum64.github.io/EmotionBuilds/1.1.0//… \$\endgroup\$ – JPeroutek Mar 18 '19 at 20:53
3
\$\begingroup\$

Excel, 10 bytes

Pretty much equivalent to @remoel's VBA answer:

=RIGHT(A1)
\$\endgroup\$
3
\$\begingroup\$

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!

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

Mornington Crescent, 389 Bytes

Even a task as simple as this presents an interesting optimisation challenge when riding the London Underground.

Take Northern Line to Bank
Take Circle Line to Bank
Take Central Line to Mile End
Take Central Line to Holborn
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 Mile End
Take Central Line to Bank
Take Circle Line to Bank
Take Northern Line to Mornington Crescent

Try it online!

Visiting Mile End station allows you to take a substring from the end of the input - but to chop just 1 character, you need to generate the integer 1 somehow. Rather than doing any arithmetic, the fastest method turns out to be to parse it from the station name "Heathrow Terminals 1, 2, 3".

To bypass that, an alternate strategy for this challenge would be to reverse the input, read the character code for the now first byte, and then turn that back into a char to output - but this approach takes 12 bytes longer. (Although there are fewer trips needed, so the tickets would be cheaper.)

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

Rust, 25 16 10 bytes

|s|s.pop()

Try it on the Rust Playground!

An anonymous function that takes in a mutable String and outputs the char at the end of the string. Minus a lot of bytes, thanks to madlaina

\$\endgroup\$
2
  • \$\begingroup\$ The .unwrap() call isn't necessary as per this meta post. Also, if you use the closure in a function that requires a specific signature, the compiler can do a better job at type deduction and you can leave the :String out: example \$\endgroup\$ – madlaina Aug 14 '20 at 15:25
  • \$\begingroup\$ @madlaina thanks, I was testing with let bindings, and I had to be explicit there. \$\endgroup\$ – Aiden4 Aug 14 '20 at 20:15
2
\$\begingroup\$

PowerShell, 11 bytes

"$args"[-1]

Try it online!

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

SmileBASIC, 16 bytes

INPUT S$?POP(S$)
\$\endgroup\$
2
\$\begingroup\$

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)

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

Bash, 13 bytes

echo ${1: -1}

string is passed as argument.

Try it online !

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

APL+WIN, 4 bytes

¯1↑⎕

Prompt for input string and select last byte.

\$\endgroup\$
1
  • \$\begingroup\$ 3 byte solution ⊃⌽⍞ \$\endgroup\$ – rak1507 Oct 2 '20 at 13:01

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.