Inverse-Quotes-Quine

29

Die Aussicht auf diese Herausforderung ist:

  • Wenn Ihr Programm normal ausgeführt wird, sollte der gesamte Code in den Sprachmarkierungen ( "- doppelte Anführungszeichen) gedruckt werden.
  • Wenn Ihr Programm in doppelte Anführungszeichen eingeschlossen ist (die Sprachmarkierungen werden umgekehrt), sollte der Code gedruckt werden, der normalerweise nicht in Anführungszeichen steht.

Z.B:

Angenommen, Sie haben den folgenden Code:

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

Wenn ich es laufen lasse, würde ich eine Ausgabe von erwarten:

myCode
I like

Wenn ich es jedoch in Anführungszeichen setze, erhalte ich:

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

Wenn dieser Code ausgeführt wird, wäre die erwartete Ausgabe:

fancyStuff(
); 
.isGreat();

Offensichtlich ist das obige Beispiel keine funktionale Antwort in irgendeiner Sprache. Ihre Aufgabe ist es, den Code zu schreiben, der auf diese Weise ausgeführt wird.

Regeln

  • Es gelten Standardlücken.
  • Die gedruckten Werte, sowohl in Anführungszeichen als auch in nicht Anführungszeichen, dürfen nicht leer sein oder nur aus Leerzeichen bestehen. Dies bedeutet auch, dass alle Programme mindestens einen Satz von Anführungszeichen enthalten müssen.
  • Es ist jedoch zulässig, Leerzeichen nach- oder voranzustellen.
  • Sie müssen sich nicht Ihren eigenen Code, die erforderlichen Dateinamen usw. ansehen.
  • Nicht übereinstimmende Angebote sind nicht zulässig
  • Wenn mehrere Zeichenfolgen vorhanden sind, können diese entweder als Zeilenumbrüche (wie im Beispiel) oder auf andere für den Menschen lesbare Weise gedruckt werden - ohne Arrays oder Objekte
  • Das ist , also gewinnt die kürzeste Antwort in Bytes.
Geza Kerecsenyi
quelle
Are unmatched quotes allowed, and if so, how should they be handled?
negative seven
3
@GezaKerecsenyi So, is a separator required, or can we simply concatenate the strings?
Erik the Outgolfer
9
How is an array not a human readable format?
Wheat Wizard
4
Must strings be output in the same order they appear in our code?
Shaggy
1
I think this would have been slightly more complex if you had to print whatever was not in the quotes
Jo King

Antworten:

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
quelle
5
print(";print('print()');") is also 27 bytes, and also Python 3.
tsh
2
@tsh This code reveals a 20-byte Python 2 solution that should have been obvious in hindsight, very nice!
negative seven
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
quelle
Seems that body:after also works on some implementations?
Shieru Asakoto
@ShieruAsakoto You are right. :after is available in most browsers for backward compatibility reason
tsh
@Shaggy But it's hard to define how "wrap your source code with quotes" works when both HTML and CSS provided.
tsh
@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
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
quelle
7
Maybe this is the longest HQ9+ answer on this site. (?)
tsh
1
"an undefined behavior of C is required to terminate the program" Undefined behavior is undefined: it can do anything.
Solomon Ucko
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
@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
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
quelle
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
@GezaKerecsenyi Oops, read past the part "in both quoted and unquoted forms". Should be fixed now.
Kevin Cruijssen
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
quelle
3
Note that 4 is the minimum score for this question, since any less violates the restrictions.
Jo King
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
quelle
Split() -> Trim(), and var a -> _
negative seven
1
@negativeseven Thanks, I would of never thought of using a discard!
Embodiment of Ignorance
1
@negativeseven Actually, my solution didn't even need to be so complicated, look at my newest edit
Embodiment of Ignorance
64 bytes using an extension method. Pretty surprised this worked out so well!
negative seven
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
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
quelle
2
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.
user0721090601
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
quelle
"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
2
So, basically, just rip off every other solution to this challenge?!
Shaggy
@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
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
quelle
5
Welcome to Code Golf!
Stephen
@Stephen Thanks!
tjjfvi
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
quelle
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
quelle
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
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
@GezaKerecsenyi Let me know if my updated program violates any rules.
Draco18s
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
1
@KevinCruijssen $ is "print top of stack" and ; terminates. But you essentially got a valid answer.
Draco18s
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
quelle
Ahem!
Shaggy
@Shaggy Our solutions are a bit different, there are many different solutions to this challenge
Embodiment of Ignorance
Use Q instead of P, it's more meta! ;)
Shaggy
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
quelle
You may want to add a note that the linked program is the non-wrapped version.
Jonathan Frech
I would also add a note about the leading and trailing spaces. It's hard to notice them otherwise.
mbomb007
Edited, thanks for the suggestions!
James Otto
0

AppleScript, 9 bytes

return"&"

Explained:

return"&"    -- returns "&"

Quoted:

"return"&""  -- implied return of the string "return" concatenated with ""
a stone arachnid
quelle