Inverse-quotes-quine

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.
• Are unmatched quotes allowed, and if so, how should they be handled? – negative seven Jun 20 at 15:24
• @GezaKerecsenyi So, is a separator required, or can we simply concatenate the strings? – Erik the Outgolfer Jun 20 at 15:32
• How is an array not a human readable format? – Sriotchilism O'Zaic Jun 20 at 20:02
• Must strings be output in the same order they appear in our code? – Shaggy Jun 20 at 20:55
• I think this would have been slightly more complex if you had to print whatever was not in the quotes – Jo King Jun 21 at 7:05

Python 2, 20 bytes

print";print'print'"


-7 bytes thanks to tsh

Try it online!

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

• print(";print('print()');") is also 27 bytes, and also Python 3. – tsh Jun 21 at 3:05
• @tsh This code reveals a 20-byte Python 2 solution that should have been obvious in hindsight, very nice! – negative seven Jun 21 at 5:29

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

• Seems that body:after also works on some implementations? – Shieru Asakoto Jun 21 at 4:09
• @ShieruAsakoto You are right. :after is available in most browsers for backward compatibility reason – tsh Jun 21 at 4:37
• @Shaggy But it's hard to define how "wrap your source code with quotes" works when both HTML and CSS provided. – tsh Jun 21 at 9:46
• @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 Jun 21 at 23:41

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.

• Maybe this is the longest HQ9+ answer on this site. (?) – tsh Jun 21 at 6:24
• "an undefined behavior of C is required to terminate the program" Undefined behavior is undefined: it can do anything. – Solomon Ucko Jun 22 at 22:46
• 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 Jun 22 at 22:47
• @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 Jun 23 at 9:09

05AB1E, 4 bytes

"A"§


Outputs concatenated without separator.

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)

• 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 Jun 20 at 15:47
• @GezaKerecsenyi Oops, read past the part "in both quoted and unquoted forms". Should be fixed now. – Kevin Cruijssen Jun 20 at 16:07

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.

• Note that 4 is the minimum score for this question, since any less violates the restrictions. – Jo King Jun 21 at 1:31

C# (Visual C# Interactive Compiler), 11311210770 64 bytes

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


Saved 5 bytes thanks to @negative seven

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

• Split() -> Trim(), and var a -> _ – negative seven Jun 22 at 20:54
• @negativeseven Thanks, I would of never thought of using a discard! – Embodiment of Ignorance Jun 23 at 3:49
• @negativeseven Actually, my solution didn't even need to be so complicated, look at my newest edit – Embodiment of Ignorance Jun 23 at 3:55
• 64 bytes using an extension method. Pretty surprised this worked out so well! – negative seven Jun 23 at 7:01
• @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 Jun 23 at 8:58

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.

• I 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. – guifa Jun 21 at 6:20

Foo, 4 bytes

"P"s


5 bytes (UTF-8)

"A"§


9 bytes

"!""$;"$;


11 bytes

say ".say~"


20 bytes

print";print'print'"


69 bytes

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


Hmm... Foo is a highly adaptable language.

• "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 Jun 21 at 9:38
• So, basically, just rip off every other solution to this challenge?! – Shaggy Jun 21 at 22:32
• @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 Jun 22 at 3:52

><>, 18 9 bytes

"|o<"r>o|


-9 bytes thanks to Jo King

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

• Welcome to Code Golf! – Stephen Jun 22 at 21:24
• @Stephen Thanks! – tjjfvi Jun 22 at 22:22

Befunge-98 (FBBI), 12 bytes

<@,k4"<@,k4"


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

Runic Enchantments, 9 bytes

"!""$;"$;


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

• 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 Jun 20 at 15:48 • 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 Jun 20 at 15:50 • @GezaKerecsenyi Let me know if my updated program violates any rules. – Draco18s Jun 20 at 16:13 • 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 Jun 20 at 16:27
• @KevinCruijssen $ is "print top of stack" and ; terminates. But you essentially got a valid answer. – Draco18s Jun 20 at 16:53 Haskell, 31 bytes putStr"#1;(#)=const<$>putStr--"


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

MathGolf, 4 bytes

";"q


Try it online!

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

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!

JavaScript (SpiderMonkey), 25 bytes

print("-printprint()-")


Try it online!

"print("-printprint()-")"


Try it online!

Trivial but functional.

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

• Ahem! – Shaggy Jun 21 at 22:34
• @Shaggy Our solutions are a bit different, there are many different solutions to this challenge – Embodiment of Ignorance Jun 22 at 2:21
• Use Q instead of P, it's more meta! ;) – Shaggy Jun 22 at 23:26

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)

• You may want to add a note that the linked program is the non-wrapped version. – Jonathan Frech Jun 21 at 21:59
• I would also add a note about the leading and trailing spaces. It's hard to notice them otherwise. – mbomb007 Jun 21 at 22:03
• Edited, thanks for the suggestions! – James Otto Jun 22 at 5:26

AppleScript, 9 bytes

return"&"


Explained:

return"&"    -- returns "&"


Quoted:

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