15
\$\begingroup\$

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

\$\endgroup\$
  • 3
    \$\begingroup\$ Hmm.. So like a 'quat' (quine + cat)? \$\endgroup\$ – lol May 2 '17 at 13:10

13 Answers 13

7
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ Very creative to use the variable name. \$\endgroup\$ – Leaky Nun May 2 '17 at 5:52
  • 3
    \$\begingroup\$ Doesn't work for input "for", or any other reserved word in Python. \$\endgroup\$ – HyperNeutrino May 2 '17 at 14:21
  • \$\begingroup\$ Can confirm. It doesn't work for all possible inputs. Try it online \$\endgroup\$ – mbomb007 May 2 '17 at 15:15
  • \$\begingroup\$ @HyperNeutrino, Noted, also no idea how to get around it \$\endgroup\$ – c.. May 2 '17 at 22:33
  • \$\begingroup\$ You could append an extra character that isn't at the end of any keyword, such as 'x'. \$\endgroup\$ – Ørjan Johansen May 2 '17 at 22:45
6
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

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 :^.
\$\endgroup\$
  • \$\begingroup\$ Doesn't really work for )( as the input, I think. \$\endgroup\$ – Rɪᴋᴇʀ May 2 '17 at 14:36
  • \$\begingroup\$ @Riker True, Underload unfortunately doesn't support mismatched parentheses. But the question specifies a-z. \$\endgroup\$ – Ørjan Johansen May 2 '17 at 14:37
  • \$\begingroup\$ Ah, okay. Didn't notice that. \$\endgroup\$ – Rɪᴋᴇʀ May 2 '17 at 14:39
3
\$\begingroup\$

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).
\$\endgroup\$
3
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Ørjan Johansen May 3 '17 at 14:55
  • \$\begingroup\$ 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. \$\endgroup\$ – user62131 May 3 '17 at 18:46
2
\$\begingroup\$

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.

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

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

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

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!

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

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.

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

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)
\$\endgroup\$
0
\$\begingroup\$

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";.

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

JavaScript, 21 bytes

$=_=>`$=${$}/*${_}*/`
\$\endgroup\$
0
\$\begingroup\$

Perl 5, 35 bytes

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

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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