21
\$\begingroup\$

Challenge

You have one string of input bytes, output only 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\$
7
  • 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\$ Commented Mar 17, 2019 at 19:11
  • 14
    \$\begingroup\$ I vote against closing; it may be trivial, but that doesn't make it offtopic \$\endgroup\$
    – MilkyWay90
    Commented Mar 17, 2019 at 22:30
  • 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
    Commented Mar 18, 2019 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\$
    – kepe
    Commented Mar 18, 2019 at 18:28
  • 2
    \$\begingroup\$ @FireCubez Yes, ASCII only \$\endgroup\$
    – jean
    Commented Mar 18, 2019 at 18:42

111 Answers 111

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
1
\$\begingroup\$

Japt -h, 1 byte

Can handle input as a string, integer or character/digit array.

s

Try it

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

Haskell, 4 bytes

last

Functions are allowed, right?

Also with IO (18 bytes):

main=interact$last
\$\endgroup\$
1
  • \$\begingroup\$ The same answer was already posted, though we do allow duplicate answers as far as I know. \$\endgroup\$
    – Laikoni
    Commented Mar 24, 2019 at 21:22
1
\$\begingroup\$

C, 36 35 34 bytes

x(char*v){printf(v+strlen(v)-1);}

Really simple stuff here. Nothing to ungolf either.

Saved one byte thanks to ceilingcat
Fixed the answer and saved another byte thanks to ASCII-only

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

Pepe, 13 bytes

REEeREEEeReEe

Try it online! Disable "Separated by" check box below the input text box.

Explanation:

REEe  # Input as string (stack R)
REEEe # Goto last char (stack R)
ReEe  # Output char (stack R)
\$\endgroup\$
1
\$\begingroup\$

Keg, 1 byte

This is the exact thing that Keg was built for.

,

Explanation

# Push implicit string input
,# Output the last pushed character
# There is no implicit output since something was outputted

TIO

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

Triangular, 10 bytes

(#~p../)?<

Try it online!

Pretty straight-forward; reads characters until it gets a null read, then prints the top of the stack.

Ungolfed:

   ( 
  # ~ 
 p . . 
/ ) ? <
--------------------------------------------
(            Set a point to jump back to
 ~.<         Read a character, change directions ("." is a no-op)
    ?)/      If ToS <= 0, skip next instruction and change directions; otherwise, jump back to "("
       p#    Pop the top value from the stack (the null input), then pop again and print that value
\$\endgroup\$
1
\$\begingroup\$

Commodore 8-bit BASIC (CBM/PET, VIC-20, C64/TheC64Mini, C128, C16/+4) - byte count later

 0inputa$:iflen(a$)thenprintright$(a$,1)

Simplified (without the sanity check):

 0inputa$:?right$(a$,1)

There is a small limitation in that Commodore 8-bit BASIC the maximum length of a string is 255 characters, so any entry above that will cause an error.

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

Clojure, 32 bytes 24 bytes

(print(last(read-line)))
(print             ;; prints output
  (last            ;; get last character of input
    (read-line)))  ;; read input

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ You could shave off 3 bytes by removing all whitespace. It also seems like you don't need the str, but my Clojure is extremely rusty so I can't say for sure. \$\endgroup\$ Commented Sep 27, 2019 at 4:58
  • 1
    \$\begingroup\$ @UnrelatedString I don't know that I can use without whitespace at all. Thanks for that. I think str is not needed at all, but it will return char (\?) instead of string ("?") (if that's okay) \$\endgroup\$
    – Ampersanda
    Commented Sep 27, 2019 at 5:30
1
\$\begingroup\$

MarioLANG, 18 bytes

>,
"+
)[
!<(-
#==.

Try it online!

code:

>    go right
,    read input
+    increment it (because EOF = -1)
[    ignore the next command, if current cell = 0
<    go to the left
!    stop moving (Mario is now standing on the elevator (#), 
     which rides up to the elevator end (")
)    go to the next memory cell
     Mario is now at the starting position (>) and runs another round 
     until the end of input

else (if he ignored the "<" instruction)
(    go one memory cell back (to the last inputted byte)
-    decrement it, so it becomes the original value again
.    print it
\$\endgroup\$
1
\$\begingroup\$

Brain-Flak-cr, (6 bytes code + 3 bytes command line) = 9 bytes

({}<>)

Try it online!

Code:

The command line argument "-cr" means "use ASCII input and output" and "reverse the stack", so when you pop a value, the last byte of the input is popped, and when you push a value, it is pushed to the end.

 {}     pop a character
(    )  and push it
   <>   on the other stack
        implicitly output the current stack
\$\endgroup\$
1
\$\begingroup\$

GolfScript, 3 bytes

The GolfScript = yields a decimal. That's pretty weird ... However, (luckily enough,) GolfScript supports slicing over a string, which allows me to slice the last item of the string.

-1>

Try it online!

Explanation

-1  # The last item
  > # Choose everything in the string
    # after the last item, including the last item
    # this (obviously) yields the last item
\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog Unicode), 2 bytesSBCS

⊢/

Try it online!

The code taken literally means to "reduce by right argument function". It works like the following (basically like foldr1 (flip const) in Haskell):

  ⊢/ 'abcd'
→ 'a' ⊢ 'b' ⊢ 'c' ⊢ 'd'
→ 'a' ⊢ 'b' ⊢ 'd'
→ 'a' ⊢ 'd'
→ 'd'

This is an idiom for taking last element from a vector (or taking last element from each row of a multi-dimensional array).

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

GolfScript, 3 bytes

Though there was already a Golfscript 3 byte solution, I figured I'd throw in the more general-purpose one.

)\;

Where ( is the front-uncon function (Golfscript treats strings as char arrays), \ swaps the string and char positions, and ; deletes the string, leaving only the last char.

\$\endgroup\$
2
  • \$\begingroup\$ Sure is not ) what you intended to use? At least with the interpreter available on tio.run this outputs the first byte, not the last. \$\endgroup\$
    – manatwork
    Commented Feb 5, 2020 at 16:56
  • \$\begingroup\$ That's exactly what I meant - thank you, accidentally copy/pasted from the wrong solution file :) \$\endgroup\$
    – Mathgeek
    Commented Feb 5, 2020 at 18:05
1
\$\begingroup\$

Burlesque, 2 bytes

[~

Try it online!

If it needs to be pretty formatted [~Q

[~ # Last char
\$\endgroup\$
1
\$\begingroup\$

Integral, 2 Bytes

Vn

Try it!

Explanation

V  Reverse the input
 n Head
\$\endgroup\$
1
\$\begingroup\$

Pip, 4 bytes

a@-1

@RVa also works, and is the same size.

Try it online!

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

Marbelous, 13 bytes

@0
00
]]..
@0

Marbelous is a language based on marble machines

  • @n (n from 0 to Z) is a portal which teleport the marble to another portal with the same value
  • 00-FF initiate a marble with this value
  • ]] passing marble take value of next input byte if there is one, else the marble is diverted to the right
  • .. is a noop
  • marbles going out of the board to the bottom are implicitly outputed

interpretor

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

Labyrinth, 8 bytes

,);.@
"(

Try it online!

,)("  Loop until EOF (-1): push a char input, increment, then decrement
;.@   On EOF, discard top, print the last char, and halt
\$\endgroup\$
1
  • \$\begingroup\$ Same bytecount, but a little simpler. I don't think a T intersection can be made any smaller \$\endgroup\$
    – Jo King
    Commented Sep 30, 2020 at 7:42
1
\$\begingroup\$

Pyth, 1 byte

e

Try it online!

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

Regex, 2 bytes

.$

Matches character then end of string.

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

APOL, 7 bytes

g(i -1)

About as simple as possible, just gets the last character of the input string.

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

Fig, \$1\log_{256}(96)\approx\$ 0.823 bytes

]

Try it online!

1-byters?? Seriously?? Why have that when you can have 0.823 bytes?? Literally "grab last char of (implicit) input."

\$\endgroup\$
1
  • \$\begingroup\$ Hey that ain't fair :( \$\endgroup\$
    – DialFrost
    Commented Oct 10, 2022 at 23:03
1
\$\begingroup\$

nroff, 57 bytes

.de F
.ds X \\$1
.length L \\*X
.substring X \\nL
\\*X
..

Attempt This Online!

Usage

.F "41382239"
.F "e"
.F "what"
.F " a "
.F "b c"

How it works

.de F             \" define macro F
.ds X \\$1        \" store 1st argument to string variable X
                  \" I had to do so because arguments are not in variable, but
                  \" can be obtained via requests only
.length L \\*X    \" store length of X to register L
                  \" no numerical express to do so
.substring X \\nL \" 1-indexed. replace X with L-th to last substring
\\*X              \" content of X
..                \" end of definition
\$\endgroup\$
1
\$\begingroup\$

ARM Thumb machine code, 6 bytes

01 39 40 5c 70 47

Commented assembly:

    .syntax unified
    .arch armv7-a
    .thumb
    .globl lastchar
    .thumb_func
    // C-callable function
    // Input:
    //  - r0: String of ASCII (not utf-32)
    //  - r1: Length of string
    // Output:
    //  - r0: Char
    // int lastchar(const char *ptr, uint32_t length);
lastchar:
    // It's literally ptr[length - 1]
    // Subtract 1 from the length since ARM can't do [reg, reg, offset]
    subs    r1, #1
    // load ptr[length]
    ldrb    r0, [r0, r1]
    // return in r0
    bx      lr

I mean it is literally return ptr[length - 1], there isn't much to explain. The function is callable using the C convention, using byte string + length (not null-terminated UTF-32 like most of my solutions):

int lastchar(const char *ptr, uint32_t length);

ARM Thumb machine code, 10 bytes

This is the "equivalent Thumb code" I mentioned on my aarch64 answer. It is much less elegant.

01 c9 0b 68 00 2b fb d1 70 47

Commented assembly:

    .syntax unified
    .arch armv7-a
    .thumb
    .globl lastchar
    .thumb_func
    // C-callable function
    // Input:
    //  - r1: Null terminated UTF-32LE string
    // Output:
    //  - r0: Char
    // char32_t lastchar(int dummy, const char32_t *str);
lastchar:
.Lloop:
    // load 1 char, advance pointer 
    ldmia   r1!, {r0}
    // Load next char into temp
    ldr     r3, [r1]
    // compare and loop if r3 wasn't a null terminator
    cmp     r3, #0
    bne     .Lloop
    // return in r0
    bx      lr

This uses the same function signature, taking a null terminated UTF-32 string and a dummy parameter to put str in r1:

char32_t lastchar(int dummy /* r0 */, const char32_t *str /* r1 */); 

In an ideal world, the direct equivalent could be done:

lastchar:
.Lloop:
    ldrd    r0, r3, [r1], #4
    cbnz    r3, .Lloop
    bx      lr

But unfortunately:

  • ldrd will bus error if the pointer is not 8 byte aligned (even with unaligned access support), and advancing the pointer by 4 will end up unaligned.
  • ldmia always advanced by 4 * NumRegisters
  • cbnz can only branch forwards
\$\endgroup\$
1
\$\begingroup\$

AArch64 machine code, 12 bytes

20 8c c0 28 e3 ff ff 35 c0 03 5f d6

Commented assembly

    .globl lastchar
    // Input: x1: null terminated UTF-32 string
    // Output: w0: last char in string
    // char32_t lastchar(int dummy, const char32_t *str);
lastchar:
.Lloop:
    // load two chars, advance by one char
    // w0 = *x1++; w3 = *x1;
    ldp     w0, w3, [x1], #4
    // loop if w3 was not zero (null terminator)
    cbnz    w3, .Lloop
    // return
    // result is in w0
    ret

This can be called from C using a dummy parameter to put the null terminated UTF-32 string in x1.

char32_t lastchar(int dummy /* x0 */, const char32_t *ptr /* x1 */);

I don't usually do AArch64 because it isn't very compact at 4 bytes per instruction and almost always longer than Thumb, but this particular solution is really elegant even if it is larger than the equivalent Thumb code by 2 bytes (it's solely because of the return instruction being 2 bytes) (Thumb does better with ptr+length though)

\$\endgroup\$

Your Answer

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

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