Inverse-quotes-quine

29

2

The prospect of this challenge is:

  • If your program is run normally, all of the code in the speech marks (" - double quotes) should be printed.
  • If your program is wrapped in double quotes (in turn inverting the speech marks), the code that is normally not in quotes should be printed.

E.g:

Let's say you have the following code:

fancyStuff("myCode"); "I like".isGreat();

If I run it, I would expect an output of:

myCode
I like

However, if I wrapped it in quotes, I would get:

"fancyStuff("myCode"); "I like".isGreat();"

When this code is run, the expected output would be:

fancyStuff(
); 
.isGreat();

Obviously, the above example is not a functional response in any language. Your job is to write the code that performs in this way.

Rules

  • Standard loopholes apply.
  • The printed values, in both quoted and unquoted forms, must be non-empty, or consist solely of whitespace. This also means that all programs must include at least one set of quotes.
  • However, trailing/preceeding whitespace is allowed.
  • No looking at your own code, required file names, etc.
  • Unmatched quotes are disallowed
  • If there are multiple strings, they can either be printed as newlines (as in the example), or in some other human-readable way - no arrays or objects
  • This is , so shortest answer in bytes wins.

Geza Kerecsenyi

Posted 2019-06-20T15:17:21.123

Reputation: 1 892

Are unmatched quotes allowed, and if so, how should they be handled? – negative seven – 2019-06-20T15:24:42.797

Must the output be split with newlines like in the examples? – Erik the Outgolfer – 2019-06-20T15:27:48.633

@negativeseven no, I'll say they're not allowed. – Geza Kerecsenyi – 2019-06-20T15:29:07.803

@ErikTheOutgolfer I've updated the rules a bit, see now. – Geza Kerecsenyi – 2019-06-20T15:32:01.527

3@GezaKerecsenyi So, is a separator required, or can we simply concatenate the strings? – Erik the Outgolfer – 2019-06-20T15:32:41.127

@EriktheOutgolfer No separator is required - the only condition is that the output be a string format. – Geza Kerecsenyi – 2019-06-20T15:36:53.013

Let's say my program starts or ends with a double quote, does the program wrapped in double quotes has to output an empty leading/trailing item, or are they optional? – Kevin Cruijssen – 2019-06-20T15:41:45.953

@KevinCruijssen If you decide to separate quotes with newlines, yes, an empty line is required. But if you concatenate them, it won't make a different anyway. – Geza Kerecsenyi – 2019-06-20T15:43:21.357

Can we use single quotes or backticks instead of double quotes? – Shaggy – 2019-06-20T19:36:52.737

@Shaggy you can use them in your code, but for purposes of standardisation, the 'strings' that need to be printed should be in double quotes. – Geza Kerecsenyi – 2019-06-20T19:45:38.437

9How is an array not a human readable format? – Post Rock Garf Hunter – 2019-06-20T20:02:13.407

@SriotchilismO'Zaic It's not a string. Some consoles could even render it as [object] if you just try to print it. Further, arrays are made for data storage; strings are made for no reason other than human readability, so they are more primarily for that purpose. I'm only counting strings/integers/floats (but in this case just strings are relevant) as human-readable. – Geza Kerecsenyi – 2019-06-20T20:32:20.160

4Must strings be output in the same order they appear in our code? – Shaggy – 2019-06-20T20:55:36.020

1I think this would have been slightly more complex if you had to print whatever was not in the quotes – Jo King – 2019-06-21T07:05:11.837

@JoKing good idea - could I make a new challenge (like part 2) about that? – Geza Kerecsenyi – 2019-06-21T07:09:48.170

@shaggy yes, they do. – Geza Kerecsenyi – 2019-06-21T07:10:20.950

Answers

28

Python 2, 20 bytes

print";print'print'"

-7 bytes thanks to tsh

Try it online!


Old answer:

Python 2, 27 bytes

'';print";print 2*"'';print

Try it online!

Train of thought behind this answer:

Begin with a simple print, because we need to print something.

print"a"

We also need to print something in the inverted case, ie. have a print inside quotes.

print"print"

The non-inverted case is pretty good at this point. Let's focus on the inverted case. We now start with the string print, which can't be followed immediately by a print statement. Let's fix this with a semicolon.

print";print"

Good stuff. Except, the inverted code doesn't actually print anything. We'll need to print the print at the start, because it ends up in quotes, but also print whatever comes after the second quote, because it ends up in quotes too. The obvious way around this is to append print and multiply the last string by 2.

print";print 2*"print

Now the inverted code works fine, though we have to be wary of the fact that the section before the first quote and the section after the second quote need to be kept the same throughout future changes. As for the non-inverted code, it throws a syntax error - once again, we need to introduce a semicolon to separate expressions.

;print";print 2*";print

Python doesn't really like the look of that lone semicolon, so we must satisfy the snake's hunger with two of the same no-op expression, inserted before the first semicolon and the last semicolon. Most expressions will work fine in the first case, but in the second case it must follow print";print 2*" in the non-inverted code without breaking anything. We can use '', which simply gets concatenated with the prior string.

'';print";print 2*"'';print

negative seven

Posted 2019-06-20T15:17:21.123

Reputation: 1 931

5print(";print('print()');") is also 27 bytes, and also Python 3. – tsh – 2019-06-21T03:05:43.260

2@tsh This code reveals a 20-byte Python 2 solution that should have been obvious in hindsight, very nice! – negative seven – 2019-06-21T05:29:35.493

19

CSS, 66 bytes

body:after{content:"{}body:after{content:'body:after{content:}'}"}

"body:after{content:"{}body:after{content:'body:after{content:}'}"}"

Not so much questions may be solved by CSS...

tsh

Posted 2019-06-20T15:17:21.123

Reputation: 13 072

Seems that body:after also works on some implementations? – Shieru Asakoto – 2019-06-21T04:09:24.783

@ShieruAsakoto You are right. :after is available in most browsers for backward compatibility reason – tsh – 2019-06-21T04:37:37.700

@Shaggy But it's hard to define how "wrap your source code with quotes" works when both HTML and CSS provided. – tsh – 2019-06-21T09:46:16.730

@tsh It's fine - I didn't specify about multiple files in the rules, since I wasn't expecting such a unique answer! – Geza Kerecsenyi – 2019-06-21T23:41:52.383

10

HQ9+[see notes below], 1016 bytes

"Hello World"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""Hello World

Use the implementation on https://esolangs.org/w/index.php?title=HQ9%2B&oldid=59995 and compile the interpreter with MinGW GCC 5.3.0 on Windows. I'm not sure if it works with other version of compiler, since an undefined behavior of C is required to terminate the program. The buffer is 1000 bytes long. And source code greater than 1000 bytes do the trick. I'm not sure how these happened.

tsh

Posted 2019-06-20T15:17:21.123

Reputation: 13 072

7Maybe this is the longest HQ9+ answer on this site. (?) – tsh – 2019-06-21T06:24:43.493

1"an undefined behavior of C is required to terminate the program" Undefined behavior is undefined: it can do anything. – Solomon Ucko – 2019-06-22T22:46:51.533

According to page 4 of the C18 standard: "Possible undefined behavior ranges from ignoring the situation completely with unpredictable results,to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message)."

– Solomon Ucko – 2019-06-22T22:47:12.087

@SolomonUcko But we define a language by its compiler / interpreter on this site. And the answer is fine as long as at least one compiler / interpreter may produce the correct result. – tsh – 2019-06-23T09:09:42.203

9

05AB1E, 4 bytes

"A"§

Outputs concatenated without separator.

Try it online or try it online with surrounding quotes.

Explanation:

        # Program without surrounding quotes will output string "A"
"A"     # Push "A" to the stack
   §    # Cast it to a string
        # (output the top of the stack implicitly as result)

        # Program with surrounding quotes will output string "§"
""      # Push an empty string to the stack
  A     # Push the alphabet to the stack: "abcdefghijklmnopqrstuvwxyz"
   "§"  # Push "§" to the stack
        # (output the top of the stack implicitly as result)

Kevin Cruijssen

Posted 2019-06-20T15:17:21.123

Reputation: 67 575

Unfortunately, this violates rule #2. While it is fine to have some empty quotes in your code, there must be at least one non-empty one in both surrounded and unsurrounded forms. – Geza Kerecsenyi – 2019-06-20T15:47:05.633

@GezaKerecsenyi Oops, read past the part "in both quoted and unquoted forms". Should be fixed now. – Kevin Cruijssen – 2019-06-20T16:07:18.963

6

Japt, 4 bytes

"P"s

Try it unquoted or quoted

P is the Japt variable for the empty string and the s method slices a string - without any arguments, it does nothing.


Or, ever so slightly less trivial:

"+"u

Try it unquoted or quoted

The first one uppercases + and the second one appends u to an empty string.

Shaggy

Posted 2019-06-20T15:17:21.123

Reputation: 24 623

3Note that 4 is the minimum score for this question, since any less violates the restrictions. – Jo King – 2019-06-21T01:31:02.777

5

C# (Visual C# Interactive Compiler), 113 112 107 70 64 bytes

Write(".f();static void f(this string s){Write(s+')'+';');}//");

Saved 5 bytes thanks to @negative seven

Unquoted and Quoted

After a while, I realized that my solution was too complicated. The newest program shown here simply hides the rest of the program in a comment to avoid errors when wrapped in quotes.

When wrapped in quotes, Write( is passed onto an extension method, which prints it along with );.

Embodiment of Ignorance

Posted 2019-06-20T15:17:21.123

Reputation: 7 014

Split() -> Trim(), and var a -> _ – negative seven – 2019-06-22T20:54:42.470

1@negativeseven Thanks, I would of never thought of using a discard! – Embodiment of Ignorance – 2019-06-23T03:49:16.973

1@negativeseven Actually, my solution didn't even need to be so complicated, look at my newest edit – Embodiment of Ignorance – 2019-06-23T03:55:53.693

64 bytes using an extension method. Pretty surprised this worked out so well! – negative seven – 2019-06-23T07:01:23.250

1@negativeseven Thanks! And you gave me an idea for part two of the challenge: I was capturing the outside in a variable and then using Remove and Insert on it, now I can just use an extension method! – Embodiment of Ignorance – 2019-06-23T08:58:01.443

4

Perl 6, 11 bytes

say ".say~"

Try it online!

Prints .say~ with a trailing newline. Seems too easy. Am I missing something?

When wrapped in quotes, produces say with a space and trailing newline.

Jo King

Posted 2019-06-20T15:17:21.123

Reputation: 38 234

2I don't think so. It's just that p6 let's you say (ha) say $foo and $foo.say which makes it a lot easier. – user0721090601 – 2019-06-21T06:20:45.033

4

Foo, 4 bytes

"P"s

Try it online! Also works in Japt.

5 bytes (UTF-8)

"A"§

Try it online! Also works in 05AB1E.

9 bytes

"!""$;"$;

Try it online! Also works in Runic Enchantments.

11 bytes

say ".say~"

Try it online! Also works in Perl 6.

20 bytes

print";print'print'"

Try it online! Also works in Python 2.

69 bytes

body::after{content:"{}body::after{content:'body::after{content:}'}"}

Try it online! Also works in CSS.

Hmm... Foo is a highly adaptable language.

jimmy23013

Posted 2019-06-20T15:17:21.123

Reputation: 34 042

"Hmm... Foo is a highly adaptable language." For anyone who don't know Foo: everything within double-quotes is output and everything else (except for a few other builtin-characters) are no-ops. "The perfect language for the job" is an understatement here. ;) – Kevin Cruijssen – 2019-06-21T09:38:27.323

2So, basically, just rip off every other solution to this challenge?! – Shaggy – 2019-06-21T22:32:49.640

@Shaggy The language is Foo. Almost everything with at least a pair of quotes and both expected output non-empty works in Foo in this challenge. So "a"b 1"2" also works in Foo. The only answer in this challenge that is not a Foo polyglot so far is in HQ9+, because the Foo interpreter also has the overflowing problem. – jimmy23013 – 2019-06-22T03:52:05.050

4

><>, 18 9 bytes

"|o<"r>o|

-9 bytes thanks to Jo King

Try it online! (quoted)

Explanation

"|o<"r>o|
"|o<"     Pushes the quoted characters onto the stack
     r    Reverses the stack
      >o| Outputs all characters on stack & errors
""|o<"r>o|"
""          No-op
  |         Reverses the IP direction
     "r>o|" Pushes the quoted characters onto the stack (backwards)
  |o<       Outputs all characters on stack & errors

tjjfvi

Posted 2019-06-20T15:17:21.123

Reputation: 489

5Welcome to Code Golf! – Stephen – 2019-06-22T21:24:22.110

@Stephen Thanks! – tjjfvi – 2019-06-22T22:22:46.513

3

Befunge-98 (FBBI), 12 bytes

<@,k4"<@,k4"

Unquoted Quoted

Both cases print <@,k4. Either (or both) of the @s can be replaced with q instead.

attinat

Posted 2019-06-20T15:17:21.123

Reputation: 3 495

2

Runic Enchantments, 9 bytes

"!""$;"$;

Try it online! and ""!""$;"$;"

From Kevin Cruijssen, who essentially fixed my first attempt utilizing what I did in my second.

Going down the "fungoids never have unmatched quotes" rule-bending "there's something about this that shouldn't be OK" route, alluded to in my own comment:

7 bytes

0".""$;

Try it online! and "0".""$;"

Under normal circumstances, this program executes as 0".""$;0".""$; pushing an integer 0, then the string ., concatenates $;0, NOP, concatenates an empty string, prints top-of-stack (the string .$;0) and terminates. Wrapping it in quotes produces "0".""$;" which pushes a string-0, NOPs, concatenates an empty string, prints top-of-stack, and terminates (rendering the previously un-printed integer 0 in string form). The last " is left unexecuted (and not part of the original program anyway).

Fungoids don't have string literals, they have a command that toggles "read own source as a string" mode and some form of "instruction pointer has reached the source boundary" rule (usually edge-wrap), so the same source-code-positional-byte acts as both "begin string" and "end string" instruction, creating a string literal of that entire row/column (excluding the " itself).

Draco18s no longer trusts SE

Posted 2019-06-20T15:17:21.123

Reputation: 3 053

Super quick answer! I'm just wondering if I misunderstood something about your submission, but for me it doesn't print the $; at the end in the quoted version. – Geza Kerecsenyi – 2019-06-20T15:48:16.307

I realized that upon re-reading and am trying to puzzle out if runic can ever execute that bit. Starting to investigate "unpaired" quotes now; e.g "$; and ""$;" (Runic wrap-around quotes pairing with themselves).

– Draco18s no longer trusts SE – 2019-06-20T15:50:12.493

@GezaKerecsenyi Let me know if my updated program violates any rules. – Draco18s no longer trusts SE – 2019-06-20T16:13:01.430

Umm.. How is this valid? Your program without quotes output !. (which is correct), but shouldn't your program with quotes output 0$;? PS: I don't know Runic Enchantments at all, but a potential fix based on your current layout which I think is valid would be 0"!""$;"$; (which outputs !$; as is, or outputs 0$; if surrounded with quotes). EDIT: Actually, I think you can drop the 0 and output !$; and $;.

– Kevin Cruijssen – 2019-06-20T16:27:44.587

@KevinCruijssen I think that might've been what I'd been trying to do originally and fell down a rabbit hole somewhere. Post it as your own answer dude, you earned it. – Draco18s no longer trusts SE – 2019-06-20T16:36:15.277

@Draco18s Nah, I don't know Runic Enchantment at all. As I said, I just looked at your answer and made an easy fix of replacing the . with the $; (which I assume is join stack and print, or just print all or something along those lines?) So you can edit your answer to "!""$;"$;, which is still 9 bytes (feel free to credit me in the answer if you'd like :) ). – Kevin Cruijssen – 2019-06-20T16:38:01.937

1@KevinCruijssen $ is "print top of stack" and ; terminates. But you essentially got a valid answer. – Draco18s no longer trusts SE – 2019-06-20T16:53:02.877

@draco18s the current, 9-byte solution is perfectly valid, thanks for checking. – Geza Kerecsenyi – 2019-06-20T17:09:38.280

2

Haskell, 31 bytes

putStr"#1;(#)=const<$>putStr--"

Try it online! Or enclosed in quotes: Try it online!

nimi

Posted 2019-06-20T15:17:21.123

Reputation: 34 639

1

MathGolf, 4 bytes

";"q

Try it online!

The ; and q can be exchanged for a lot of different commands, including no-ops.

Jo King

Posted 2019-06-20T15:17:21.123

Reputation: 38 234

1

Ruby, 8 bytes

p";p'p'"

Try it online!

Wraps output in quotes, which may be illegal.

Ruby, 17 bytes

puts";puts'puts'"

Try it online!

MegaTom

Posted 2019-06-20T15:17:21.123

Reputation: 3 787

0

JavaScript (SpiderMonkey), 25 bytes

print("-print`print()`-")

Try it online!

"print("-print`print()`-")"

Try it online!

Trivial but functional.

tsh

Posted 2019-06-20T15:17:21.123

Reputation: 13 072

0

Japt, 4 bytes

"P"u

Unquoted, it converts the string P to uppercase. Quoted, it prints u.

Try it

Japt, 4 bytes

"P"w

Unquoted, it reverses the string P. Quoted, it prints w.

Try it

Embodiment of Ignorance

Posted 2019-06-20T15:17:21.123

Reputation: 7 014

Ahem! – Shaggy – 2019-06-21T22:34:11.443

@Shaggy Our solutions are a bit different, there are many different solutions to this challenge – Embodiment of Ignorance – 2019-06-22T02:21:21.950

Use Q instead of P, it's more meta! ;) – Shaggy – 2019-06-22T23:26:58.550

0

R, 16 bytes

 ";print(";");" 

Note that the above code is not wrapped in the additional quotation marks and has both leading and trailing spaces.

Try it (non-wrapped version)

James Otto

Posted 2019-06-20T15:17:21.123

Reputation: 101

You may want to add a note that the linked program is the non-wrapped version. – Jonathan Frech – 2019-06-21T21:59:09.220

I would also add a note about the leading and trailing spaces. It's hard to notice them otherwise. – mbomb007 – 2019-06-21T22:03:23.313

Edited, thanks for the suggestions! – James Otto – 2019-06-22T05:26:30.577

0

AppleScript, 9 bytes

return"&"

Explained:

return"&"    -- returns "&"

Quoted:

"return"&""  -- implied return of the string "return" concatenated with ""

a stone arachnid

Posted 2019-06-20T15:17:21.123

Reputation: 1 053