Print a quine that contains the input

15

0

Task

You will be given a string in the input consisting only of the characters from a to z, i.e. the input will match the regex /^[a-z]*$/.

Your output should be a complete program in the same language, whose source code contains the input, and is a proper quine.

Example

Your program would receive the input abc and output:

...abc...

The above should be a complete program in the same language which takes no input and outputs:

...abc...

i.e. it outputs itself.

Scoring

This is . Shortest answer in bytes wins.

References

Leaky Nun

Posted 2017-05-02T04:42:06.447

Reputation: 45 011

3Hmm.. So like a 'quat' (quine + cat)? – Matthew Roh – 2017-05-02T13:10:29.340

Answers

7

Python 3, 57 61 bytes

lambda x:"s='s=%r;print(s%%s)';print(s%s)".replace('s',x+'x')

Takes a basic python 3 quine from here and replaces the variable name with the input.

Note: As pointed out by Hyper Neutrino in comments, this does not work for reserved keywords like for, if, etc.

Appending a character that none of the reserved keywords ends with such as 'x' or any number fixes this. (Ørjan Johansen).

c..

Posted 2017-05-02T04:42:06.447

Reputation: 191

Very creative to use the variable name. – Leaky Nun – 2017-05-02T05:52:47.443

3Doesn't work for input "for", or any other reserved word in Python. – HyperNeutrino – 2017-05-02T14:21:18.213

Can confirm. It doesn't work for all possible inputs. Try it online

– mbomb007 – 2017-05-02T15:15:24.647

@HyperNeutrino, Noted, also no idea how to get around it – c.. – 2017-05-02T22:33:37.423

You could append an extra character that isn't at the end of any keyword, such as 'x'. – Ørjan Johansen – 2017-05-02T22:45:59.067

@ØrjanJohansen Can't believe I didn't think of that, thanks! – c.. – 2017-05-02T23:27:41.173

6

Jelly, 9 bytes

Generator

;“¶Øv”ṾØv

Try it online!

How it works

;“¶Øv”ṾØv  Main link. Argument: s (string of letters)

;“¶Øv”     Concatenate s and the string "\nØv".
      Ṿ    Uneval; get its string representation.
           (implicit) Print the previous return value since the next link is an
           otherwise unparsable nilad.
       Øv  Set the return value to "Ṙv".

Quine

If the input is quine, the following program is generated.

“quine
Øv”Ṙv

Try it online!

How it works

This is the standard Jelly quine. First,

“quine
Øv”

sets the left argument and the return value to the string "quine\nØv".

Then, prints a string representation (the code from the previous block) and returns the unaltered string.

Afterwards, v takes the left argument and passes it as input to the Jelly program

quine
Øv

In all Jelly programs, only the main link (defined on the last line) is executed, so the first line is ignored entirely.

Finally, Øv sets the return value to "Ṙv", which is printed implicitly when the outer program finishes.

Dennis

Posted 2017-05-02T04:42:06.447

Reputation: 196 637

8 bytes – Erik the Outgolfer – 2018-03-17T18:16:26.597

4

Haskell, 51 bytes

q takes a string and returns a string.

q s|t<-"main=putStr$fst`mappend`show$"=t++show(t,s)

Try it online!

Example output for putStr$q"test":

main=putStr$fst`mappend`show$("main=putStr$fst`mappend`show$","test")

Try it online!

  • Puts the main quine text and the desired string in a tuple.
  • Uses fst to extract the main text.
  • Uses show to turn the whole tuple into a string.
  • Uses mappend to combine the two previous functions. Conveniently mappend on two functions gives a function that applies each function to its argument and combines the results with mappend for the result type (here string concatenation).

Ørjan Johansen

Posted 2017-05-02T04:42:06.447

Reputation: 6 914

4

Underload, 14 bytes

(~aSaS(:^)S):^

Try it online!

Use as (test)(~aSaS(:^)S):^ – which is itself a quine.

How it works

  • Underload is a concatenative (stack-based) esoteric language. It doesn't support reading input, so any arguments are put on the stack initially.
  • (test) and (~aSaS(:^)S) are string literals, so place themselves on the stack, with the latter on top.
  • : duplicates the (~aSaS(:^)S) string on top of the stack, then ^ runs its contents as a subprogram.
  • ~ swaps the top two elements on the stack, so now (test) is uppermost.
  • a wraps (test) in extra parentheses.
  • S takes the string ((test)) on top of the stack, and prints it without the outer parentheses (which are just literal syntax).
  • Now aS prints the remaining (~aSaS(:^)S) on the stack similarly (with the parentheses).
  • At last, (:^)S prints the final :^.

Ørjan Johansen

Posted 2017-05-02T04:42:06.447

Reputation: 6 914

Doesn't really work for )( as the input, I think. – Rɪᴋᴇʀ – 2017-05-02T14:36:39.927

@Riker True, Underload unfortunately doesn't support mismatched parentheses. But the question specifies a-z. – Ørjan Johansen – 2017-05-02T14:37:33.190

Ah, okay. Didn't notice that. – Rɪᴋᴇʀ – 2017-05-02T14:39:19.913

4

Underload, 14 bytes

a(aS(:^)S)~*:^

Try it online!

A different approach from the other Underload answer; rather than itself being a quine, this constructs a quine. Interestingly, it comes out to the same number of bytes. This is a function that takes its argument from the stack and outputs to standard output.

Explanation

a(aS(:^)S)~*:^
a               Generate a string literal containing the input
 (aS(:^)S)~*    Prepend "aS(:^)S"
            :^  Mockingbird: run the resulting function with itself as argument

The resulting function looks like this:

aS(:^)S(input)
aS              Print a string literal containing the argument
  (:^)S         Print ":^"
       (input)  Push "input" onto the stack

In other words, it prints a string literal containing itself, followed by :^. This is clearly a quine (because what was just printed is the same as the code we executed to run it in the first place).

user62131

Posted 2017-05-02T04:42:06.447

Reputation:

It looks like in TIO, you can drop the initial a, as long as you don't mind the segmentation fault error printed to stderr when the input contains a. – Ørjan Johansen – 2017-05-03T14:55:06.230

I didn't even think of just executing the string literal that the user gives, on the basis that the program's work is already finished at that point and without ( being a character that appears in the input, all programs will necessarily do nothing or crash. I don't even think that's specific to TIO; the program is invalid, but it's already produced the desired output at the time it crashes. – None – 2017-05-03T18:46:56.293

2

V, 9 bytes

ñ"qPxÉÑ~j

Try it online!

This is a modification of the standard V quine, and I'm proud that this is only one byte longer.

Hexdump:

00000000: f122 7150 78c9 d17e 6a                   ."qPx..~j

Explanation:

ñ           " Record the following commands into register 'q'
 "qP        " Paste register 'q' before all input
    x       " Delete the last character of what we just pasted (this will be a 'ÿ')
     ÉÑ     " Insert 'Ñ' at the beginning of this line
       ~    " Toggle the case of this character
        j   " Move down a line. During playback, this will cancel playback of the current macro,
            " So everything after here is a NOOP

Then, the recording implicitly stops and is played back. This will generate the following output:

ñ"qPxÉÑ~jHello

Since j will break playback of the macro, nothing in Hello will ever get run.

James

Posted 2017-05-02T04:42:06.447

Reputation: 54 537

2

Python 2, 38 bytes

Though input is only required to support a-z, this should work with any single-line input that doesn't contain NUL bytes.

s='s=%r;print s%%s#'+input();print s%s

Try it online

For input abc the resulting quine is:

s='s=%r;print s%%s#abc';print s%s#abc


Try it online

mbomb007

Posted 2017-05-02T04:42:06.447

Reputation: 21 944

1

RProgN 2, 15 bytes

"{']C'.%q[}]C"F

Explained

Using the format

{']C'.%q[}]C}

where %q is the qouted input, builds a quine of the flavour

{']C'."Some Text"[}]C

which is a standard RProgN2 quine, {']C'.}]C That, before finished, appends and destroys the inputted string.

Try it online!

ATaco

Posted 2017-05-02T04:42:06.447

Reputation: 7 898

1

Retina, 14 bytes

Byte count assumes ISO 8859-1 encoding.

\(\`^
¶\(*S1`|

Try it online!

For input x, this outputs:


\(*S1`|x
\(*S1`|x

Explanation

The output is a minor modification of the standard quine. We simply use the regex |x instead of the empty regex. Since the | still allows (and prioritises) and empty match, the functionality itself is not affected, and since x will only ever contain letters, it's guaranteed to be valid regex syntax itself.

Printing this actually uses a technique which is similar to the quine itself. To avoid the duplication of the quine, we insert ¶\(*S1`| only once into the beginning of the string. That's exactly half the source code. To print it twice without a linefeed, we use the configuration \(\, which wraps the entire program in a group and makes both the stage itself as well as the group containing it print the result without a linefeed.

Martin Ender

Posted 2017-05-02T04:42:06.447

Reputation: 184 808

1

Japt, 14 bytes

"\"iQ ²ª`"+U ²

Test it online! For an input of abc, outputs

"iQ ²ª`abc"iQ ²ª`abc

which outputs itself. Test it online!

Explanation

This is an extension of the standard payload-capable Japt quine:

"iQ ²"iQ ²

"iQ ²"      // Take this string.  iQ ²
      iQ    // Insert a quote.    "iQ ²
         ²  // Repeat this twice. "iQ ²"iQ ²
            // Implicit: output result of last expression

The only difference is that we append ª`abc at the end, which in JavaScript is basically ||"abc". Since the result of the first part is always a non-empty string (truthy), the || never gets run.

There are several alternative versions of the same length:

"iQ ²ª`abc"iQ ²ª`abc   quine||"abc"
"iQ ²ª$abc"iQ ²ª$abc   quine||abc
"iQ ²ª{abc"iQ ²ª{abc   quine||function(){ ... }
"iQ ²ªXabc"iQ ²ªXabc   quine||X.a(...) (X could be any uppercase letter or digit)
"iQ ²//abc"iQ ²//abc   quine//abc      (// is a comment in JS/Japt)
"iQ ²;[abc"iQ ²;[abc   quine;          (unmatched [ causes a parsing error)

ETHproductions

Posted 2017-05-02T04:42:06.447

Reputation: 47 880

0

CJam, 16 14 bytes

"`_~"q`';++`_~

Try it online!

How it works

"`_~"           e# Push "`_~"
     q`         e# Push a string representation of the input (input wrapped in quotes)
       ';       e# Push a semicolon
         ++     e# Concatenate all this together
           `    e# Get the string representation of the resulting string
            _~  e# Duplicate it and eval it (pushing the original string on the stack again)

Which outputs something like "`_~\"test\";"`_~"test";.

Business Cat

Posted 2017-05-02T04:42:06.447

Reputation: 8 927

0

JavaScript, 21 bytes

$=_=>`$=${$}/*${_}*/`

ericw31415

Posted 2017-05-02T04:42:06.447

Reputation: 2 229

0

Perl 5, 35 bytes

$_=q{say"\$_=q{$_};eval"#}.<>;eval

Try it online!

Dom Hastings

Posted 2017-05-02T04:42:06.447

Reputation: 16 415