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\$ 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
    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
    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
    Mar 18, 2019 at 18:28
  • 2
    \$\begingroup\$ @FireCubez Yes, ASCII only \$\endgroup\$
    – jean
    Mar 18, 2019 at 18:42

111 Answers 111

1 2 3
4
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, 2019 at 10:57
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\$
0
\$\begingroup\$

MathGolf, 2 bytes

┤Þ

Try it online.

Or alternatively:

Try it online.

Explanation:

┤   # Pop the left character of the (implicit) input-string loose to the stack
 Þ  # Only leave the top item of the stack, and discard everything else
    # (implicitly output the entire stack joined together)

b   # Push -1
 §  # Get the -1'th character of the (implicit) input-string
    # (0-based modulair similar as Python, so will index into the last character)
    # (implicitly output the entire stack joined together)
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 1 byte

θ

Try it online!

   # (implicit) push STDIN to stack
θ  # push last character of top of stack
   # (implicit) output top of stack to STDOUT
\$\endgroup\$
0
\$\begingroup\$

Rockstar, 30 bytes

listen to S
cut S
say S at S-1

Try it here (Code will need to be pasted in)

Or 51 bytes if we need to be able to handle newlines in the input:

listen to S
while S
listen to S

cut S
say S at S-1
\$\endgroup\$
0
\$\begingroup\$

CSASM v2.2.1.1, 46 bytes

func main:
in ""
push ^1
push ^0
substr
print
ret
end

Commented:

func main:
    ; Get the input
    in ""
    ; Get a substring of the input from (length - 1) to (length - 0)
    push ^1
    push ^0
    substr
    ; Print it
    print
    ret
end
\$\endgroup\$
0
\$\begingroup\$

Duocentehexaquinquagesimal, 3 bytes

t`F

Try it online!

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

J, 2 bytes

{:

{: is implicit verb for last element of any array (including strings)

Try it online!

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

Factor, 11 bytes

[ 1 tail* ]

Try it online!

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

M4, 36 bytes

define(f,`substr($1,decr(len($1)))')

Attempt This Online!

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

Brainfuck, 11 bytes

,>+[-,>+]<.

Explained:

,> [ ,> ]   get input
  + -  +    sentinels
         <. output last non-NUL byte (NUL doesn't count, doesn't it?)
\$\endgroup\$
0
\$\begingroup\$

MAWP, 2 bytes

|;

Try it!

Explanation:

|      take whole input onto stack as ASCII value
 ;     output top of stack
\$\endgroup\$
0
\$\begingroup\$

Go, 38 bytes

func(s []byte)byte{return s[len(s)-1]}

Attempt This Online!

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

Standard ML, 8 bytes

There are two common ways of string processing: char lists and strings. In both cases, the underlying char is an 8 bit value using "extended ASCII".

For char lists:

hd o rev

I'm not sure why List.last isn't top-level, but that works too.

For strings, convert to List and use explode:

hd o rev o explode
\$\endgroup\$
0
\$\begingroup\$

Vyxal, 1 byte

t

Try it Online!

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

JavaScript (Node.js), 10 bytes

Assumes every byte is a letter one of 0 through 9. Is this justified?

s=>s%10+""

Attempt This Online!

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

Thunno 2 t, 0 bytes

Attempt This Online!

That's right: no bytes. The t flag does all the work.

Or 1 byte flagless:

t

Attempt This Online!

Built-in for last item.

\$\endgroup\$
1 2 3
4

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.