Created
July 30, 2012 06:22
-
-
Save darkf/3205301 to your computer and use it in GitHub Desktop.
Logic, how does it work?
This file contains hidden or 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 parse(expr): | |
| lst = [] | |
| for line in expr.split('\n'): | |
| s = line.split(' -> ') | |
| if len(s) == 1: | |
| lst.append(('def', line)) | |
| else: | |
| lst.append(('imply', s[0], s[1])) | |
| return lst | |
| def leval(expr): | |
| sym = {} | |
| for p in expr: | |
| if p[0] == 'def': | |
| sym[p[1]] = True | |
| elif p[0] == 'imply': | |
| if sym.setdefault(p[1], False) is True: | |
| sym[p[2]] = True | |
| elif not p[2] in sym: | |
| sym[p[2]] = False | |
| return sym | |
| def main(): | |
| p = parse("""F | |
| F -> G | |
| H -> F | |
| Y -> L | |
| H""") | |
| assert p == [('def', 'F'), | |
| ('imply', 'F', 'G'), | |
| ('imply', 'H', 'F'), | |
| ('imply', 'Y', 'L'), | |
| ('def', 'H')] | |
| print leval(p) | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment