Last active
August 29, 2015 14:07
-
-
Save jul/f350e15ff6db3e0fc749 to your computer and use it in GitHub Desktop.
How to have fun in python with turiing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ift(p, vt, vf): | |
return lambda val: (ift(*vt)(val) if isinstance(vt, tuple) else vt) \ | |
if p(val) else (ift(*vf)(val) if isinstance(vf, tuple) else vf) | |
compile_nbit = lambda nbit: lambda f: sum([int(bool(f(x)))<<x for x in range(0, nbit)]) | |
compile = compile_nbit(256) | |
interpret = lambda code: lambda val: int((1<<(val))&code != 0) | |
code_by3 = compile(lambda x: (x%3)==0) | |
code_by2 = compile(lambda x: (x%2)==0) | |
code_by6 = code_by3&code_by2 | |
print bin(code_by3) | |
#0b10010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010#01001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001 | |
print bin(code_by6) | |
#0b10000010000010000010000010000010000010000010000010000010000010000010000010000010000010000010000010000010000010000010000010000010000#01000001000001000001000001000001000001000001000001000001000001000001000001000001000001000001000001000001000001000001000001 | |
#unit_testing | |
assert code_by6 == compile(lambda x: (x%6)==0), "not same results" | |
assert str(bin(code_by6)) == "0b1" + ("000001" * (256/6)), "not same code" | |
evaluate=interpret(code_by6) | |
print "\n".join(map(lambda x: "%d is %sdivsisble by 6" % (x, ift(evaluate, "", "not ")(x)), range(10))) | |
#0 is divsisble by 6 | |
#1 is not divsisble by 6 | |
#2 is not divsisble by 6 | |
#3 is not divsisble by 6 | |
#4 is not divsisble by 6 | |
#5 is not divsisble by 6 | |
#6 is divsisble by 6 | |
#7 is not divsisble by 6 | |
#8 is not divsisble by 6 | |
#9 is not divsisble by 6 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment