5
\$\begingroup\$

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 nth 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', ...
\$\endgroup\$
  • \$\begingroup\$ This means only printable ascii right? \$\endgroup\$ – Maltysen Aug 17 '17 at 0:20
  • \$\begingroup\$ I actually intended this to be all ASCII. \$\endgroup\$ – mbomb007 Aug 17 '17 at 0:22
  • \$\begingroup\$ uhhh should I repr before printing or something? \$\endgroup\$ – Maltysen Aug 17 '17 at 0:23
  • 2
    \$\begingroup\$ Related, maybe dupe. In fact, I think a past challenge to output all ASCII strings was closed as a dupe. \$\endgroup\$ – xnor Aug 17 '17 at 0:31
  • 3
    \$\begingroup\$ It would have been better to use only printable ASCII. It's difficult to output non-printable chars and check they are the correct ones \$\endgroup\$ – Luis Mendo Aug 17 '17 at 1:23
0
\$\begingroup\$

SOGL V0.12, 10 bytes

]¶tēN»─{Rt

Try it Here!

Note that this spams the stack with an extra item every iteration, and with how slow SOGL is, it gets out of hand quickly.

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

Python 2, 69 62 bytes

a=0
while 1:
 k=a
 while k:print chr(k%128),;k>>=7
 print;a+=1

-7 bytes thanks to @notjagan

Try it online!

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

Proton, 96 bytes

i=0
(p=print)()
while++i for j:cartesianpower(i,0..128)p(j is tuple? ''.join(map(chr,j)):chr(j))

Try it online!

\$\endgroup\$
  • \$\begingroup\$ I am sure there's some way to print an empty line that's shorter than (p=print)() but I can't find it if there is \$\endgroup\$ – Stephen Aug 17 '17 at 0:42
1
\$\begingroup\$

Pyth - 12 bytes

.Vj^sCMS127b

Finite version online.

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

Ruby, 64 bytes

(f=->a{f[a.flat_map{|s|puts s;(1..127).map{|b|s+b.chr}}]})[['']]

Recursive function that prints the array a then calls itself on a newly constructed array whose contents are (in set notation) { sc | s in a, c in ASCII }. We kick it off with an array containing the empty string.

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

Mathematica, 44 41 61 bytes

Crossed out 44 is still regular 44...

0//.i_:>(Print@@@Array[FromCharacterCode,128,0]~Tuples~i;i+1)

Prints in the pattern: "", "a", "b", ... , "z", "aa", "ab", ... , "az", "ba", "bb", ... , "zz", "aaa", "aab", ... (but with \00, \01, ...).

Try it on Wolfram Sandbox

Unfortunately, I could not use the built-in CharacterRange function (which would have reduced 10 bytes) because it cannot generate the null character.


I recommend testing with the version that pauses 0.25 sec between each Print calls:

0//.i_:>((Pause[0.25];Print@##)&@@@Array[FromCharacterCode,128,0]~Tuples~i;i+1)
\$\endgroup\$
0
\$\begingroup\$

Python 3, 103 78 bytes

n=0
while 1:print(n.to_bytes(n.bit_length()+7>>3,"big").decode());n+=n+1&128|1

Try it online!

\$\endgroup\$

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