12
\$\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"

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.

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

## 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 ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";
var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk";
var OVERRIDE_USER = 8478; // This should be the user ID of the challenge author.

/* App */

var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page;

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

function commentUrl(index, answers) {
  return "https://api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER;
}

function getAnswers() {
  jQuery.ajax({
    url: answersUrl(answer_page++),
    method: "get",
    dataType: "jsonp",
    crossDomain: true,
    success: function (data) {
      answers.push.apply(answers, data.items);
      answers_hash = [];
      answer_ids = [];
      data.items.forEach(function(a) {
        a.comments = [];
        var id = +a.share_link.match(/\d+/);
        answer_ids.push(id);
        answers_hash[id] = a;
      });
      if (!data.has_more) more_answers = false;
      comment_page = 1;
      getComments();
    }
  });
}

function getComments() {
  jQuery.ajax({
    url: commentUrl(comment_page++, answer_ids),
    method: "get",
    dataType: "jsonp",
    crossDomain: true,
    success: function (data) {
      data.items.forEach(function(c) {
        if (c.owner.user_id === OVERRIDE_USER)
          answers_hash[c.post_id].comments.push(c);
      });
      if (data.has_more) getComments();
      else if (more_answers) getAnswers();
      else process();
    }
  });  
}

getAnswers();

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

var OVERRIDE_REG = /^Override\s*header:\s*/i;

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

function process() {
  var valid = [];
  
  answers.forEach(function(a) {
    var body = a.body;
    a.comments.forEach(function(c) {
      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],
        link: a.share_link,
      });
    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;
    
    var answer = jQuery("#answer-template").html();
    answer = answer.replace("{{PLACE}}", lastPlace + ".")
                   .replace("{{NAME}}", a.user)
                   .replace("{{LANGUAGE}}", a.language)
                   .replace("{{SIZE}}", a.size)
                   .replace("{{LINK}}", a.link);
    answer = jQuery(answer);
    jQuery("#answers").append(answer);

    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)
                       .replace("{{LINK}}", lang.link);
    language = jQuery(language);
    jQuery("#languages").append(language);
  }

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

#answer-list {
  padding: 10px;
  width: 290px;
  float: left;
}

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

table thead {
  font-weight: bold;
}

table td {
  padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.sstatic.net/Sites/codegolf/all.css?v=ffb5d0584c5f">
<div id="language-list">
  <h2>Shortest Solution by Language</h2>
  <table class="language-list">
    <thead>
      <tr><td>Language</td><td>User</td><td>Score</td></tr>
    </thead>
    <tbody id="languages">

    </tbody>
  </table>
</div>
<div id="answer-list">
  <h2>Leaderboard</h2>
  <table class="answer-list">
    <thead>
      <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr>
    </thead>
    <tbody id="answers">

    </tbody>
  </table>
</div>
<table style="display: none">
  <tbody id="answer-template">
    <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr>
  </tbody>
</table>
<table style="display: none">
  <tbody id="language-template">
    <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr>
  </tbody>
</table>

\$\endgroup\$
  • 4
    \$\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
  • 11
    \$\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
  • 9
    \$\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

76 Answers 76

2
\$\begingroup\$

Retina, 10 9 bytes

(.|¶)*
$1

Try it online!

|improve this answer|||||
\$\endgroup\$
2
\$\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
|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

F#, 14 8 bytes

Seq.last

-6 bytes thanks to aloisdg.

Strings are treated as sequences in F#, so you can use the Seq.last function to get the last character in it.

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

C (gcc), 31 bytes

f(int*s){gets(s),printf("%s");}

Try it online!

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ O_o how does this even work \$\endgroup\$ – ASCII-only Mar 31 '19 at 10:52
  • \$\begingroup\$ I have no idea, it just works. \$\endgroup\$ – Natural Number Guy Mar 31 '19 at 14:07
  • \$\begingroup\$ Impressive list of complaints from the compiler! \$\endgroup\$ – roblogic Sep 27 '19 at 5:00
2
\$\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.)

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

Scratch 3.0, 7 blocks/68 bytes

enter image description here

or, as scratchblocks syntax

when gf clicked
ask()and wait
say(letter(length of(answer))of(answer

Try it on scratch

Did I mention this was done 100% on mobile? Because it was really hard making this, but I think it was worth it.

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

naz, 38 bytes

2a2x1v1x1f1r3x1v2e2x2v1f0x1x2f2v1o0x1f

Works for any input string terminated with the control character STX (U+0002).

Explanation (with 0x commands removed)

2a2x1v             # Set variable 1 equal to 2
1x1f1r3x1v2e2x2v1f # Function 1
                   # Read a byte of input
                   # Jump to function 2 if it equals variable 1
                   # Otherwise, store it in variable 2,
                   # then jump back to the start of the function
1x2f2v1o           # Function 2
                   # Load variable 2 into the register and output it
1f                 # Call function 1
|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Wolfram Language (Mathematica), 16 bytes

#~StringTake~-1&

Try it online!

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

C# (.NET Core), 81 bytes, command-line input

class M{static void Main(string[] a){System.Console.Write(a[0][a[0].Length-1]);}}

Try it online!

If you run this from an actual command line, you will need to wrap your string in quotes if it contains spaces.

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

C# (.NET Core), 115 bytes, console input

using C=System.Console;class M{static void Main(string[] a){int c=0,d;while((d=C.Read())>-1)c=d;C.Write((char)c);}}

Try it online!

This feels kinda janky, but it does work. Interestingly, I can't save any bytes with a for loop as the code stands.

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Perl 6, 11 bytes

*.comb[*-1]

Try it online!

Anonymous Whatever lambda that takes a string, splits it into characters, and returns the last one.

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Batch, 24 bytes

@set s=%1
@echo %s:~-1%

Takes input as a command-line argument. Note that arguments can't include special characters or spaces, but you can fake arguments with spaces in this case by preceding it with a ", which results in a single argument that begins with ", however there is no easy solution for arguments that include special characters. Batch can't easily read "all of stdin". To read up to but not including the first newline itself would however be a byte shorter:

@set/ps=
@echo %s:~-1%

Edit: A version that handles arbitrary characters in a (quoted) argument for 92 bytes:

@set s="%~1"
@set "s=%s:~-2,1%
@if "%s%"=="" (echo ^")else for %%s in ("%s%")do @echo %%~s

Explanation: The first line makes a copy of the argument in a variable and ensures that it is quoted. The second argument then takes the second last character (because the quote is now the last character). However, if that was also a quote then this results in an empty variable, so we need to special-case that and output a (quoted) quote. Otherwise, we still need to quote the character in case it is a special character as echoing "%s%" will echo the quotes and echoing %s% will actually interpret special characters, so the variable needs to be quoted to allow it to be parsed but then immediately unquoted so it can be printed. This is achieved using the for command. 86 bytes to read up to but not including the first newline from stdin while supporting special characters:

@set/ps=
@set "s=%s:~-1%
@if "%s%"=="" (echo ^")else for %%s in ("%s%")do @echo %%~s
|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Scratch 3.0, scratchblocks3 syntax

As a function, 61 bytes

define l
ask[]and wait
say(letter(length of(answer))of(answer

As a full program, 68 bytes

when gf clicked
ask[]and wait
say(letter(length of(answer))of(answer

Try both online

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Befunge-98, 5 bytes

~2j@,

Try it online!

Explanation:

~           Take input
 2j         Skip next two instructions
~           Repeat until EOF, where it reflects
   @,       Print the last character and exit
|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

C# (Visual C# Interactive Compiler), 11 bytes

x=>x.Last()

Try it online!

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ you can use a function :P \$\endgroup\$ – ASCII-only Mar 18 '19 at 0:47
  • \$\begingroup\$ OK - Ill update my answer :) \$\endgroup\$ – dana Mar 18 '19 at 1:21
  • 1
    \$\begingroup\$ uhm why is testcase in STDIN not footer \$\endgroup\$ – ASCII-only Mar 18 '19 at 1:38
  • \$\begingroup\$ Wow - It's been a while. Thanks for pointing that out ;) \$\endgroup\$ – dana Mar 18 '19 at 1:39
  • 1
    \$\begingroup\$ That's not how I understand it. I understand that it should print the last non-empty character. We need to ask OP. \$\endgroup\$ – Ven Mar 18 '19 at 17:33
1
\$\begingroup\$

Japt, 1 byte

Ì

Try it online!

-1 byte thanks to Quintec!

|improve this answer|||||
\$\endgroup\$
  • 1
    \$\begingroup\$ 1 byte \$\endgroup\$ – Quintec Mar 18 '19 at 2:00
1
\$\begingroup\$

APL+WIN, 4 bytes

¯1↑⎕

Prompt for input string and select last byte.

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Whitespace, 54 bytes

[N
S S N
_Create_Label_LOOP][S S S N
_Push_0][S N
S _Duplicate_0][T   N
T   S _Read_STDIN_as_character][T   T   T   _Retrieve_input][S N
S _Duplicate][S S S T   S T S N
_Push_10][T S S T   _Subtract][N
T   S S N
_If_0_Jump_to_Label_PRINT][N
S N
N
_Jump_to_Label_LOOP][N
S S S N
_Create_Label_PRINT][S N
N
_Discard_top][T N
S S _Print_as_character]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Since Whitespace can only take input as integer or character, we must add a trailing character to indicate we're done with the input-string after reading it character by character, for which I've used a newline.

Try it online (with raw spaces, tabs and new-lines only).

Example run: input = A2#

Command    Explanation                   Stack                Heap    STDIN STDOUT STDERR

NSSN       Create Label_LOOP             []
 SSSN      Push 0                        [0]
 SNS       Duplicate top (0)             [0,0]
 TNTS      Read STDIN as character       [0]                  {0:65}  A
 TTT       Retrieve at heap address (0)  [65]                 {0:65}
 SNS       Duplicate top (65)            [65,65]              {0:65}
 SSSTSTSN  Push 10                       [65,65,10]           {0:65}
 TSST      Subtract top two (65-10)      [65,55]              {0:65}
 NTSSN     If 0: Jump to Label_PRINT     [65]                 {0:65}
 NSNN      Jump to Label_LOOP            [65]                 {0:65}

 SSSN      Push 0                        [65,0]
 SNS       Duplicate top (0)             [65,0,0]
 TNTS      Read STDIN as character       [65,0]               {0:50}  2
 TTT       Retrieve at heap address (0)  [65,50]              {0:50}
 SNS       Duplicate top (50)            [65,50,50]           {0:50}
 SSSTSTSN  Push 10                       [65,50,50,10]        {0:50}
 TSST      Subtract top two (50-10)      [65,50,40]           {0:50}
 NTSSN     If 0: Jump to Label_PRINT     [65,50]              {0:50}
 NSNN      Jump to Label_LOOP            [65,50]              {0:50}

 SSSN      Push 0                        [65,50,0]
 SNS       Duplicate top (0)             [65,50,0,0]
 TNTS      Read STDIN as character       [65,50,0]            {0:35}  #
 TTT       Retrieve at heap address (0)  [65,50,35]           {0:35}
 SNS       Duplicate top (35)            [65,50,35,35]        {0:35}
 SSSTSTSN  Push 10                       [65,50,35,35,10]     {0:35}
 TSST      Subtract top two (35-10)      [65,50,35,25]        {0:35}
 NTSSN     If 0: Jump to Label_PRINT     [65,50,35]           {0:35}
 NSNN      Jump to Label_LOOP            [65,50,35]           {0:35}

 SSSN      Push 0                        [65,50,35,0]
 SNS       Duplicate top (0)             [65,50,35,0,0]
 TNTS      Read STDIN as character       [65,50,35,0]         {0:10}  \n
 TTT       Retrieve at heap address (0)  [65,50,35,10]        {0:10}
 SNS       Duplicate top (10)            [65,50,35,10,10]     {0:10}
 SSSTSTSN  Push 10                       [65,50,35,10,10,10]  {0:10}
 TSST      Subtract top two (10-10)      [65,50,35,10,0]      {0:10}
 NTSSN     If 0: Jump to Label_PRINT     [65,50,35,10]        {0:10}

NSSSN      Create Label_PRINT            [65,50,35,10]        {0:10}
 SNN       Discard top                   [65,50,35]           {0:10}
 TNSS      Print as character to STDOUT  [65,50]              {0:10}        #
                                                              {0:10}               error

Stops with the error: Exit not defined.

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Japt -h, 1 byte

U

Run it online

|improve this answer|||||
\$\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.

|improve this answer|||||
\$\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.

|improve this answer|||||
\$\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!

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Forth (gforth), 17 bytes

: f 1- + 1 type ;

Try it online!

Explanation

Adds string-length - 1 to the string address and then prints a string of length 1 starting at that address.

Code Explanation

: f        \ start a new word definition
  1-       \ subtract 1 from string length
  +        \ add result to string address
  1 type   \ print string of length 1 starting at the new address
;          \ end word definition
|improve this answer|||||
\$\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!

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Japt -h, 1 byte

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

s

Try it

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

LiveScript, 8 bytes

(.[*-1])

Explanation:

(.[*-1])
(.[*-1]) # "BIOP": operator section à la Haskell
 .[   ]  # Index into the implicit argument
   *-1   # In [], "*" refers to the length
|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Haskell, 4 bytes

last

Functions are allowed, right?

Also with IO (18 bytes):

main=interact$last
|improve this answer|||||
\$\endgroup\$
  • \$\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

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

|improve this answer|||||
\$\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!

|improve this answer|||||
\$\endgroup\$
  • \$\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

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.