Code golf for the lazy

10

1

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.

haykam

Posted 2016-07-09T23:13:31.893

Reputation: 784

1Can the header look like # Language Name, Byte Count instead of ## Language Name, Byte Count? – user8397947 – 2016-07-09T23:32:06.210

@dorukayhan No. – haykam – 2016-07-09T23:42:50.137

3Will {code} ever be a multiline code? – Adnan – 2016-07-09T23:54:09.673

Does our output have to exactly match yours, or does it just have to be valid mark down? – James – 2016-07-09T23:55:48.433

@DrGreenEggsandIronMan, it needs to be an exact match except the variables have to be replaced. – haykam – 2016-07-10T00:11:27.697

1Can't you use the builtin js snippet instead of JSFiddle? – None – 2016-07-10T01:29:17.583

8"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? – primo – 2016-07-10T07:34:54.717

@primo yeah, I'm actually very confused by all the current answers. E.g. the top-rated Java one seems to be assuming the existence of a variable b such that b[0]=="Java" && b[1]=="70" && b[2]=="String A(String[]b){return\"## \"+b[0]+\", \"+b[1]+\" bytes\\n\\n \"+b[2];}", which really feels like cheating. I would have expected that the program would have to generate those outputs itself, not read them in from a magic variable.

– Quuxplusone – 2016-07-10T08:17:25.137

2Since 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 [tag: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.) – Martin Ender – 2016-07-10T10:36:35.720

1@MartinEnder They are inputs. code and language are strings, and bytes are integers. – haykam – 2016-07-10T13:11:14.260

2Could you also clarify Adnan's question whether the input code can itself contain linefeeds? – Martin Ender – 2016-07-10T13:13:35.050

1@Adnan, yes, you can do that just like you do with the answer. – haykam – 2016-07-10T13:14:30.383

@MartinEnder Okay, I've clarified that to them. I'm also editing the question to make sure people understand that we're assuming all the variables except answer will already be filled out. – haykam – 2016-07-10T13:15:55.447

If {code} contains linefeeds, do we need to indent each line with four spaces, or only the first? – msh210 – 2016-07-11T15:47:45.960

@msh210 Do whatever you want. – haykam – 2016-07-11T15:49:44.367

1@Peanut You realize that in an actual PPCG answer, you'd have to indent each line for it to be formatted correctly? – mbomb007 – 2016-07-11T16:07:46.670

2@Peanut I'm confused. Can we assume code will not be multiline, or can't we? Please indicate that in the challenge text – Luis Mendo – 2016-07-11T16:40:22.947

Answers

2

05AB1E, 21 bytes

Code:

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

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

Adnan

Posted 2016-07-09T23:13:31.893

Reputation: 41 965

This doesn't handle multiline strings

– James – 2016-07-10T16:59:22.617

@DrGreenEggsandIronMan Fixed. – Adnan – 2016-07-10T20:02:40.407

And so the V vs O5AB1E battle continues... Nice job! – James – 2016-07-10T20:04:02.123

@DrGreenEggsandIronMan I'll be waiting for the next ASCII art challenge to take revenge :). – Adnan – 2016-07-10T20:05:07.513

8

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.

user8397947

Posted 2016-07-09T23:13:31.893

Reputation: 1 242

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? – R. Kap – 2016-07-10T00:00:27.510

@R.Kap Sometimes using tabs instead of spaces creates a mess on SE. – user8397947 – 2016-07-10T00:04:30.080

Ah, I see. So does the byte count take into consideration \t and not 4 spaces? – R. Kap – 2016-07-10T00:28:39.213

@R.Kap Exactly. – user8397947 – 2016-07-10T00:36:56.440

3Lose the parens on the lambda; you can save two bytes with just b->. – Brian McCutchon – 2016-07-10T01:58:01.230

2What happens if there's more than one line of code? – Neil – 2016-07-10T08:55:49.253

5

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.

James

Posted 2016-07-09T23:13:31.893

Reputation: 54 537

1It always seems like a constant struggle between Jolf and V... :3 – Conor O'Brien – 2016-07-10T02:52:57.743

4

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

Neil

Posted 2016-07-09T23:13:31.893

Reputation: 95 035

@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?) – Neil – 2016-07-10T08:54:40.173

4

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

aloisdg moving to codidact.com

Posted 2016-07-09T23:13:31.893

Reputation: 1 767

4

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)

R. Kap

Posted 2016-07-09T23:13:31.893

Reputation: 4 730

2You can shave off several bytes if you make the parameter list *f and drop the tuple call. – Mego – 2016-07-10T03:23:36.053

@Mego Unfortunately, that does not work, as I get TypeError: not enough arguments for format string whenever I run it like that. – R. Kap – 2016-07-10T04:08:34.933

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 %. – Mego – 2016-07-10T04:10:38.817

@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. – R. Kap – 2016-07-10T04:12:01.890

@Mego It's updated. – R. Kap – 2016-07-10T04:16:44.183

3

Mathematica, 40 bytes

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

    ",#3]&

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

LegionMammal978

Posted 2016-07-09T23:13:31.893

Reputation: 15 731

2

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"

Conor O'Brien

Posted 2016-07-09T23:13:31.893

Reputation: 36 228

1

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

Luis Mendo

Posted 2016-07-09T23:13:31.893

Reputation: 87 464

0

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

coredump

Posted 2016-07-09T23:13:31.893

Reputation: 6 292

0

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

dkudriavtsev

Posted 2016-07-09T23:13:31.893

Reputation: 5 781

0

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 

Luis Mendo

Posted 2016-07-09T23:13:31.893

Reputation: 87 464

23 bytes: '#_Sl',Sl" bytes"N_S4*l – NinjaBearMonkey – 2016-07-10T14:14:33.553

@NinjaBearMonkey Thanks! Edited. This idea saved a byte also for my other answer – Luis Mendo – 2016-07-10T15:15:41.963

0

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.

user47018

Posted 2016-07-09T23:13:31.893

Reputation:

0

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)

Blue

Posted 2016-07-09T23:13:31.893

Reputation: 26 661

What makes the 0 bytes version work? – haykam – 2016-07-11T13:19:44.697

Can you add that to the answer? I'm not counting that but you can still keep it up there. – haykam – 2016-07-11T13:37:02.253

Hey @muddyfish, can you separate these into two answers? – haykam – 2017-03-01T03:04:11.263

@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 – Blue – 2017-03-01T12:01:32.797

I won't accept that answer. – haykam – 2017-03-01T19:48:56.617

0

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

msh210

Posted 2016-07-09T23:13:31.893

Reputation: 3 094

0

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

Lord Yuuma

Posted 2016-07-09T23:13:31.893

Reputation: 587