3
\$\begingroup\$

The narcissist is a program which, given its own source as input, prints a truthy value and, given anything other than that, prints a falsey value. Today, we're reversing this.

Create a program that, given the name of whatever your language's truthy value is (True, true, 1), prints its own source. Given the name of whatever your language's falsey value is (False, false, 0), or any input other than these two, it should output nothing (empty string or array: erroring is acceptable).

Trailing whitespace is only good if your language needs it printed alongside with the code.

You need not account for all truthy and falsey values. You can choose one truthy and one falsey value for input.

For languages without truthy/falsey values, use 1 and 0.

This is code golf so the shortest program wins. Standard loopholes and quine rules apply.

\$\endgroup\$
  • \$\begingroup\$ What are "standard quine rules"? \$\endgroup\$ – attinat Jun 13 at 5:50
  • \$\begingroup\$ So given a truthy value, act as a quine, otherwise output nothing? Similar to this one but with exit instead of reverse \$\endgroup\$ – Jo King Jun 13 at 6:11
  • 7
    \$\begingroup\$ Do we need to account for all truthy or falsey values, or can we choose one truthy and one falsey value for input? \$\endgroup\$ – Sok Jun 13 at 8:59
  • 4
    \$\begingroup\$ For further challenges I recommend you to use the sandbox to get feedback before posting it here. \$\endgroup\$ – Luis felipe De jesus Munoz Jun 13 at 12:27
  • 1
    \$\begingroup\$ When you say we should "output nothing", are outputting an empty string/array or throwing an error acceptable? \$\endgroup\$ – Shaggy Jun 13 at 12:46

15 Answers 15

2
\$\begingroup\$

Gol><>, 8 bytes

sP#;H?I"

Try it online!

Adapted from my comment on this answer.

Explanation

sP        Pop and add 17 to the bottom of the stack (default 0)
  #       Reflect
sP        Add 17 again to make 34
       "  Wrapping string literal to push the source
     ?I   If the input is non-zero
    H     Halt and output the stack
   ;      Otherwise just halt
\$\endgroup\$
2
\$\begingroup\$

Java 10, 227 202 bytes (full program)

interface N{static void main(String[]a){var s="interface N{static void main(String[]a){var s=%c%s%1$c;if(new Boolean(a[0]))System.out.printf(s,34,s);}}";if(new Boolean(a[0]))System.out.printf(s,34,s);}}

-25 bytes thanks to @ripkoops.

Try it online.

Java 10, 98 bytes (lambda function)

b->{var s="b->{var s=%c%s%1$c;return b?s.format(s,34,s):%1$c%1$c;}";return b?s.format(s,34,s):"";}

Try it online.

explanation:

  • The var s contains the unformatted source code
  • %s is used to put this String into itself with s.format(...)
  • %c, %1$c, and 34 are used to format the double-quotes
  • s.format(s,34,s) puts it all together

And then a ternary if-else check is added, which prints this source code if truthy, or an empty string if falsey.

\$\endgroup\$
  • \$\begingroup\$ 202 bytes with printf instead of s.format \$\endgroup\$ – ripkoops Jun 14 at 4:12
  • \$\begingroup\$ @ripkoops Ah, of course. Thanks! :) \$\endgroup\$ – Kevin Cruijssen Jun 14 at 6:33
1
\$\begingroup\$

Haskell, 42 bytes

f n|n=putStr<>print$"f n|n=putStr<>print$"

Try it online!

A simple modification of the standard quine (current version of the Prelude with <> required). Throws an error when given False.

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

Brachylog, 20 bytes

≜!1∧"≜!1∧~kgjw₁"gjw₁

Try it online!

Brachylog doesn't really have truthy or falsy values, so this just assumes that 0 and 1 are valid choices regardless of actual truthiness.

≜                       Label the input,
 !                      and discard the resulting choice point.
  1                     If the input is 1,
   ∧"≜!1∧~kgjw₁"gjw₁    quine.

This could be reduced to 1∧"1∧~kgjw₁"gjw₁ if we only had to handle the chosen values, or only integers, or only literals--≜! is necessary to handle an empty input or variable input. A lot of different two-byte chunks of code could work in place of ≜!, such as any one of 023456789\^cxzạḅịḷṇụċṁṗẠḄḌẸḤỊḶṆṢṬṾẈẒĊĖṀȮṠṪπφ followed by |, but ≜! seems like the best for specifically catching an unbound input.

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

Python 2, 55 bytes

a=input();_='a=input();_=%r;print(_%%_*a)';print(_%_*a)

Try it online!

-10 bytes thanks to milkyway

Takes 1 or 0 as input. Technically you could give another integer but that produces weird behaviour.

\$\endgroup\$
  • \$\begingroup\$ My understanding of the challenge is that solutions must work for all truthy/falsey values, but I'm not sure - probably worth asking for clarification. \$\endgroup\$ – Shaggy Jun 13 at 8:03
  • \$\begingroup\$ I tried to make your answer better: __import__("sys").stderr=object();a=bool(eval(input()));_='__import__("sys").stderr=object();_=%r;print(_%%_*a)';print(_%_*a) it just suppresses error messages if it cannot eval the input, which may not be the best solution or even against the rules. \$\endgroup\$ – Gábor Fekete Jun 13 at 9:35
  • \$\begingroup\$ @Shaggy I'll wait for clarification from the OP but since Python's values are True and False which are 1 and 0 I don't think my solution is invalid. Not sure; I may have to bool-eval it. \$\endgroup\$ – HyperNeutrino Jun 13 at 12:07
  • \$\begingroup\$ @GáborFekete Sorry if I'm misunderstanding something, but how is that better? \$\endgroup\$ – HyperNeutrino Jun 13 at 12:08
  • 1
    \$\begingroup\$ Sometimes typos half-way across the keyboard do happen, but it's very rare \$\endgroup\$ – MilkyWay90 Jul 10 at 23:54
0
\$\begingroup\$

Pyth, 15 bytes

*QjN*2]"*QjN*2]

Try it online!

Input is either True or False. is a modification of the standard Pyth quine, jN*2]"jN*2].

*QjN*2]"*QjN*2]   Implicit: Q=eval(input()), N=quote mark
       "*QjN*2]   The string "*QjN*2]" - closing quote is inferred
      ]           Wrap in a list
    *2            Duplicate
  jN              Join on "
*Q                Multiply by Q - True is evaluated to 1, False to 0
                  Implicit print

Technically, a truthy value in Pyth is any value not in the following list: 0, 0.0, "", [], (), {}, set(), whereas my submission above only works with the strict values for True and False. If we need to cater for any truthy or falsey value, the following will work for 19 bytes: ?!QkjN*2]"?!QkjN*2] (TIO link). This replaces the multiplcation with ?!Qk, which uses the negation of the truthiness of the input (!Q) to select either an empty string (k) or the quine string through the ternary operator (?).

\$\endgroup\$
  • \$\begingroup\$ 13 bytes, compatible with all truthy and falsey inputs. A modification of the quine jN B"jN B. \$\endgroup\$ – hakr14 Jun 14 at 21:33
0
\$\begingroup\$

05AB1E, 21 18 bytes

"34çìIΘ·×"34çìIΘ·×

-3 bytes thanks to @Grimy.

Try it online.

Explanation:

NOTE: Only 1 is truthy in 05AB1E. Everything else is falsey.

It's based on this base quine: "34çìDJ"34çìDJ (or to be more correct: "34çì2×"34çì2×)

"34çìIΘ·×"          # Push string "34çìIΘ·×"
          34ç       # Push 34, converted to character '"'
             ì      # Prepend it in front of the string: '"34çìIΘ·×'
              I     # Push the input
               Θ    # Trutify it (==1)
                ·   # Double it
                 ×  # And repeat the string that many times
                    #  with truthy input 1: '"34çìIΘ·×"34çìIΘ·×'
                    #  with falsey input: ""
                    # (after which the result is output implicitly)
\$\endgroup\$
  • 1
    \$\begingroup\$ 2096239D20BJ is a shorter proper quine, not sure how you'd adapt it though. \$\endgroup\$ – Grimy Jun 13 at 11:13
  • \$\begingroup\$ @Grimy Oh, nice quine. You should post it here. (Although, not sure if the trailing newline is allowed.) But I don't think it can easily be modified for other quine challenges tbh. \$\endgroup\$ – Kevin Cruijssen Jun 13 at 11:25
  • 1
    \$\begingroup\$ Well, my quine can be adapted, but it's not pretty. Otoh, here's an 18 based on 34ç. \$\endgroup\$ – Grimy Jun 13 at 11:45
  • \$\begingroup\$ @Grimy Oh, nice! :) \$\endgroup\$ – Kevin Cruijssen Jun 13 at 12:02
0
\$\begingroup\$

JavaScript (Node.js), 16 bytes

Assuming the only falsey string is the empty one

f=_=>_?'f='+f:''

Try it online!


JavaScript (Node.js), 19 bytes

Assuming anything different from true, false, 1, 0 is falsey

f=_=>~_+1?'f='+f:''

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Why not simply _?...:''? By the way, the only falsey string in JS is the empty string. \$\endgroup\$ – Shaggy Jun 13 at 12:45
  • \$\begingroup\$ @Shaggy I assumed anything different from true, 1, false, 0 would be false (as it said in the challenge) but you are right tho \$\endgroup\$ – Luis felipe De jesus Munoz Jun 13 at 12:51
  • \$\begingroup\$ Does f=s=>s&&'f='+f work? \$\endgroup\$ – Neil Jun 13 at 17:09
  • \$\begingroup\$ @Neil it would output 0 instead of 'nothing' \$\endgroup\$ – Luis felipe De jesus Munoz Jun 13 at 17:10
  • \$\begingroup\$ @Neil, well, I suppose 0 technically is nothing! :D \$\endgroup\$ – Shaggy Jun 13 at 20:44
0
\$\begingroup\$

Japt, 14 bytes

Based on ETH's original standard quine. Expects 0 for falsey and 1 for truthy.

"iQ pUÑ"iQ pUÑ

Try it

"iQ pUÑ"iQ pUÑ     :Implicit input of integer U
"iQ pUÑ"           :Literal string
        i          :Prepend
         Q         :  Quotation mark
           p       :Repeat
            UÑ     :  U*2 times
\$\endgroup\$
0
\$\begingroup\$

Runic Enchantments, 12 bytes

"i1=?;3X4+k@

Try it online!

If it weren't for the "or any other value" having to print nothing I could save 2 bytes by removing 1=, but that allows for values like 11 to also print the quine. Note that the ascii character 0x01 implicitly converts to 1 and is effectively also a true value. The only way to distinguish it from numerical 1 would be to concatenate it as a string.

Explanation

The quine portion of this answer is actually pretty boring and straight forward, only i1=?; is added to meet challenge specifications:

"               Begin string mode
 i1=?;3X4+k@    Push the string literal i1=?;3X4+k@
"               Stop string mode
 i1=            Read input, compare to 1
    ?           Skip a number of instructions equal to the top of the stack (0 or 1)
     ;          If 0, terminate
      3X4+      If 1, push 34
          k     Convert to character (")
           @    Dump stack, top to bottom, to output and terminate.
\$\endgroup\$
0
\$\begingroup\$

lin, 53 bytes

in (1g@ dup outln out ) e|
in (1g@ dup outln out ) e|

Takes input via STDIN. Truthy value is empty input, falsy value is any other input.

Explanation

in (...) e| only executes whatever is within the parentheses if the input is empty. 1g@ retrieves the second line. dup outln out outputs 2 copies of the second line separated by a newline.

This takes advantage of the fact that lin only executes the program's first line (unless told otherwise by commands).

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

Perl 5 (-p), 29 24 bytes

Could save 2 bytes thanks to @Abigail, and some more thanks to this track

$_&&=<<''x2
$_&&=<<''x2

TIO

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

Charcoal, 40 bytes

≔´η´¿´N´«´´´≔´F´η´⁺´´´´´ι´ηη¿N«´≔Fη⁺´´ιη

Try it online! I can't actually seem specify an input of an arbitrary type (I get some weird zero-length truthy value if I try), so this just inputs numeric values and prints itself for any non-zero value. This is based on the standard Charcoal quine in the linked answer, with the actual printing wrapped inside a conditional (+3 bytes) plus that code then quoted inside the assignment (+6 bytes). Charcoal no longer outputs a trailing newline which saves 1 byte. If an exact match with 1 is required, then for 46 bytes:

≔´η´¿´¬´⊖´N´«´´´≔´F´η´⁺´´´´´ι´ηη¿¬⊖N«´≔Fη⁺´´ιη

Try it online!

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

Ruby -n, 46 bytes

Based on the Ruby heredoc quine. Falsy values are nil and false. Truthy values are basically every other valid Ruby object, including true, any number (even zero), any string (even the empty string), and any array (even an empty array).

puts <<2*2,2if eval$_
puts <<2*2,2if eval$_
2

Try it online!

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

C# (Visual C# Interactive Compiler), 108 bytes

var s="var s={0}{1}{0};if(bool.Parse(ReadLine())Write(s,'{0}',s);";if(bool.Parse(ReadLine()))Write(s,'"',s);

Try it online!

The above link demonstrates the truthy case. Below are some alternative inputs:

Based on my C# quine.

Here is a lambda variation:

C# (Visual C# Interactive Compiler), 79 bytes

b=>{var s="b=>{{var s={0}{1}{0};if(b)Write(s,'{0}',s);}}";if(b)Write(s,'"',s);}

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.