How many contiguous "strings" can you find in a set of strings?

5

Consider the set of 16-character strings containing only the 95 printable ASCII characters. There are 95164.4×1031 such strings.

Imagine these strings are put into a list and sorted lexicographically (which is the way most languages will sort a list of strings). What is the largest contiguous slice of this list you can find such that every string is a valid program in your programming language of choice whose output is the sole word string?

All output should go to the same place for all programs; use stdout or a similar alternative. An optional trailing newline may be a part of the output, but otherwise nothing besides string should be printed for any of your 16-character programs. There is no input and all programs should run without erroring. The program does not have to terminate.

The list of length 16 strings does not "loop-around" from ~~~~~~~~~~~~~~~~ to                  (16 spaces).

Give your starting string and ending string and explain why every program-string that's in between them lexicographically outputs string.

The submission with the largest slice wins.

Calvin's Hobbies

Posted 2015-02-05T15:12:19.030

Reputation: 84 000

1Is it my imagination or is this challenge just "find the language that prints a string literal and can throw away the most additional characters"? – Sparr – 2015-12-06T22:16:43.577

Answers

8

///, 6634204313747905 > 958

From

string/.~~~~~/0 

to

string/0     /.~

This relies on the program not being required to terminate. I started from a core-range of 958 much like the other solutions, from

string//        

to

string//~~~~~~~~

However, in this case, // is not a comment. In /// everything that's not a slash is just printed (hence this always prints string before doing anything else). A slash then starts a substitution pattern of the form /pattern/replacement/. If only one or two / are found, then the program simply terminates. However, the above range obviously also includes strings with three or more slashes. The trick here is that the pattern to be searched for is empty, and substitutions are performed iteratively until the pattern cannot be found any more. Since the empty string can always be found, this substitution never terminates, and hence the program never gets around to print anything after the initial string.

But since this doesn't rely on // being a comment, we can actually go a bit further! E.g. the previous and next string around this range, respectively, are:

string/.~~~~~~~~
string/0        

These still don't contain a full substitution instruction so they also only print string and then terminate. Let's see how far we can go! I'll focus on the upper end of the range (the lower end works analogously). What if we get another /? The first time this happens is

string/0       /

But that's still not a valid replacement pattern, so all is well. It looks like we need three slashes to break the magic. The first time we get three slashes is at

string/0      //

What now? Well, the pattern to be searched for is 0 (and a bunch of spaces), to be replaced with an empty string. That pattern is never found, so the substitution completes immediately. But luckily, there is nothing to print afterwards in the source code, so the result is still only string. Neat. :)

So we need a valid substitution instruction and more source code afterwards to not print string:

string/0     // 

And this is indeed the first code that doesn't work to spec, as it prints string followed by a space. So the last code that works is

string/0     /.~

We can apply the same argument to the beginning of the range, the last failing code being

string/.~~~~~//~

such that the first code that works is

string/.~~~~~/0 

Martin Ender

Posted 2015-02-05T15:12:19.030

Reputation: 184 808

3

pyexpander: 95^8 = 6634204312890625

string$#        

to

string$#~~~~~~~~

Wow, it took forever to find the language I was looking for: one that outputs most source literals untouched and has a rest-of-line comment delimiter. This is still not theoretically optimal, as it's possible that another language like this exists, but with a one-character comment delimiter. However, I couldn't find one.

Runer112

Posted 2015-02-05T15:12:19.030

Reputation: 3 636

This is almost 100 times better than the other answers. xD – TheNumberOne – 2015-02-05T19:49:45.467

2

GolfScript: 95^7 = 69833729609375

"string"#       

to

"string"#~~~~~~~

Everything from the # is a comment; the literal string is echoed at the end of execution.

Peter Taylor

Posted 2015-02-05T15:12:19.030

Reputation: 41 901

I think your score is actually a bit higher. The code before your first one is "string""~~~~~~~ and as far as I know and unmatched " acts like a "supercomment" just as an unmatched } does. So I think your earliest valid code is "string""~~~~~~#. – Martin Ender – 2015-02-06T09:21:43.367

@MartinBüttner, no, the parser complains "Unterminated string meets end of file". – Peter Taylor – 2015-02-06T09:26:34.190

2

PHP, 95^7 = 69833729609375

string<?#

followed by arbitrary characters.

feersum

Posted 2015-02-05T15:12:19.030

Reputation: 29 566

+1 for using my pet language. Golfing in PHP is a real challenge :) – None – 2015-02-06T14:08:36.237

0

Windows Powershell: 95^7 = 69833729609375

"string"#

to

"string"#~~~~~~~

All characters from the # and following are part of a comment.

Same as @PeterTaylor's GolfScript answer

bmarks

Posted 2015-02-05T15:12:19.030

Reputation: 2 114

0

Microscript, 95^6=735091890625

"gnirts"ah (padded to 16 chars with trailing spaces) through "gnirts"ah~~~~~~

SuperJedi224

Posted 2015-02-05T15:12:19.030

Reputation: 11 342