Skip to content

Instantly share code, notes, and snippets.

@knkillname
Last active April 7, 2019 05:15
Show Gist options
  • Save knkillname/89824d0d8d0f8b20aa01 to your computer and use it in GitHub Desktop.
Save knkillname/89824d0d8d0f8b20aa01 to your computer and use it in GitHub Desktop.
Generate all posible programs
# This program prints all the programs that can be written in Python
# (even this very same program if you give it enough time)
import string
def generate_programs(alphabet = string.printable):
alphabet = tuple(alphabet)
word, m, n = [], 0, len(alphabet)
while True:
p = ''.join(alphabet[i] for i in word)
try:
compile(p, '', 'exec')
yield p
except SyntaxError: pass
for j in range(m - 1, -1, -1):
word[j] = (word[j] + 1) % n
if word[j] != 0: break
else: word.append(0); m += 1
for (i, p) in enumerate(generate_programs()):
print('Program {}:\n{}'.format(i, p))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment