12
3
The proof of the set of characters is that there is a translator which takes any ascii python program as input and produces a limited character set version of the program. The limited character set program can be slower than the original program. It simply has to be possible to produce the same output as the valid python program.
For example, here is a translator that takes any ascii python program as input and writes out a version that doesn't use the character x:
import random
import string
with open('in.py') as infile:
with open('out.py', 'w') as outfile:
lines = infile.readlines()
outfile.write("data = '")
random_string = ''.join(random.choice(string.digits) for x in range(20))
for line in lines:
outfile.write(line.encode('string-escape').replace('x', random_string))
outfile.write("'\n")
outfile.write("exec data.replace('%s', chr(%s))" % (random_string, ord('x')))
note that this will fail if the program happens to have the random string in it already, but for the purposes of this question lets accept that 'any' can be slightly limited. We don't have to consider extremely low probabilities of failure due to random chance or a malicious input program.
2The question is? No idea about the sense of this "questions" – None – 2012-08-11T11:44:15.077
good luck writing a useful program without
– None – 2012-08-11T12:04:38.173sorted(set('import for while = in : if def [] return class () break .'))
On the other hand, you can write http://en.wikipedia.org/wiki/Whitespace_(programming_language) programs with only three characters.@msw: To be fair, that's not true. All
for
loops can be replaced withwhile
loops (though admittedly that doesn't get you anywhere if you leave indef
,import
, andreturn
), all list literals could turn from[...]
tolist((...))
, you can shuffle a program to avoid anybreak
, and there are probably similar transformations. – David Robinson – 2012-08-11T12:56:17.6331
@vishvanda: While this question has problems, it did inspire me to write such a translator, which removes all characters except for
– David Robinson – 2012-08-11T13:18:50.843"()+,.1[]cehijnorx
. My solution is here.2
@msw: As you can see in my gist, it's actually possible to write any Python program with just the characters
– David Robinson – 2012-08-11T13:20:23.120"()+,.1[]cehijnorx
).Interesting, amusing, and likely a fun puzzle. Cheers. – None – 2012-08-11T14:20:54.787
I thought it was pretty interesting, too bad it was closed as. – None – 2012-08-11T16:16:10.207
1By the way, I've improved my solution to use only 9 characters:
()+1cehrx
. – David Robinson – 2012-08-11T19:29:44.367But does it support unicode ;) Good explanation regarding codegolf, too. – None – 2012-08-11T20:55:37.157