Print last inputted byte

12

1

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>

jean

Posted 2019-03-17T18:19:13.480

Reputation: 371

4

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.

– ბიმო – 2019-03-17T19:11:34.210

11I vote against closing; it may be trivial, but that doesn't make it offtopic – MilkyWay90 – 2019-03-17T22:30:05.713

1@MillyWay I think most of the close votes were before the extensive edit by ბიმო – Sanchises – 2019-03-18T06:47:09.290

9

@ბიმო We have a consensus not to edit off-topic questions to make them on-topic which I think would have applied here.

– Laikoni – 2019-03-18T07:19:13.337

2What kind of string? Is it guaranteed to be ASCII only? Or should we handle UTF-8 (and how?) for example? – FireCubez – 2019-03-18T18:28:41.480

1@FireCubez Yes, ASCII only – jean – 2019-03-18T18:42:55.910

Answers

11

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

640KB

Posted 2019-03-17T18:19:13.480

Reputation: 7 149

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? – Cody Gray – 2019-03-19T04:31:56.470

@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. – 640KB – 2019-03-19T15:04:20.043

11

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

Conor O'Brien

Posted 2019-03-17T18:19:13.480

Reputation: 36 228

4:| wtf so many alternatives – ASCII-only – 2019-03-18T00:19:31.437

1@ASCII-only Least I could do on a simple challenge like this :p – Conor O'Brien – 2019-03-18T00:41:40.940

Print Last inputted byte. The programs contents fit with the challenge – MilkyWay90 – 2019-03-19T23:53:53.837

7

Brainf***, 7 bytes

,[>,]<.

SuperJedi224

Posted 2019-03-17T18:19:13.480

Reputation: 11 342

https://ideone.com/XoJLD0

It still doesn't work ;(

– jean – 2019-03-17T20:35:05.810

@jean Try it here. (Honestly, I didn't even know ideone did BF).

– SuperJedi224 – 2019-03-17T20:38:56.220

Yes, in your link everything works fine. But online judge for this problem uses ideone where it doesn't work ;( – jean – 2019-03-17T20:45:26.263

@jean ideone seems to use -1 as the EOF. +[>,+]<-. should work – Jo King – 2019-03-17T21:21:55.583

@Jo King Sorry, but the input looks like: 29845812674[enter][EOF] how can I print the last digit? – jean – 2019-03-17T21:59:06.577

@jean You have to make a new question for that challenge – MilkyWay90 – 2019-03-17T22:15:17.410

@jean do <<-. instead of <-.? – Jo King – 2019-03-17T22:35:31.843

@Jo King Yes!!! +[>,+]<<-. That's it!!! Thanks a lot!!! – jean – 2019-03-17T22:39:57.497

7

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

Suever

Posted 2019-03-17T18:19:13.480

Reputation: 10 257

I'd have gone for J)... – Sanchises – 2019-03-18T06:48:15.010

6

PHP, 13 bytes

<?=$argn[-1];

Try it online!

Run with php -nF input is STDIN. Example:

$ echo 29845812674|php -nF lost.php

640KB

Posted 2019-03-17T18:19:13.480

Reputation: 7 149

5

Javascript, 14 bytes

a=>a.slice(-1)

SuperJedi224

Posted 2019-03-17T18:19:13.480

Reputation: 11 342

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) – Matsyir – 2019-09-26T14:15:54.970

4

Python 3, 14 bytes

lambda x:x[-1]

Try it online!

MilkyWay90

Posted 2019-03-17T18:19:13.480

Reputation: 2 264

I feel dumb, how are you calling this without declaring it to a variable? – Nathan Dimmer – 2019-03-22T14:12:18.663

I looked at your TIO, but it doesn’t make much sense... What are you doing in your header? – Nathan Dimmer – 2019-03-22T14:14:53.587

1@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] – MilkyWay90 – 2019-03-22T20:14:56.143

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 – MilkyWay90 – 2019-03-22T20:16:51.877

That’s really cool! Thank you! – Nathan Dimmer – 2019-03-27T14:03:23.217

@NathanDimmer Thanks! When you make an anonymous lambda as a PPCG answer, you should use the format my answer used for the TIO link to make it easier to test your test cases – MilkyWay90 – 2019-03-29T20:57:48.150

4

Haskell, 9 4 bytes

last

Try it online!

flawr

Posted 2019-03-17T18:19:13.480

Reputation: 40 560

Why bother with pure? Isn't last enough? – dfeuer – 2019-03-19T07:22:34.230

I assumed that the output must be a string too, but you're right, OP is only talking about "bytes". – flawr – 2019-03-19T08:41:42.993

4

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.

Tau

Posted 2019-03-17T18:19:13.480

Reputation: 1 935

4

Bash + coreutils, 8 bytes

tail -c1

Input is from stdin, output is to stdout.

pizzapants184

Posted 2019-03-17T18:19:13.480

Reputation: 3 174

4

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.

kungfushark

Posted 2019-03-17T18:19:13.480

Reputation: 61

3

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)

Benjamin Urquhart

Posted 2019-03-17T18:19:13.480

Reputation: 1 262

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.

– Kevin Cruijssen – 2019-03-18T10:47:32.430

61 bytes. – Olivier Grégoire – 2019-03-19T13:03:06.130

3

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.

JPeroutek

Posted 2019-03-17T18:19:13.480

Reputation: 734

Actually, $$ instead of p1 should work without the warning for the same amount of bytes – Jo King – 2019-03-18T00:46:22.480

3

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.

Nick Kennedy

Posted 2019-03-17T18:19:13.480

Reputation: 11 829

-1 Fails for any number (https://tio.run/##y0rNyan8///hzlX///83MTY1NjE1MQYA)

– MilkyWay90 – 2019-03-17T22:01:54.377

@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. – ბიმო – 2019-03-17T22:16:07.857

Okay, I'll try seeing whether or not it will work as a link – MilkyWay90 – 2019-03-17T22:16:53.703

@ბიმო Seems to work (OP can you edit the answer so I can undo my downvote?) – MilkyWay90 – 2019-03-17T22:17:48.393

You can just define the T as a link and make it input a string, removing the need for "" in your input. – MilkyWay90 – 2019-03-18T23:00:06.583

3

><>, 2 bytes

Using command line args

o;

Try it online!

><>, 11 bytes

Using stdin

\~o;
/?(0:i

Try it online!

Emigna

Posted 2019-03-17T18:19:13.480

Reputation: 50 798

3

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

MickyT

Posted 2019-03-17T18:19:13.480

Reputation: 11 735

3

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

MilkyWay90

Posted 2019-03-17T18:19:13.480

Reputation: 2 264

3

Excel, 10 bytes

Pretty much equivalent to @remoel's VBA answer:

=RIGHT(A1)

Wernisch

Posted 2019-03-17T18:19:13.480

Reputation: 2 534

3

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!

EdgyNerd

Posted 2019-03-17T18:19:13.480

Reputation: 1 106

2

PowerShell, 11 bytes

"$args"[-1]

Try it online!

Gabriel Mills

Posted 2019-03-17T18:19:13.480

Reputation: 778

2

SmileBASIC, 16 bytes

INPUT S$?POP(S$)

12Me21

Posted 2019-03-17T18:19:13.480

Reputation: 6 110

2

05AB1E, 1 byte

¤

Try it online!

θ or ` would also work.

Emigna

Posted 2019-03-17T18:19:13.480

Reputation: 50 798

2

Bash, 13 bytes

echo ${1: -1}

string is passed as argument.

Try it online !

brokenfoot

Posted 2019-03-17T18:19:13.480

Reputation: 121

2

Python 3, 11 18 34 Bytes

import sys;print(sys.argv[-1][-1])

Usage via running the program as a python script on the command line. Input is provided as the last argument to the program.

Try it online!

Mrwerdo

Posted 2019-03-17T18:19:13.480

Reputation: 129

This does not print anything or return anything from a function - snippets are not allowed, only functions or full programs. – Stephen – 2019-03-18T00:08:07.910

Ah I see, didn’t think about that when I though of the answer. Only thought about running it in the interpreter. – Mrwerdo – 2019-03-18T00:24:56.910

38 – ASCII-only – 2019-03-18T00:41:22.747

114 – ASCII-only – 2019-03-18T00:44:26.387

1How about this? – Mrwerdo – 2019-03-18T00:51:59.737

that should work too – ASCII-only – 2019-03-18T00:53:34.497

The first -1 can just be 1, or alternatively, the lambda is valid – Jo King – 2019-03-18T09:51:15.730

2

Turing Machine Code, 72 42 bytes

Assumes an input with no empty cells (spaces). Thanks to ASCII-only for saving 30 bytes.

0 * * r 1
1 * * l 2
1 _ _ l halt
2 * _ r 0

Old version in 72 bytes:

0 * * r 0
0 _ * l 1
1 * * l 2
2 * _ l 2
2 _ _ r 3
3 _ _ r 3
3 * * * halt

Try it online.

SuperJedi224

Posted 2019-03-17T18:19:13.480

Reputation: 11 342

10 * * r 1/1 * * l 2/1 _ _ l halt/2 * _ r 0? – ASCII-only – 2019-03-18T00:22:42.320

oi pls reply :|| – ASCII-only – 2019-03-18T00:35:56.210

wouldn't work in what way? I've tested it online – ASCII-only – 2019-03-18T01:46:28.763

@ASCII-only It turns out you're correct, and I was simply misinterpreting the way your program actually worked. I think it's different enough that you can post it as a different answer if you want to. – SuperJedi224 – 2019-03-18T02:42:21.990

Well, this is a simple challenge, don't think it needs more than one answer in any language :P – ASCII-only – 2019-03-18T03:22:18.857

@ASCII-only Okay then. – SuperJedi224 – 2019-03-18T12:28:01.593

2

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)

Ismael Miguel

Posted 2019-03-17T18:19:13.480

Reputation: 6 797

Ok, that's the first time I see a Twig golf. – Ven – 2019-03-18T16:49:23.763

@Ven I've been doing it for quite a while. Here's an example: https://codegolf.stackexchange.com/a/174040/ and here's another one: https://codegolf.stackexchange.com/a/166800/ (I have a few more, but well)

– Ismael Miguel – 2019-03-18T16:59:38.503

2

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!

Quantum64

Posted 2019-03-17T18:19:13.480

Reputation: 371

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 – ASCII-only – 2019-03-18T10:24:44.223

Actually what you're looking at is output from a compiler, which can also be accessed via the online interface. – Quantum64 – 2019-03-18T14:45:07.613

Isn't each emoji more than one byte? I'd think they'd be two bytes at least. – Kyle Delaney – 2019-03-18T20:18:03.807

Emotion uses a custom code page. See https://quantum64.github.io/EmotionBuilds/1.1.0//?state=JTdCJTIybW9kZSUyMiUzQSUyMmNvZGVwYWdlJTIyJTdE

– JPeroutek – 2019-03-18T20:53:47.507

2

VBA (Excel), 14 12 bytes

using Immediate Window and Cell A1 as input

Thanks @tsh

?[RIGHT(A1)] or ?Right([A1],1)

remoel

Posted 2019-03-17T18:19:13.480

Reputation: 511

1Is 1 optional? – tsh – 2019-03-18T08:54:28.187

not on the second code. Thanks :) – remoel – 2019-03-18T08:59:28.097

2

IBM/Lotus Notes Formula, 11 bytes

@Right(i;1)

Computed field formula taking its input from editable field i

enter image description here

ElPedro

Posted 2019-03-17T18:19:13.480

Reputation: 5 301

2

C# 8.0, 8 bytes

Requires .NET Core 3.0, which is in beta. This currently crashes the CLR due to a bug, but once the bug is fixed, this will run as expected and fulfill the challenge requirements.

s=>s[^1]

C# 8.0, Runs without crashing at time of writing, 22 bytes

s=>s.ToCharArray()[^1]

C# 8.0, Full Program, 78 bytes

using C=System.Console;class A{static void Main(){C.Write(C.ReadLine()[^1]);}}

Arcanox

Posted 2019-03-17T18:19:13.480

Reputation: 121

The challenge mentions "output", so the last solution is probably the right one – Ven – 2019-03-18T16:48:26.893

Dang, you outdid my console answer by a lot. How does the ^1 work? – Stackstuck – 2019-03-18T20:21:15.603

It's the new Index type. Starting an index with a caret indicates it's from the end, i.e. array[^n] is the same as array[array.Length - n] – Arcanox – 2019-03-18T20:24:17.450

Interesting! I always try to stay up to date with new C# features. Do you have any link / reference on that? – mortb – 2019-03-19T09:00:22.083

1

Found it: https://docs.microsoft.com/en-us/dotnet/core/whats-new/dotnet-core-3-0

– mortb – 2019-03-19T09:06:04.153

2

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.

Ciaran_McCarthy

Posted 2019-03-17T18:19:13.480

Reputation: 689

2Seq.last is a function. You can remove the let s= – aloisdg moving to codidact.com – 2019-03-19T08:19:09.330

2

Retina, 10 9 bytes

(.|¶)*
$1

Try it online!

Sara J

Posted 2019-03-17T18:19:13.480

Reputation: 2 576

2

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

Unrelated String

Posted 2019-03-17T18:19:13.480

Reputation: 5 300

2

C (gcc), 31 bytes

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

Try it online!

Natural Number Guy

Posted 2019-03-17T18:19:13.480

Reputation: 211

O_o how does this even work – ASCII-only – 2019-03-31T10:52:48.163

I have no idea, it just works. – Natural Number Guy – 2019-03-31T14:07:42.250

Impressive list of complaints from the compiler! – roblogic – 2019-09-27T05:00:36.947

2

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.)

Alevya

Posted 2019-03-17T18:19:13.480

Reputation: 101

2

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.

Lyxal

Posted 2019-03-17T18:19:13.480

Reputation: 5 253

2

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

sporeball

Posted 2019-03-17T18:19:13.480

Reputation: 461

1

shrap

Posted 2019-03-17T18:19:13.480

Reputation: 126

1

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.

Stackstuck

Posted 2019-03-17T18:19:13.480

Reputation: 209

1

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.

Stackstuck

Posted 2019-03-17T18:19:13.480

Reputation: 209

1

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.

Jo King

Posted 2019-03-17T18:19:13.480

Reputation: 38 234

1

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

Neil

Posted 2019-03-17T18:19:13.480

Reputation: 95 035

1

dana

Posted 2019-03-17T18:19:13.480

Reputation: 2 541

you can use a function :P – ASCII-only – 2019-03-18T00:47:08.127

OK - Ill update my answer :) – dana – 2019-03-18T01:21:55.983

1uhm why is testcase in STDIN not footer – ASCII-only – 2019-03-18T01:38:02.970

Wow - It's been a while. Thanks for pointing that out ;) – dana – 2019-03-18T01:39:25.503

@Ven - This is an anonymous function that takes a string as input and returns a char. The OP mentions that functions are valid submissions. – dana – 2019-03-18T17:09:56.900

Challenge also says "is non-empty." – Ven – 2019-03-18T17:13:18.157

@Ven - It says the input is non-empty. My function should work if at least 1 character is specified (i.e. non-empty). Not sure I understand your question. – dana – 2019-03-18T17:31:05.627

1That's not how I understand it. I understand that it should print the last non-empty character. We need to ask OP. – Ven – 2019-03-18T17:33:18.303

1

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

Silas Reel

Posted 2019-03-17T18:19:13.480

Reputation: 111

1

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

Jo King

Posted 2019-03-17T18:19:13.480

Reputation: 38 234

1

Japt, 1 byte

Ì

Try it online!

-1 byte thanks to Quintec!

dana

Posted 2019-03-17T18:19:13.480

Reputation: 2 541

11 byte – Quintec – 2019-03-18T02:00:14.810

1

APL+WIN, 4 bytes

¯1↑⎕

Prompt for input string and select last byte.

Graham

Posted 2019-03-17T18:19:13.480

Reputation: 3 184

1

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.

Kevin Cruijssen

Posted 2019-03-17T18:19:13.480

Reputation: 67 575

1

Japt -h, 1 byte

U

Run it online

Oliver

Posted 2019-03-17T18:19:13.480

Reputation: 7 160

1

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.

Draco18s no longer trusts SE

Posted 2019-03-17T18:19:13.480

Reputation: 3 053

1

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.

SuperJedi224

Posted 2019-03-17T18:19:13.480

Reputation: 11 342

1

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!

Phlarx

Posted 2019-03-17T18:19:13.480

Reputation: 1 366

1

LiveScript, 8 bytes

(.[*-1])

Explanation:

(.[*-1])
(.[*-1]) # "BIOP": operator section à la Haskell
 .[   ]  # Index into the implicit argument
   *-1   # In [], "*" refers to the length

Ven

Posted 2019-03-17T18:19:13.480

Reputation: 3 382

1

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

reffu

Posted 2019-03-17T18:19:13.480

Reputation: 1 361

1

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!

CT Hall

Posted 2019-03-17T18:19:13.480

Reputation: 591

Simple trick to save 1 byte – Robert S. – 2019-03-25T21:23:44.753

132 bytes. – Robin Ryder – 2019-09-26T19:16:43.887

1

Japt -h, 1 byte

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

s

Try it

Shaggy

Posted 2019-03-17T18:19:13.480

Reputation: 24 623

1

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

qookie

Posted 2019-03-17T18:19:13.480

Reputation: 81

invalid, you need to change to printf – ASCII-only – 2019-03-31T10:55:34.277

1

Haskell, 4 bytes

last

Functions are allowed, right?

Also with IO (18 bytes):

main=interact$last

Mega Man

Posted 2019-03-17T18:19:13.480

Reputation: 1 379

The same answer was already posted, though we do allow duplicate answers as far as I know.

– Laikoni – 2019-03-24T21:22:04.643

1

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

user85052

Posted 2019-03-17T18:19:13.480

Reputation:

1

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

Dorian

Posted 2019-03-17T18:19:13.480

Reputation: 1 521

1

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!

Ampersanda

Posted 2019-03-17T18:19:13.480

Reputation: 121

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. – Unrelated String – 2019-09-27T04:58:43.433

1@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) – Ampersanda – 2019-09-27T05:30:56.810

1

Caboose, 1514 bytes

Caboose hates me, because it does. There isn't any convenient string-indexing instruction in Caboose!

var a=input();print(a.endsWith(' ')and' 'or a.endsWith('!')and'!'or a.endsWith('"')and'"'or a.endsWith('#')and'#'or a.endsWith('$')and'$'or a.endsWith('%')and'%'or a.endsWith('&')and'&'or a.endsWith("'")and"'"or a.endsWith('(')and'('or a.endsWith(')')and')'or a.endsWith('*')and'*'or a.endsWith('+')and'+'or a.endsWith(',')and','or a.endsWith('-')and'-'or a.endsWith('.')and'.'or a.endsWith('/')and'/'or a.endsWith('0')and'0'or a.endsWith('1')and'1'or a.endsWith('2')and'2'or a.endsWith('3')and'3'or a.endsWith('4')and'4'or a.endsWith('5')and'5'or a.endsWith('6')and'6'or a.endsWith('7')and'7'or a.endsWith('8')and'8'or a.endsWith('9')and'9'or a.endsWith(':')and':'or a.endsWith(';')and';'or a.endsWith('<')and'<'or a.endsWith('=')and'='or a.endsWith('>')and'>'or a.endsWith('?')and'?'or a.endsWith('@')and'@'or a.endsWith('A')and'A'or a.endsWith('B')and'B'or a.endsWith('C')and'C'or a.endsWith('D')and'D'or a.endsWith('E')and'E'or a.endsWith('F')and'F'or a.endsWith('G')and'G'or a.endsWith('H')and'H'or a.endsWith('I')and'I'or a.endsWith('J')and'J'or a.endsWith('K')and'K'or a.endsWith('L')and'L'or a.endsWith('M')and'M'or a.endsWith('N')and'N'or a.endsWith('O')and'O'or a.endsWith('P')and'P'or a.endsWith('Q')and'Q'or a.endsWith('R')and'R'or a.endsWith('S')and'S'or a.endsWith('T')and'T'or a.endsWith('U')and'U'or a.endsWith('V')and'V'or a.endsWith('W')and'W'or a.endsWith('X')and'X'or a.endsWith('Y')and'Y'or a.endsWith('Z')and'Z'or a.endsWith('[')and'['or a.endsWith('\\')and'\\'or a.endsWith(']')and']'or'~');

If I add more constants, then Caboose will say that there are too many constants in the chunk. Fortunately it passes all test cases given. Basically it (tries to) check the last character against all characters in printable ASCII.

TIO

user85052

Posted 2019-03-17T18:19:13.480

Reputation:

1

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

user85052

Posted 2019-03-17T18:19:13.480

Reputation:

0

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

Nathan Dimmer

Posted 2019-03-17T18:19:13.480

Reputation: 511

1you don't need the x= here – ASCII-only – 2019-03-31T10:57:28.960

0

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)

u_ndefined

Posted 2019-03-17T18:19:13.480

Reputation: 1 253

0

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

Reinstate Monica

Posted 2019-03-17T18:19:13.480

Reputation: 1 382

0

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.

Shaun Bebbers

Posted 2019-03-17T18:19:13.480

Reputation: 1 814

0

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

Dorian

Posted 2019-03-17T18:19:13.480

Reputation: 1 521

0

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.

BradC

Posted 2019-03-17T18:19:13.480

Reputation: 6 099

0

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.

booshlinux

Posted 2019-03-17T18:19:13.480

Reputation: 81

0

Zsh, 11 bytes

try it online!!

<<<${1: -1}

clone of the bash answer, 2 bytes shorter

roblogic

Posted 2019-03-17T18:19:13.480

Reputation: 554

0

Pushy, 1 byte

'

Try it online!

Print the top of stack as a character!

FlipTack

Posted 2019-03-17T18:19:13.480

Reputation: 13 242

0

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).

Bubbler

Posted 2019-03-17T18:19:13.480

Reputation: 16 616

0

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.

Mathgeek

Posted 2019-03-17T18:19:13.480

Reputation: 408

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. – manatwork – 2020-02-05T16:56:23.073

That's exactly what I meant - thank you, accidentally copy/pasted from the wrong solution file :) – Mathgeek – 2020-02-05T18:05:31.903

0

Burlesque, 2 bytes

[~

Try it online!

If it needs to be pretty formatted [~Q

[~ # Last char

DeathIncarnate

Posted 2019-03-17T18:19:13.480

Reputation: 916

0

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!

Value Ink

Posted 2019-03-17T18:19:13.480

Reputation: 10 608