5
Description
There are an infinite number of ASCII strings. Write a program that will output every possible ASCII string exactly once.
The ordering does not matter, but you must be able to show that for any possible ASCII string s
, there exists an integer n
such that s
is the n
th string in the output. This restriction means that you cannot output 'a', 'aa', 'aaa', 'aaaa', ...
, since the string 'b'
will never occur.
The simplest ordering (shown here in terms of letters so I don't have to type unprintables) is:
'', 'a', 'b', 'c', ..., 'z', 'aa', 'ab', ..., 'az', 'ba', 'bb', 'bc', ... 'zz', 'aaa', ...
Essentially, this is counting in base 128 using ASCII characters.
Rules
- The empty string is required
- You may use any valid ordering you want
- All possible strings using ASCII must be known to occur in the output
- The ordering does not need to be deterministic, as long as you can show that every string will be guaranteed to occur. Be careful if you use randomness for each string, because at some point your bits of entropy may not be enough to guarantee a unique string.
Example orderings
Note that, again, I'm showing only letters for the examples, but you have to include all ASCII characters.
Valid:
'', 'a', 'b', 'c', ..., 'z', 'aa', 'ab', ..., 'az', 'ba', 'bb', 'bc', ... 'zz', 'aaa', ...
'', 'z', 'y', 'x', ..., 'a', 'zz', 'zy', ...
'', 'a', 'b', 'ab', 'ba', 'c', 'ac', 'ca', 'bc', 'cb', ...
Invalid:
'', 'a', 'aa', 'aaa', 'aaaa', 'aaaaa', ...
'', 'a', 'ab', 'abc', 'abcd', ..., 'abc...xyza', ...
This means only printable ascii right? – Maltysen – 2017-08-17T00:20:53.113
I actually intended this to be all ASCII. – mbomb007 – 2017-08-17T00:22:01.990
uhhh should I repr before printing or something? – Maltysen – 2017-08-17T00:23:05.957
It's up to you. Maybe for testing purposes. Defining a Python generator expression would also be acceptable. – mbomb007 – 2017-08-17T00:24:05.510
2Related, maybe dupe. In fact, I think a past challenge to output all ASCII strings was closed as a dupe. – xnor – 2017-08-17T00:31:45.263
*bijective base 128 – Destructible Lemon – 2017-08-17T00:32:24.937
Can we output the string in bytes (i.e. in Python
b'\x01'
) or must we print the actual character? – notjagan – 2017-08-17T00:48:25.2673It would have been better to use only printable ASCII. It's difficult to output non-printable chars and check they are the correct ones – Luis Mendo – 2017-08-17T01:23:12.667
@xnor Ah. Yeah maybe a dupe. I searched for a good while looking for potential dupes, and I couldn't find that challenge. – mbomb007 – 2017-08-17T03:02:48.653
And now find a program doing this by Gödel numbering :) – Felix Palmen – 2017-08-17T06:20:34.463