10
\$\begingroup\$

Your goal in this code golf is to take two strings, language and code (if the golfed code you wrote for this is multiline, then this variable would be multiline.), and one integer, bytes. They represent the variables for a programming language, number of bytes it takes, and the code itself.

After that, you are going to format it like a code golfer does it.

The output variable is a multiline string called answer.

You can use multiline strings on:

If you search Stack Overflow, you should be able to find more programming languages that support it.

Here is a template of the output markdown. The code variable is in a code block, and there is a second-level header.

## {language}, {bytes} bytes

     {code}

Here is what the output would look like when pasted into a Markdown parser.

{language}, {bytes} bytes

    {code}

Variables are assumed to be filled out already as the language you coded your code golf entry in, the number of bytes it takes, and the actual code for it.

Here is another example of the output as code, this time with variables filled out:

## JavaScript, 1337 bytes

document.getElementById("foo").innerHTML = bar;

Here is the version as a blockquote:

JavaScript, 1337 bytes

    document.getElementById("foo").innerHTML = bar;

Bytes can be taken off from your code if you use a way to set the text of a <p> element in HTML by the id result, like the following JavaScript code:

document.getElementById("result").innerHTML = answer;

Make sure to also include the <p id="result"></p> in the HTML section of JSFiddle for this to work.

Scoring

Like all questions, the code that uses the least amount of bytes is the best.

\$\endgroup\$
17
  • 3
    \$\begingroup\$ Will {code} ever be a multiline code? \$\endgroup\$
    – Adnan
    Jul 9, 2016 at 23:54
  • 8
    \$\begingroup\$ "Variables will be filled out as the language you coded your code golf entry in, the number of bytes it takes, and the actual code for it." Am I the only one interpreting this as a quine variation? \$\endgroup\$
    – primo
    Jul 10, 2016 at 7:34
  • 2
    \$\begingroup\$ Since the answers are solving two completely different problems, I'm putting this on hold this as unclear. Please clarify whether language, bytes and code are input or whether this is a quine variant where those things should match the solving code itself. (And if so, whether directly or indirectly reading that source code is allowed, and whether quine built-ins are allowed.) \$\endgroup\$ Jul 10, 2016 at 10:36
  • 2
    \$\begingroup\$ Could you also clarify Adnan's question whether the input code can itself contain linefeeds? \$\endgroup\$ Jul 10, 2016 at 13:13
  • 2
    \$\begingroup\$ @Peanut I'm confused. Can we assume code will not be multiline, or can't we? Please indicate that in the challenge text \$\endgroup\$
    – Luis Mendo
    Jul 11, 2016 at 16:40

16 Answers 16

8
\$\begingroup\$

Java, 70 bytes

String A(String[]b){return"## "+b[0]+", "+b[1]+" bytes\n\n    "+b[2];}

Assumes b[0] is the language name, b[1] is the byte count, and b[2] is the code.

Making it compilable costs 9 bytes, resulting in a 79-byte non-standalone program:

class a{String A(String[]b){return"## "+b[0]+", "+b[1]+" bytes\n\n    "+b[2];}}

The equivalent monolithic program is 103 bytes long:

interface a{static void main(String[]A){System.out.print("## "+A[0]+", "+A[1]+" bytes\n\n    "+A[2]);}}

The monolithic one works with command line arguments, assuming these just like the non-standalone program:

  • First argument is the language name
  • Second argument is the byte count
  • Third argument is the code

Java (lambda expression), 56 48 bytes

(b)->"## "+b[0]+", "+b[1]+" bytes\n\n    "+b[2];

This is a java.util.function.Function<String[], String>.


None of these programs/functions handle multiline strings. To do so, simply replace b[2] and A[2] with b[2].replace("\n","\n ") and A[2].replace("\n","\n ") - doing so adds 23 bytes.

\$\endgroup\$
6
  • \$\begingroup\$ I'm not near a computer right now to confirm it, but can't you use \t or something to replace the 4 spaces after the two newlines? \$\endgroup\$
    – R. Kap
    Jul 10, 2016 at 0:00
  • \$\begingroup\$ @R.Kap Sometimes using tabs instead of spaces creates a mess on SE. \$\endgroup\$ Jul 10, 2016 at 0:04
  • \$\begingroup\$ Ah, I see. So does the byte count take into consideration \t and not 4 spaces? \$\endgroup\$
    – R. Kap
    Jul 10, 2016 at 0:28
  • 3
    \$\begingroup\$ Lose the parens on the lambda; you can save two bytes with just b->. \$\endgroup\$ Jul 10, 2016 at 1:58
  • 2
    \$\begingroup\$ What happens if there's more than one line of code? \$\endgroup\$
    – Neil
    Jul 10, 2016 at 8:55
5
\$\begingroup\$

V, 24 bytes

Note that there is a trailing space at the end.

This program makes the assumption that {code} will not be on multiple lines.

Op has clarified, "code" may be a multiline string. This 24 byte version works:

2é#á $á,JA bytes
vGî4é 

Since this contains an unprintable character, here is the readable version:

2é#á $á,JA bytes
<esc>vGî4é<space>

Where <esc> is the literal escape character, e.g. 0x1B.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ It always seems like a constant struggle between Jolf and V... :3 \$\endgroup\$ Jul 10, 2016 at 2:52
4
\$\begingroup\$

Python 3.5, 40 33 bytes:

(-7 bytes thanks to some clarification from Mego)

lambda*f:'## %s, %s bytes\n\n\t%s'%f

An anonymous lambda function that takes inputs as positional arguments in the format <function name>(String, Number, String) and outputs a multiline string.

Try It Online! (Ideone)

\$\endgroup\$
5
  • 2
    \$\begingroup\$ You can shave off several bytes if you make the parameter list *f and drop the tuple call. \$\endgroup\$
    – user45941
    Jul 10, 2016 at 3:23
  • \$\begingroup\$ @Mego Unfortunately, that does not work, as I get TypeError: not enough arguments for format string whenever I run it like that. \$\endgroup\$
    – R. Kap
    Jul 10, 2016 at 4:08
  • \$\begingroup\$ Works fine when I try it - you have to take positional arguments instead of a list argument. Additionally, you can drop the space between the # and the %. \$\endgroup\$
    – user45941
    Jul 10, 2016 at 4:10
  • \$\begingroup\$ @Mego Wow, that is so weird, because when I try it in PyCharm with Python 3.5.1, I get an error. That confused me so much. Also, the space between the # and the lambda is needed, or so I think. \$\endgroup\$
    – R. Kap
    Jul 10, 2016 at 4:12
  • \$\begingroup\$ @Mego It's updated. \$\endgroup\$
    – R. Kap
    Jul 10, 2016 at 4:16
4
\$\begingroup\$

JavaScript (ES6), 56 bytes

(l,b,c)=>`## ${l}, ${b} bytes

`+c.replace(/^/gm,`    `)

Also, for laughs, here is an answer that formats itself for me:

JavaScript (ES6), 68 bytes

f=_=>`## JavaScript (ES6), ${`${f}`.length+3} bytes\n\n    f=${f};`;

Print the result of f().

\$\endgroup\$
1
  • \$\begingroup\$ @PatrickRoberts Oops, I didn't actually check the length, I just copied and pasted it, then later added 6 bytes for the (ES6). (Why did Cᴏɴᴏʀ O'Bʀɪᴇɴ only bother to change the length?) \$\endgroup\$
    – Neil
    Jul 10, 2016 at 8:54
4
\$\begingroup\$

C#, 40 38 bytes

(a,b,c)=>$"## {a}, {b} bytes\n\n\t"+c;

C# lambda where inputs and output are strings.


C#, 59 bytes

(a,b,c)=>$"## {a}, {b} bytes\n\n\t"+c.Replace("\n","\n\t");

With handling of a multiline answer.


C#, 71 bytes

The 38 bytes solution which print itself

()=>$"## C#, 38 bytes\n\n\t"+@"(a,b,c)=>$""## {a}, {b} bytes

\t""+c;";

Try them online

\$\endgroup\$
3
\$\begingroup\$

Mathematica, 40 bytes

Print["## ",#,", ",#2,"bytes

    ",#3]&

Anonymous function. Takes the language, byte count, and program as input and prints the body to STDOUT.

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 21 bytes

Code:

“## ÿ, ÿ¡Ï
“,|v4ð×y«,

Uses the CP-1252 encoding. Try it online!.

\$\endgroup\$
4
  • \$\begingroup\$ This doesn't handle multiline strings \$\endgroup\$
    – DJMcMayhem
    Jul 10, 2016 at 16:59
  • \$\begingroup\$ @DrGreenEggsandIronMan Fixed. \$\endgroup\$
    – Adnan
    Jul 10, 2016 at 20:02
  • \$\begingroup\$ And so the V vs O5AB1E battle continues... Nice job! \$\endgroup\$
    – DJMcMayhem
    Jul 10, 2016 at 20:04
  • \$\begingroup\$ @DrGreenEggsandIronMan I'll be waiting for the next ASCII art challenge to take revenge :). \$\endgroup\$
    – Adnan
    Jul 10, 2016 at 20:05
2
\$\begingroup\$

Jolf, 24 22 bytes

"## ¦i, ¦j Ξ/u3

    ¦

Not much to explain here. ¦ means interpolated input.

Try it here! Output for the input in the link is:

## Jolf, 24 bytes

    some filler code I think

Input is as:

name

number

"code"
\$\endgroup\$
1
\$\begingroup\$

MATL, 28 27 bytes

1 byte saved thanks to @NinjaBearMoneky's suggestion

35tOj', 'j' bytes'10t4Z"j&h

Try it online!

The code block must be on a single line.

Explanation

35t           % Push 35 (ASCII for '#') twice
0             % Push 0. When converted to char, it will be displayed as a space
j             % Input string (language name)
', '          % Push this string
j             % Input string (byte count)
' bytes'      % Push this string
10t           % Push 10 (ASCII for linefeed) twice
4Z"           % Push string containing four spaces
j             % Input string (code)
&h            % Concatenate everything into a string. Implicitly display
\$\endgroup\$
0
\$\begingroup\$

Common Lisp, 57 bytes

(lambda(L b c)(format()"## ~A, ~A bytes~%~%    ~A"L b c))

Also, for fun, here below is a snippet of code which prints a header for itself.

Common Lisp, 146 bytes

#1=(PROGN
(SETF *PRINT-CIRCLE* T)
(LET ((S (FORMAT NIL "~S" '#1#)))
  (FORMAT NIL "## Common Lisp, ~A bytes~%~%    ~A" (LENGTH S) S)))
\$\endgroup\$
0
\$\begingroup\$

Ruby, 63 bytes

def f(l,b,c) s="\#\# #{l}, #{b} bytes\n\n    #{c}";return s;end

Try it online: http://codepad.org/EIn0Gw9M

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

hashmap, 29 bytes.

"## "i", "h" bytes.\n\n    "i

(I wrote that post with the program, but I had to escape \n into \\n)
Explanation:

"## "                         Push string
     i", "                    Push input and string
          h" bytes.\n\n    "i Push input as number and string, then the input.
\$\endgroup\$
0
\$\begingroup\$

CJam, 26 23 bytes

Thanks to @NinjaBearMonkey for removing 3 bytes!

'#_Sl',Sl" bytes"N_S4*l

The code block must be on a single line.

Try it online!

Explanation

'#_S       e# Push character "#" twice, then a space
l          e# Read line from input
',S        e# Push a comma, then a space
l          e# Read line from input
" bytes"   e# Push this string
N_S4*      e# Push newline twice, then four spaces
l          e# Read line from input. Implicitly display 
\$\endgroup\$
2
  • \$\begingroup\$ 23 bytes: '#_Sl',Sl" bytes"N_S4*l \$\endgroup\$ Jul 10, 2016 at 14:14
  • \$\begingroup\$ @NinjaBearMonkey Thanks! Edited. This idea saved a byte also for my other answer \$\endgroup\$
    – Luis Mendo
    Jul 10, 2016 at 15:15
0
\$\begingroup\$

Pyke, 25 bytes

"##"Q", "z" bytes"skd4*z+

Try it here!

Or

Pyke, 0 bytes

Try it here! - click the copy answer button ;)

EDIT - It's just a feature of the website, it's cheating (or at least I would consider it so) because it never parses an AST and the web program probably isn't considered part of the language due to it not interacting with the language much (even though it is running the Pyke interpreter)

\$\endgroup\$
5
  • \$\begingroup\$ What makes the 0 bytes version work? \$\endgroup\$ Jul 11, 2016 at 13:19
  • \$\begingroup\$ Can you add that to the answer? I'm not counting that but you can still keep it up there. \$\endgroup\$ Jul 11, 2016 at 13:37
  • \$\begingroup\$ Hey @muddyfish, can you separate these into two answers? \$\endgroup\$ Mar 1, 2017 at 3:04
  • \$\begingroup\$ @haykam if this is for the purposes of accepting an answer, I'd rather not as I don't think the 0 byte solution counts as actually using Pyke and feels too chesty \$\endgroup\$
    – Blue
    Mar 1, 2017 at 12:01
  • \$\begingroup\$ I won't accept that answer. \$\endgroup\$ Mar 1, 2017 at 19:48
0
\$\begingroup\$

Perl 5, 35 bytes

A full program, this takes input as command-line arguments in reverse order. It requires -M5.01, which is free.

say pop.', '.pop.' bytes

    '.pop
\$\endgroup\$
0
\$\begingroup\$

Emacs Lisp, 97 bytes

(lambda(l c)(format"## %s, %s bytes\n\n%s"l(string-bytes c)(replace-regexp-in-string"^""    "c)))

Also, since it can sometimes be tough to escape quotes and what not, a usage example that copies the string to the clipboard. (Mark region and use M-:)

Emacs Lisp, 184 bytes

(kill-new ((lambda(l c)(format"## %s, %s bytes\n\n%s"l(string-bytes c)(replace-regexp-in-string"^""    "c)))"Emacs Lisp"(buffer-substring-no-properties(region-beginning)(region-end))))
\$\endgroup\$

Your Answer

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

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