Intron Adder Quine

5

In the context of quines, an intron (or payload) is a part of a quine that serves no purpose in producing output, but is still included in the data section. For example:

function quine() {
    const data = "function quine() {\n    const data=\"@\";\n    // Arr! This be an intron!\n    return data.replace(\"@\", JSON.stringify(data));\n}";
    // Arr! This be an intron!
    return data.replace("@", JSON.stringify(data));
}

My challenge is to write a quine that will normally run as a standard quine, but if an argument is passed in, then it returns a new quine with the argument placed somewhere in it. If that new quine is called with an argument, it should do the same. For example:

function quine(intron="") {
    const data = "function quine(intron=\"\0\") {\n    const data = \"%%%\";\n    //\0\n    return data.replace(/\u0000/g, intron).replace(\"%%%\", JSON.stringify(data));\n}";
    //
    return data.replace(/\u0000/g, intron).replace("%%%", JSON.stringify(data));
}

A few statements:

  • If you have no input, then the output should be identical to the source code. If you have input, then the output only needs to:
    • Be in the same language
    • Contain the input
    • Be a solution to this challenge in its own right
  • Quines only need to contain the latest intron. Introns from the program that generated it are optional.
  • If your program already contains the input, then the same program is fine. For example, if it's called with the same input twice.
  • The input can be assumed to be printable ASCII only.
  • The shortest program in bytes wins.

Nissa

Posted 2018-04-09T21:17:50.967

Reputation: 3 334

So with no argument, the program should be a quine, and with an argument the output should be a quine with the argument as a contiguous sublist somewhere in the text? – dylnan – 2018-04-09T21:30:04.177

@dylnan You can put it anywhere: comments, literals, variable names, as long as you handle any printable ASCII. – Nissa – 2018-04-09T21:32:45.623

3I thought the usual term was payload – Jo King – 2018-04-09T21:34:21.390

@JoKing I got the term "intron" from this article on quines.

– Nissa – 2018-04-09T21:45:55.430

Related. – Martin Ender – 2018-04-09T22:26:58.433

"My challenge is to write a quine that will normally run as a standard quine" and "The returned program need only be similar in any way to the original if it's called without arguments" condradict each other. – Shaggy – 2018-04-10T07:16:06.387

Answers

1

V, 11 bytes

ñéÑvu"qpx:"

Try it online!

Hexdump:

00000000: f1e9 d176 7522 7170 783a 22              ...vu"qpx:"

This is a simple modification of the standard V quine. Given the input:

Hello

this program will output:

ñéÑvu"qpx:"Hello

Which outputs:

ñéÑvu"qpx:"Hello

This works because the input is automatically appended to the end of ñéÑvu"qpx, and :" is a comment (essentially)

James

Posted 2018-04-09T21:17:50.967

Reputation: 54 537

1Wouldn't a " in the input break it? – Jo King – 2018-04-10T11:41:13.103

1@joking Surprisingly, no. " is a line comment. – James – 2018-04-12T04:50:07.173

2

Python 2, 70 bytes

def f(x=''):s='def f(x=%r):s=%r;print s%%(x,s,x)#%s';print s%(x,s,x)#

Try it online!

A weird extension of the usual python quine s="s=%r;print s%%s";print s%s. This adds an extra %s after a comment, to output the data, and a %r in the default string to push the original string as default instead of an empty string.

Jo King

Posted 2018-04-09T21:17:50.967

Reputation: 38 234

I don't believe this supports input containing both ' and ". – Ørjan Johansen – 2018-04-10T18:05:52.790

@ØrjanJohansen Should be fixed – Jo King – 2018-04-11T00:24:27.553

I don't quite understand the purpose of the %r in the default, or of changing x in the function. Although it might be related to the question I just asked on the OP (in which case I don't think it actually works if you explicitly pass in the same string again.) – Ørjan Johansen – 2018-04-11T01:19:39.563

1

Python 3, 77 bytes

t='';s='t=input()or t;print(f"t={repr(t)};s={repr(s)};exec(s)#{t}")';exec(s)#

Try it online!

Full programs. Input must be terminated with a newline, and output a trailing newline.

Equivalent Python 2 would be 76 bytes (I think).

user202729

Posted 2018-04-09T21:17:50.967

Reputation: 14 620