31
\$\begingroup\$

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

18 Answers 18

28
\$\begingroup\$

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
\$\endgroup\$
2
  • 5
    \$\begingroup\$ print(";print('print()');") is also 27 bytes, and also Python 3. \$\endgroup\$
    – tsh
    Jun 21, 2019 at 3:05
  • 2
    \$\begingroup\$ @tsh This code reveals a 20-byte Python 2 solution that should have been obvious in hindsight, very nice! \$\endgroup\$ Jun 21, 2019 at 5:29
19
\$\begingroup\$

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

\$\endgroup\$
4
  • \$\begingroup\$ Seems that body:after also works on some implementations? \$\endgroup\$ Jun 21, 2019 at 4:09
  • \$\begingroup\$ @ShieruAsakoto You are right. :after is available in most browsers for backward compatibility reason \$\endgroup\$
    – tsh
    Jun 21, 2019 at 4:37
  • \$\begingroup\$ @Shaggy But it's hard to define how "wrap your source code with quotes" works when both HTML and CSS provided. \$\endgroup\$
    – tsh
    Jun 21, 2019 at 9:46
  • \$\begingroup\$ @tsh It's fine - I didn't specify about multiple files in the rules, since I wasn't expecting such a unique answer! \$\endgroup\$ Jun 21, 2019 at 23:41
10
\$\begingroup\$

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.

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

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)
\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$ Jun 20, 2019 at 15:47
  • \$\begingroup\$ @GezaKerecsenyi Oops, read past the part "in both quoted and unquoted forms". Should be fixed now. \$\endgroup\$ Jun 20, 2019 at 16:07
6
\$\begingroup\$

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.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ Note that 4 is the minimum score for this question, since any less violates the restrictions. \$\endgroup\$
    – Jo King
    Jun 21, 2019 at 1:31
5
\$\begingroup\$

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

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

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.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ Jun 21, 2019 at 6:20
4
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ "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. ;) \$\endgroup\$ Jun 21, 2019 at 9:38
  • 2
    \$\begingroup\$ So, basically, just rip off every other solution to this challenge?! \$\endgroup\$
    – Shaggy
    Jun 21, 2019 at 22:32
  • \$\begingroup\$ @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. \$\endgroup\$
    – jimmy23013
    Jun 22, 2019 at 3:52
4
\$\begingroup\$

><>, 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
\$\endgroup\$
1
  • 5
    \$\begingroup\$ Welcome to Code Golf! \$\endgroup\$
    – Stephen
    Jun 22, 2019 at 21:24
3
\$\begingroup\$

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.

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

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

\$\endgroup\$
8
  • \$\begingroup\$ 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. \$\endgroup\$ Jun 20, 2019 at 15:48
  • \$\begingroup\$ 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). \$\endgroup\$ Jun 20, 2019 at 15:50
  • \$\begingroup\$ @GezaKerecsenyi Let me know if my updated program violates any rules. \$\endgroup\$ Jun 20, 2019 at 16:13
  • \$\begingroup\$ 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 $;. \$\endgroup\$ Jun 20, 2019 at 16:27
  • 1
    \$\begingroup\$ @KevinCruijssen $ is "print top of stack" and ; terminates. But you essentially got a valid answer. \$\endgroup\$ Jun 20, 2019 at 16:53
2
\$\begingroup\$

Haskell, 31 bytes

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

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

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

MathGolf, 4 bytes

";"q

Try it online!

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

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

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!

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

JavaScript (SpiderMonkey), 25 bytes

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

Try it online!

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

Try it online!

Trivial but functional.

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

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

\$\endgroup\$
3
  • \$\begingroup\$ Ahem! \$\endgroup\$
    – Shaggy
    Jun 21, 2019 at 22:34
  • \$\begingroup\$ @Shaggy Our solutions are a bit different, there are many different solutions to this challenge \$\endgroup\$
    – Gymhgy
    Jun 22, 2019 at 2:21
  • \$\begingroup\$ Use Q instead of P, it's more meta! ;) \$\endgroup\$
    – Shaggy
    Jun 22, 2019 at 23:26
0
\$\begingroup\$

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)

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

AppleScript, 9 bytes

return"&"

Explained:

return"&"    -- returns "&"

Quoted:

"return"&""  -- implied return of the string "return" concatenated with ""
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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