41
6
Write a program with the following properties:
When run as-is, the program produces no output (i.e. 0 bytes of output).
There is a location within the program (of your choice: it could be at the start, end, or somewhere in the middle) with the following property: modifying the program via placing any string there will cause the resulting program to print that string when executed.
This must work regardless of whether the string contains quotes, backslashes, comment marks, delimiters, NUL bytes, etc.; no matter what you place there, the string is still interpreted as a string and printed entirely verbatim. You can, however, fail to handle very very long strings if they would cause the compiler to run out of memory, or the like (to be precise, you should at least be able to handle strings up to 1000 bytes long or three times the length of your program, whichever is longer).
An example of an invalid solution would be
print("");
# ^ text goes here
in Python, Perl, Ruby, etc.; although it works for many strings, it will not work for a string containing a double quote, or a string containing the substring \n
(which would be interpreted as a newline).
Note that this problem is probably impossible in most languages; the challenge is at least partially about finding a language where it works. Your chosen language must be a programming language under this site's definition, e.g. no submitting a solution in Text.
As this is a code-golf, the shortest program template wins. However, do not be discouraged from submitting solutions even if they can't beat the current winner! You can still compete for second, third, etc. place, or simply to find as many answers where it works as possible. You should, however, ensure that your program meets the entire specification before submitting it; approximate solutions would miss the point of the problem.
The relevant Sandbox post is here.
– None – 2016-11-24T00:46:17.003Can you clarify the range of characters we need to support for the inserted string? ASCII? ASCII + unprintable? All of unicode? – James – 2016-11-24T00:53:26.797
3All 256 octets. Whether you interpret those as bytes or Unicode is up to you; it won't make much difference when it's printed. – None – 2016-11-24T00:54:25.010
When outputting the string, does it have to be only the string that's output or can there be extra stuff (along the lines of
this is the string: '<string_here>'
)? – milk – 2016-11-24T01:01:18.6501The intent of the question (and the way it's currently worded) is that you can't have any additional output. Did you have a solution in mind that can't be adapted to avoid it? (Using extra bytes to avoid stray output is preferable to creating stray output and not complying with the spec.) – None – 2016-11-24T01:04:40.643
My idea was to write a program that doesn't print anything, but adding any string makes it output
syntax error: '<string_here>'
when run. ;p I guess I'll try to think of something else... – milk – 2016-11-24T01:06:30.677I suspect that doesn't work because syntax errors tend to stop at newlines rather than printing the whole of the rest of the program source. Interesting idea, though. – None – 2016-11-24T01:08:51.827
Newlines are allowed in ruby strings, and I think python strings as well. Just to be pedantic. – Conor O'Brien – 2016-11-24T02:24:56.897
That's why I didn't specify newline as a character that would break. Rather, the two characters backslash,
n
break in the example because they're incorrectly interpreted as a newline. – None – 2016-11-24T02:40:37.423@jimmy23013 Well, I don't know. Can you prove that it's impossible? It seems impossible, but I'm not 100% sure. (If replacements weren't done in a loop, it would be as simple as
//\\/
.) If you're sure it's impossible then we might as well delete the comments. – Martin Ender – 2016-11-24T10:29:19.047@MartinEnder Let S be the first matched string, A be a character different from the first and last character of S. A len(S) times cannot be changed by the first replacement. Let X be A len(S) times concatenated with S, and Y be the result of the first replacement applied to X. Y cannot contain S, so applying the first replacement to it yield itself too. Prefixing a string won't make a difference either, because of the A len(S) times part. X and Y would be indistinguishable afterwards. – jimmy23013 – 2016-11-24T10:51:18.150
If the string will never have either of these characters
– Brad Gilbert b2gills – 2016-11-24T20:43:34.197「」
that will be as simple asprint 「」
in Perl 6.Do trailing newlines count? – user8397947 – 2016-11-25T15:51:19.863
You should aim to reproduce the trailing newlines of the strings. You're trying to make a byte-by-byte copy of them. – None – 2016-11-25T16:23:48.050
1If this didn't require a code change, it'd be trivial in
AWK
, just1
would do it. – Robert Benson – 2017-05-04T21:03:11.470