6
\$\begingroup\$

Forgetting quotes can lead to certain problems. For example, in Scala the two expressions:

"("+("+")+(")+(")+")" ("+("+")+(")+(")+") 

differ only in starting and ending quotes, but they respectively yield the result:

(+)+() +()+()+ 

But this expression in scala almost has the desired property:

def m="+m+" ""+m+"" 

Your task is to write the shortest expression surrounded with quotes, such that if we remove the outer quotes, the expression still returns the same string.

Key points:

  • Your language must support "..." as string literals
  • The program has to contain at least three characters
  • You are not allowed to define a context outside of the quotes themselves (as I did for m)
  • You are not allowed to use other delimiters for strings, e.g. no """, no '.
  • Both expressions should return strings.
  • Shortest code wins (counting the quotes themselves)
\$\endgroup\$
11
  • 2
    \$\begingroup\$ return or output? can we do either? Also, how is "quine" related to this? \$\endgroup\$ Commented Jan 9, 2014 at 17:09
  • \$\begingroup\$ This problem isn't even a little quineish. \$\endgroup\$ Commented Jan 9, 2014 at 18:34
  • \$\begingroup\$ @boothby But now it's a completely different question. \$\endgroup\$ Commented Jan 9, 2014 at 18:40
  • \$\begingroup\$ @Howard I changed only the title to reflect the content of the question. \$\endgroup\$ Commented Jan 9, 2014 at 19:08
  • \$\begingroup\$ @Quincunx: Both return or output, but it should be a string. I added a line to describe it. The relation with quines is that solutions somehow encode a part of the program in a string. Ok I agree it's more a far-distant cousin relationship. \$\endgroup\$ Commented Jan 10, 2014 at 6:47

15 Answers 15

15
\$\begingroup\$

C (4 chars)

The expression

"""" 

satisfies the minimal requirement of three characters. It encodes the empty string, no matter even if you omit one level of quotes:

 "" 

If you want the resulting string to be at least three characters, here is an expression of length 10 which results in three characters of string, namely three spaces:

"" " " " " 

If you don't like spaces either, here are four printable characters in a length 12 expression:

""/**/"/**/" "/**/"/**/ 

Putting these expressions into a small program:

#include <stdio.h> #define CASE(s) printf("'%s'\n", s) int main() { CASE(""""); CASE(""); CASE("" " "); CASE(" " ); CASE(""/**/"/**/"); CASE("/**/"/**/); } 

Output:

'' '' ' ' ' ' '/**/' '/**/' 
\$\endgroup\$
4
\$\begingroup\$

Shell (3 chars)

"w" w 

Both execute the command w.

\$\endgroup\$
5
  • 3
    \$\begingroup\$ Seriously?!? How many seconds of my life have been wasted typing who? \$\endgroup\$ Commented Jan 10, 2014 at 6:49
  • 1
    \$\begingroup\$ The question doesn't ask you to execute a command, it asks you to return a string. \$\endgroup\$ Commented Jan 10, 2014 at 10:47
  • 1
    \$\begingroup\$ @PeterTaylor: The way I read it, it asks for an expression which can be interpreted in two ways with the same meaning either way. So the expression above will do that. It is when you type that expression as the sole argument on an otherwise empty command line that the expression gets interpreted as a command name and executed. You could just as well use that expression as an argument to something else, e.g. to echo. \$\endgroup\$ Commented Jan 10, 2014 at 11:00
  • \$\begingroup\$ @boothby: assuming your typing speed is the same as mine, about 0.15 seconds per invocation of w. \$\endgroup\$ Commented Jan 10, 2014 at 16:49
  • \$\begingroup\$ I updated the question: "Both programs should return strings." \$\endgroup\$ Commented Jan 11, 2014 at 12:02
4
\$\begingroup\$

JavaScript, 28 27 15 12 (6 if result can be non-string (still works with output))

This one's pretty interesting.

"+"*1+"+"+"" 

The basic explanation is that "string" * 1 is NaN, and +"string" is also NaN. Therefore, it results in the string "NaN+" with or without the outside quotes.

For further explanation, it gets parsed as ("+" * 1) + ("+") + ("") with the outside quotes, and (+ "*1+") + "+" without.

The version that works if the output can be non-string:

"+"*"" 

Which does work when outputting (as JavaScript is weakly typed):

alert("+"*"") // NaN alert(+"*") // NaN 

Old version:

"+".replace(/./,NaN)+"+"+"" 

With the quotes outside, it is parsed as

("+".replace(/./, NaN)) + ("+") + ("") "NaN" + "+" + "" "NaN+" 

Without the quotes, it gets parsed as

(+".replace(/./,NaN)+") + ("+") // +"any string" evaluates to NaN NaN + "+" "NaN+" 
\$\endgroup\$
3
\$\begingroup\$

Perl - 4 bytes

"$~" 

Test program:

print"$~"; print$~; 

Ouput:

STDOUT STDOUT 
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Also works with many other single-character special variables in Perl. Notably,$0, $;, $/, $: and $^ also have a non-empty string as their default value. (So does $", but "$"" is a syntax error.) \$\endgroup\$ Commented Apr 5, 2015 at 11:49
3
\$\begingroup\$

JavaScript, 40

"(f=function (){return'(f='+f+')()'})()" 

When you remove the quotes, it becomes an immediately invoked function expression, and this function happens to return '(f='+f+')()', which is the entirety of the string. (Absuing Function#toString. ;))

\$\endgroup\$
3
  • \$\begingroup\$ Nice usage of quine. Could you do the same without using secondary quotes? \$\endgroup\$ Commented Jan 10, 2014 at 14:00
  • \$\begingroup\$ @Mikaël Probably not, since I can't have double quotes inside double quotes :-P \$\endgroup\$ Commented Jan 10, 2014 at 14:01
  • \$\begingroup\$ @MikaëlMayer I made a version (completely different one) without secondary quotes! :D codegolf.stackexchange.com/a/18188/3808 \$\endgroup\$ Commented Jan 10, 2014 at 14:14
3
\$\begingroup\$

huh?, 7

"Ouch!" 

It works because both "Ouch!" and Ouch! are both invalid files, so the interpreter will return Ouch!

\$\endgroup\$
3
\$\begingroup\$

Update: Python (12 chars)

program

""+"+""+"+"" "+"+""+"+" 

both giving

'++' 

Python (28 chars)

program

"".__str__()+".__str__()+""" ".__str__()+".__str__()+"" 

both giving

'.__str__()+' 

Python (38 chars)

program

"".replace("+","+")+".replace("+","")" ".replace("+","+")+".replace("+","") 

both giving

'.replace(,)' 
\$\endgroup\$
2
  • \$\begingroup\$ so in python, ","")" evaluates to ",)" ? interesting. \$\endgroup\$ Commented Jan 10, 2014 at 12:12
  • \$\begingroup\$ yes that was also new to me, it also allows for a more compact solution (see edit). \$\endgroup\$ Commented Jan 10, 2014 at 13:40
2
\$\begingroup\$

Befunge 98 - 3, 4, 6, 17

In these programs, I'll assume you just want to get a string, but you don't care if the program terminates. In other words, you want a code snippet that can be placed in the code somewhere. (Note: in Befunge, whatever is on the stack is a string. It is also a number)

This code snippet must be placed on the first part of the first line.

"g" 

When the quotes are removed, g acts like 00g, so it pushes a g. Either way, it pushes a g.

"""" 

Works in Befunge 93 as well; it always pushes nothing.

This one is more interesting

""z"z" 

It will always push z. z is a no-op so although the IP executes the command, it does nothing (I could have used , but z is more interesting).

This one only has one set of ", and is quine-like:

">:0g\1+:f`1+jb<" 

It always pushes the contents of the string, but when the quotes are removed, it does it in a quine-like way. This must be placed on the first part of the first line, but adjustments can be made to put it elsewhere

\$\endgroup\$
2
  • \$\begingroup\$ I updated the question so that it removes the first case: "Both programs should return strings." \$\endgroup\$ Commented Jan 11, 2014 at 12:03
  • \$\begingroup\$ @MikaëlMayer No, the first case is not removed. Both programs "g" and g return strings, one is g, and the other is g. \$\endgroup\$ Commented Jan 12, 2014 at 0:58
2
\$\begingroup\$

PowerShell, 4 chars

"$?" $? 

Output:

TRUE TRUE 

This uses the automatic variable $?, which is a Boolean containing the execution status of the last run command. The only effect of wrapping it in quotes is that it is converted to a string prior to being printed, which is a transparent operation.

\$\endgroup\$
1
  • \$\begingroup\$ I updated the question: "Both programs should return strings." \$\endgroup\$ Commented Jan 11, 2014 at 12:03
2
\$\begingroup\$

GolfScript: 16 chars

OK, since my 3-char solution "1" has been disqualified by a rules change, here's the straightforward solution in GolfScript:

"{`[46 126]+}.~" 

With the quotes, this is just a double-quoted string literal. Without the quotes, it's a quine. The only slightly trickly part is that, to literally comply with the rule prohibiting alternative string delimiters, I have to encode the string '.~' as an array of ASCII codes [46 126] instead.

\$\endgroup\$
1
  • \$\begingroup\$ I updated the question: "Both programs should return strings." \$\endgroup\$ Commented Jan 11, 2014 at 12:03
1
\$\begingroup\$

Scala REPL (10 chars)

"0+"++"+0" 0+"++"+0 

are both returning:

0++0 0++0 

Explanation: In the first case ++ treats the left string as a sequence of chars and concatenates the next sequence of chars and rebuilds a string. In the second case, + converts the first and last 0 to strings.

Note that everything is even a palindrom.

Other interesting solution using blocks (55 chars)

"{val o="*{val o=0;o}+".substring(9,12);val i="*0+";o}" {val o="*{val o=0;o}+".substring(9,12);val i="*0+";o} 

return both smileys:

 ;o} ;o} 

Explanation: See the below decomposition

"{val o="*{val o=0;o}+".substring(9,12);val i="*0+";o}" --multiplied by 0=>"" --------- returns "" ----- result {val o="*{val o=0;o}+".substring(9,12);val i="*0+";o} -------- o equals ";o}"-------- -i ignored- o 
\$\endgroup\$
1
\$\begingroup\$

Python 3.8 (pre-release), 11 9 7 bytes

""#"*0" 

Try it online!

The expression with quotes is ""#"*0" which is just "" as the comment is ignored. The expression without quotes is "#"*0 which is the empty string by product.

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

Python (8 characters)

"""""""" 

and

"""""" 

both return empty string
Is based on idea of MvG

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

Tcl, 1, 3

1 "1" 

Try it online!

Explanation: In Tcl, "quoting is a tool, not a rule"

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

TI-BASIC, 6 bytes

""→Str1:" "→Str1: 

Both of these end up storing an empty string to Ans (and to Str1). The second works because storing to a variable also stores to Ans.

I also found another 6-byter:

"":":" ":": 

Both of these will return ":".

I have checked every shorter chain matching "[":]{0-3}", and none meet the specifications.

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.