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

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 Mar 24 '19 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\$
1
  • \$\begingroup\$ invalid, you need to change to printf \$\endgroup\$ – ASCII-only Mar 31 '19 at 10:55
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\$

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\$ – Unrelated String Sep 27 '19 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 Sep 27 '19 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 Feb 5 '20 at 16:56
  • \$\begingroup\$ That's exactly what I meant - thank you, accidentally copy/pasted from the wrong solution file :) \$\endgroup\$ – Mathgeek Feb 5 '20 at 18:05
1
\$\begingroup\$

Burlesque, 2 bytes

[~

Try it online!

If it needs to be pretty formatted [~Q

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

Ruby -pl, 9 bytes

Similar to the Perl solution, but chop doesn't return the last character in Ruby. (It instead returns the rest of the string without the last character.)

$_=$_[-1]

Try it online!

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

Integral, 2 Bytes

Vn

Try it!

Explanation

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

Flurry, 14 bytes

([]{(){}{}}{})

Run example

$ echo -n "?" | ./flurry -bnb -c "([]{(){}{}}{})"
?
$ echo -n "Hello world" | ./flurry -bnb -c "([]{(){}{}}{})"
d

-bnb flag means "print stack as chars, print nothing for return value, and take stdin as chars".

If the challenge were asking for "the last value from an integer array", I would use return value output and write 2 byte solution {} (pop the stack and return it). But since character I/O is required and the only way to output a char is via the stack, I had to manually empty the stack.

How it works

(
 []     Stack height as Church numeral; Given 2 arguments `f` `x`,
          a Church numeral `n` acts as "apply `f` `n` times to `x`"
 {      Define f, with its argument pushed to the stack:
  ()      K; Given two arguments, ignore the second
  {}      Pop; the argument
  {}      Pop; pop an extra item from the stack (ignored by K)
 }      End definition of f: Return its argument unchanged,
          popping and discarding an item from the stack
 {}     Pop an item (last char)
        At this point, the stack is empty and the value is the last char
)     Push the result of the above
\$\endgroup\$
1
\$\begingroup\$

Pip, 4 bytes

a@-1

@RVa also works, and is the same size.

Try it online!

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

Notepad, 9 5 keystrokes

Assuming the cursor is at the end of the file.

[LeftArrow][Ctrl-Shift-LeftArrow][Delete]

I know it is not a language, but I'm posting just for fun.

\$\endgroup\$
3
  • \$\begingroup\$ The input is one line, according to the challenge specs. -6 keystrokes: [LeftArrow][Ctrl-Backspace] \$\endgroup\$ – user96495 Sep 7 '20 at 10:11
  • \$\begingroup\$ @hi. Doesn't work on my notepad. Ctrl-Backspace just adds an unprintable character, which I suspect is \x7f. \$\endgroup\$ – null Sep 7 '20 at 13:10
  • \$\begingroup\$ @hi. I don't see anything in the specs about it only being one line of input \$\endgroup\$ – Jo King Sep 7 '20 at 14:00
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 Sep 30 '20 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\$

vim, 6 bytes

VGJ$d^

Annotated

VGJ     # join all lines
$       # move cursor to (before) last character
d^      # delete to beginning

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ v}hd is 2 bytes smaller. \$\endgroup\$ – Razetime Apr 14 at 4:36
  • \$\begingroup\$ @Razetime But it fails if the last line of input only has one character: there's nowhere for the h to go, so the last character gets included in the delete range. \$\endgroup\$ – Ray Apr 14 at 13:23
  • \$\begingroup\$ ah, interesting. \$\endgroup\$ – Razetime Apr 14 at 13:29
0
\$\begingroup\$

Python 3, 16 bytes

This is a pretty basic answer, but I think that it is the lowest Python 3 can go...

x=lambda a:a[-1]

TIO

\$\endgroup\$
1
  • 1
    \$\begingroup\$ you don't need the x= here \$\endgroup\$ – ASCII-only Mar 31 '19 at 10:57
0
\$\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\$
0
\$\begingroup\$

T-SQL, 23 bytes

SELECT RIGHT(v,1)FROM i

Didn't see a SQL solution yet.

Input is via a pre-existing table \$i\$ with varchar field \$v\$, per our IO rules.

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

Zsh, 11 bytes

try it online!!

<<<${1: -1}

clone of the bash answer, 2 bytes shorter

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

Perl 5, 7 bytes

$_=chop

This must be executed using the -pe flags.

Example

$ echo -n "abcd" | perl -pe '$_=chop'
d

Explanation

The -p flag wraps code inside a block that appears as: while (<>) { ... ; print } whereas the '...' would include the code provided. The entire script would expand to:

while (<>) { $_ = chop ; print }

What I did here was set the context variable $_ to the return value of chop, which returns the last character of a string. Shortly after, print with no statements on its own will display the previously assigned context variable.

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

Pushy, 1 byte

'

Try it online!

Print the top of stack as a character!

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

Scala, 6 bytes

_.last

Try it online!

Not much to say here - it's just a function that takes a string (or really any iterable) and outputs its last element.

\$\endgroup\$

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.