77
\$\begingroup\$

The challenge is simple: Print the last, middle, and first character of your program's source code, in that order.

The middle character is defined as follows, assuming a source length of n characters, and 1-indexing:

  • If n is even, print the n/2-th and n/2 + 1-th character. (abcdef == cd)
  • If n is odd, print (n-1)/2 + 1-th character. (abcde == c)

Rules

  • Given no input, print the last, middle, and first character in your source code, in the form [last][middle][first]. This will be 3-4 characters long.
  • Output must not contain any trailing whitespace. However, if whitespace is a first, middle, or last character, it must be printed as such.
  • Source code must be n >= 3 characters long.
  • Code must consist of >= 3 unique characters.
  • Standard loopholes are forbidden.
  • This is , so shortest solution in characters wins.

Samples

# Form: [code] --> [output]
xyz --> zyx
abcd --> dbca
1 --> # not allowed: too short
abcde --> eca
aaabb --> # not allowed: not enough unique characters
System.out.print("S;pr"); --> ;prS
this is a test --> ts t
123[newline]45 --> 53[newline]1

Challenge Proposal

Leaderboards

Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.

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 leaderboard snippet:

# [><>](http://esolangs.org/wiki/Fish), 121 bytes

var QUESTION_ID=188005;
var OVERRIDE_USER=78850;
var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;function answersUrl(d){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+d+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(d,e){return"https://api.stackexchange.com/2.2/answers/"+e.join(";")+"/comments?page="+d+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(d){answers.push.apply(answers,d.items),answers_hash=[],answer_ids=[],d.items.forEach(function(e){e.comments=[];var f=+e.share_link.match(/\d+/);answer_ids.push(f),answers_hash[f]=e}),d.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(d){d.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),d.has_more?getComments():more_answers?getAnswers():process()}})}getAnswers();var SCORE_REG=function(){var d=String.raw`h\d`,e=String.raw`\-?\d+\.?\d*`,f=String.raw`[^\n<>]*`,g=String.raw`<s>${f}</s>|<strike>${f}</strike>|<del>${f}</del>`,h=String.raw`[^\n\d<>]*`,j=String.raw`<[^\n<>]+>`;return new RegExp(String.raw`<${d}>`+String.raw`\s*([^\n,]*[^\s,]),.*?`+String.raw`(${e})`+String.raw`(?=`+String.raw`${h}`+String.raw`(?:(?:${g}|${j})${h})*`+String.raw`</${d}>`+String.raw`)`)}(),OVERRIDE_REG=/^Override\s*header:\s*/i;function getAuthorName(d){return d.owner.display_name}function process(){var d=[];answers.forEach(function(n){var o=n.body;n.comments.forEach(function(q){OVERRIDE_REG.test(q.body)&&(o="<h1>"+q.body.replace(OVERRIDE_REG,"")+"</h1>")});var p=o.match(SCORE_REG);p&&d.push({user:getAuthorName(n),size:+p[2],language:p[1],link:n.share_link})}),d.sort(function(n,o){var p=n.size,q=o.size;return p-q});var e={},f=1,g=null,h=1;d.forEach(function(n){n.size!=g&&(h=f),g=n.size,++f;var o=jQuery("#answer-template").html();o=o.replace("{{PLACE}}",h+".").replace("{{NAME}}",n.user).replace("{{LANGUAGE}}",n.language).replace("{{SIZE}}",n.size).replace("{{LINK}}",n.link),o=jQuery(o),jQuery("#answers").append(o);var p=n.language;p=jQuery("<i>"+n.language+"</i>").text().toLowerCase(),e[p]=e[p]||{lang:n.language,user:n.user,size:n.size,link:n.link,uniq:p}});var j=[];for(var k in e)e.hasOwnProperty(k)&&j.push(e[k]);j.sort(function(n,o){return n.uniq>o.uniq?1:n.uniq<o.uniq?-1:0});for(var l=0;l<j.length;++l){var m=jQuery("#language-template").html(),k=j[l];m=m.replace("{{LANGUAGE}}",k.lang).replace("{{NAME}}",k.user).replace("{{SIZE}}",k.size).replace("{{LINK}}",k.link),m=jQuery(m),jQuery("#languages").append(m)}}
body{text-align:left!important}#answer-list{padding:10px;float:left}#language-list{padding:10px;float:left}table thead{font-weight:700}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/primary.css?v=f52df912b654"> <div id="language-list"> <h2>Winners 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><a href="{{LINK}}">{{SIZE}}</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr></tbody> </table> 

\$\endgroup\$
12
  • 6
    \$\begingroup\$ Maybe it's only me, but "Code must consist of >= 3 unique characters." suggests all characters of the program should be unique, while you only require to have at least 3 distinct characters. \$\endgroup\$ Jul 9, 2019 at 19:22
  • 4
    \$\begingroup\$ @Belhenix Fewer than three unique characters allows solutions as simple as 121 for a great many languages. \$\endgroup\$ Jul 9, 2019 at 19:32
  • 2
    \$\begingroup\$ If anyone can find a stack-based language that uses - for negation and implicitly prints with a linefeed, \n1- is a three-byter. My search has so far been fruitless. \$\endgroup\$ Jul 9, 2019 at 19:35
  • 3
    \$\begingroup\$ @Belhenix Whitespace is certainly possible, it has enough unique valid characters (space, linefeed, tab). \$\endgroup\$
    – bigyihsuan
    Jul 9, 2019 at 19:46
  • 3
    \$\begingroup\$ Can't change it now with 58 answers, but requiring "first letter of code must be different from last letter of code" would have ruled out a lot of trivial answers, including the current top answer. That may have been the intent of ">= 3 unique characters", but that requirement isn't actually very hard. \$\endgroup\$ Jul 10, 2019 at 14:10

144 Answers 144

1
\$\begingroup\$

HTML, 4 bytes

^db^

^<X>_<X>^

I'm not sure if this is cheating... Really.

First one also worked as PHP or Text.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ It's my bad that not creating a page for txeT before this challenge... \$\endgroup\$
    – tsh
    Jul 10, 2019 at 3:01
1
\$\begingroup\$

Lua, 31 13 bytes

print (')(p')

Prints )(p

Try it online!

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

MarioLANG, 53 bytes

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

Try it online!

Output: +++. The 27th character is one of the + characters in the second column of the code.

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

cQuents, 8 bytes

1#11,2&1

Try it online!

Outputs 11,1.

Explanation

1             Prepend 1 to the output
 #11,2        Default input is `11, 2`
      &       Output as many terms as the last input, comma seperated
       1      Each term equals 1

So first 1 is prepended, then the input becomes 11,2. Then, we output 2 terms comma separated, which is 1,1, so the final output is 11,1.

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

Perl 5, 11 bytes

say "\"\\s"

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ This violates the rule "Output must not contain any trailing whitespace. However, if whitespace is a first, middle, or last character, it must be printed as such." \$\endgroup\$
    – Chris
    Jul 11, 2019 at 12:29
  • \$\begingroup\$ @Abigail That still breaks the same rule about trailing whitespace. \$\endgroup\$
    – Chris
    Jul 30, 2019 at 10:24
1
\$\begingroup\$

BitCycle, 5 bytes


11!1

Try it online!

Outputs 11 with a trailing newline. Only the first pair of 1s is actually printed.

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

LOLCODE, 27 bytes

HAI 1
VISIBLE "E H"
KTHXBYE

Try it online!

Trivial answer in LOLCODE. The 14th character is the space just before the string literal.

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

Perl 5, 11 bytes


 say '; ';

Try it online!

Note the initial newline and the trailing space. Outputs

; 

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

Bash, 9 11 9 bytes

First attempt:

echo e\ o

Output

e o

Second attempt:

echo -n e-e

Output:

e-e

Third attempt

    
echo  oo

Note the leading newline and the double space

Output:

oo

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Welcome to codegolf.se. It should print in the order of the last, middle and first character, o e for your current code. \$\endgroup\$
    – jimmy23013
    Jul 10, 2019 at 15:19
  • 2
    \$\begingroup\$ Also “Output must not contain any trailing whitespace.” I'm afraid your contains, actually outputs 4 characters instead of 3. \$\endgroup\$
    – manatwork
    Jul 10, 2019 at 15:26
  • \$\begingroup\$ Now is correct, but became exact copy of Grzegorz Oledzki's Bash solution posted yesterday. \$\endgroup\$
    – manatwork
    Jul 11, 2019 at 13:06
1
\$\begingroup\$

SmileBASIC, 7 bytes

?":b?":

Output: :b?

(Probably works in most BASIC dialects) The middle character can be anything except ", LF, and CR.

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

F#, 13 bytes (characters)

So many esoteric languages that seem to be made specifically for code golfing… let's try a terse but 'normal' language like F#, where the following line (without a trailing newline) has 13 characters (13 bytes if the source file is saved as ANSI text):

printf"\"\"p"

Output:

""p
\$\endgroup\$
1
\$\begingroup\$

Pyth, 5 bytes


10 1

Prints 10 followed by a newline. Try it online!

Newlines normally print the next result followed by a newline, but at the start of a program it is essentially a no-op. 10 is the literal value, which is printed followed by a newline. The space before the 1 suppresses the usual implicit printing, so the final 1 is ignored.

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

Boolfuck, 29 21 bytes

;;;;;;;;;;;;;;;;;+

Outputs 3 null characters

Thanks to Jo King for the improved solution!

Try it online!

BitChanger, 23 bytes

<<<<<<<<<<}<<<}<}<}<

Very similar to the Boolfuck one: uses the fact the null characters require no bit changing to output with tape-based bit output

How it works:

           Used as it is simple to output
<<<<<<<<<< Moves over to one left of the I/O bit
}           Sets the I/O bit to one, for output
<<<         Moves over to the bit that performs I/O
}<}<}       Sets it to one 3 times, to output 3 null characters
<          I used < to waste one byte so it could be an odd number of bytes

Try it Online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Sure it matched the requirement “Code must consist of >= 3 unique characters.”? \$\endgroup\$
    – manatwork
    Jul 12, 2019 at 16:08
  • \$\begingroup\$ Oh yeah, fixed it \$\endgroup\$
    – EdgyNerd
    Jul 12, 2019 at 16:45
  • \$\begingroup\$ Oh, didn’t realise that. Does it auto fill the last byte of output to 8 bits? \$\endgroup\$
    – EdgyNerd
    Jul 13, 2019 at 7:55
1
\$\begingroup\$

Octave, 15 bytes

fprintf(')(f' )

Try it online!

As far as I know, using fprintf is the only method of out that doesn't come with extraneous output.

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

Python 2, 12 bytes 11 bytes

Placeholder to make the leading newline visible

print"\"t"
\$\endgroup\$
4
  • 1
    \$\begingroup\$ This is 12 bytes and hence is even, so you must print the 6th and 7th character of your code, which is ' ' and '"' so you should print " "p not " p like you currently do. Also please consider adding a link to e.g. Try It Online. \$\endgroup\$ Jul 15, 2019 at 10:19
  • 1
    \$\begingroup\$ It also prints an extra newline, which is not allowed. \$\endgroup\$
    – jimmy23013
    Jul 15, 2019 at 11:50
  • \$\begingroup\$ This 11 byte solution may be valid. Do you agree? @jimmy23013 \$\endgroup\$ Jul 15, 2019 at 14:27
  • \$\begingroup\$ @ExpiredData Yes it works. \$\endgroup\$
    – jimmy23013
    Jul 15, 2019 at 14:51
1
\$\begingroup\$

Rust (closure), 9 bytes

|| ";;|";

Try it online!


Rust (full program), 25 bytes

fn main(){print!("}}if")}

Try it online!

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

Pip, 15 bytes

O"}"xxL1{O"1O"}

Needs more golfing

Try it online!

Pip, 9 bytes


O"nn"x
n

shorter version with newline

Try it online!

Pip, 8 bytes

1x000Oh1

another one

Try it online!

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

Ohm v2, 5 bytes

…Lo…L

Try it online!

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

ink, 5 bytes


/a//

Prints /a and a newline

Try it online!

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

DOS COM x86 binary

% xxd test.com
00000000: 31c9 4b88 2f80 ffef 75f8 8007 30b4 028a  1.K./...u...0...
00000010: 17cd 2180 3f00 7503 e905 00fe 0fe9 f3ff  ..!.?.u.........
00000020: b402 8a17 cd21 8007 20b4 028a 17cd 2190  .....!.. .....!.
00000030: cd20 4b53 20                             . KS
\$\endgroup\$
1
\$\begingroup\$

x86-64 (Mac OS), 35 bytes

b8 04 00 00 02 bf 01 00 00 00 48 be 20 00 00 00
00 00 00 00 ba 03 00 00 00 0f 05 48 31 c0 c3 00
b8 00 b8

can be disassembled to:

[bits 64]

section .text
  global _main

_main:
  mov rax, 0x2000004
  mov rdi, 1
  mov rsi, x
  mov rdx, 3
  syscall
  xor rax, rax
  ret

section .data
  x: db 184, 0, 184

and compiled with:

$ nasm -fmacho64 FILE.asm
$ ld -fSystem FILE.asm

then run

$ ./a.out
<0xb8><0x00><0xb8>
\$\endgroup\$
1
\$\begingroup\$

Python 3, 13 bytes

print(')\'p')

Try it online!

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

Python 3, 13 bytes

This should suffice. Although it has a new line at the end of the output. There is a space in between print and '(' to make the total count of characters odd.

print (')(p')

Try it online!

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

W, 5 bytes


'+:+

Explanation

     % Newline (no-op)
'+   % The addition operator
  :+ % Multiply the character twice
     % Print with a newline
```
\$\endgroup\$
1
\$\begingroup\$

7, 11 characters

57125574032

Try it online!

Prints 255.

Explanation:

The program pushes 5|7255|4632 onto the frame (which starts with ||). Then, the last section of the frame (4632) is executed.

4 swaps the last two sections, and adds another bar between them. The frame is now ||5|4632||7255.

6 pacifies the last section of the frame by converting the active commands 7255 (shown in bold) to their passive equivalents 1255, and removing the bar before it. The frame now looks like ||5|4632|1255.

3 outputs the last section of the frame (1255) and discards the last two sections. The initial 1 means "print the rest unchanged", so 255 is printed. Then, the last two sections are discarded, leaving ||5 on the frame.

2 duplicates the last section of the frame, leaving ||5|5. There are no more characters left on the command list, so the last section of the frame (5) is executed.

5 moves the last section of the frame (which is another 5) to the start of the command list to be executed immediately, and then discards it. This leaves ||5 on the frame.

This happens again, leaving 5 on the command list and | on the frame.

The last 5 moves the last section of the frame (i.e. nothing) to the command list, leaving an empty command list and an empty frame. This causes the program to exit successfully.

Notes:

  • The 57 at the beginning and the 2 at the end are included because without them, one of the first, middle, or last characters would have been a 7, and printing 7 is hard.

  • The program can fit into 4 bytes, but the question asks to minimize characters.

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

Python 3, 13 bytes

print(")\"p")

Try It Online!

Having " as the middle char is lucky, because then I can balance the parity of the code because it requires an escape sequence.

There's a few more Python 3 13-byters, so I imagine this is the shortest possible solution. I don't there's a shorter solution with exit(), which prints to STDERR.

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

Width, 7 bytes

GAPSfeW

Try it online!

Outputs through STDERR.

I used a G to start the string. The ID of G is 39, which turns into eW. The ID of W is 55, which turns into AP. Then I just used Ai which is 50, and checked how close it was to A, saw it became R, then used Sf, which is 51, and becomes S.

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

Grok, 11 bytes

iq i`  wwwq        # Prints "q i"
\$\endgroup\$
1
\$\begingroup\$

Scratch, 23 bytes

when gf clicked
say[wcw
\$\endgroup\$
1
\$\begingroup\$

Racket, 15 bytes

(display "(y)")

Try it online!

\$\endgroup\$

Your Answer

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

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