Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Created on 14 sep. 2017
- @author: fredrikjansson
- '''
- def length(seq): # Returns the amount of items in a list
- if not seq:
- return 0
- elif isinstance(seq[0], list):
- return length(seq[0]) + length(seq[1:])
- else:
- return 1 + length(seq[1:])
- def keys(interpretation): # Returns the keys of a dictionary
- keys = list(dict.keys(interpretation))
- return keys
- def transformDict(interpretation): # Converting the interpretation from "true" and "false" to actually True and False
- dictKeys = keys(interpretation)
- print(dictKeys)
- for i in range(length(dictKeys)):
- if interpretation[dictKeys[i]] == "true":
- interpretation[dictKeys[i]] = True
- elif interpretation[dictKeys[i]] == "false":
- interpretation[dictKeys[i]] = False
- else:
- print("The value in interpretation for this key is incorrect: ", interpretation[dictKeys[i]])
- return interpretation
- def recursive(logicalExpression, interpretation):
- if type(logicalExpression) is str: # Checks if it's a string
- if not interpretation: # In order to avoid KeyError, took fucking three hours to fix this problem by adding two lines of code
- return logicalExpression
- print(logicalExpression)
- return interpretation[logicalExpression]
- # WORKS FINE FOR THE MOST PART, EXCEPT FOR logicalExpression6 WHEN IT PASSES THE elif FOR "NOT".
- # THEN IT TRIES TO DO interpretation[AND] WHICH IS A BAD FUCKING IDEA.
- elif logicalExpression[0] == "NOT": # If first string is NOT, return not the value
- return not recursive(logicalExpression[1], interpretation)
- elif length(logicalExpression) == 1:
- print("Interpretation: ", logicalExpression)
- return recursive(logicalExpression[0], interpretation)
- elif logicalExpression[1] == "OR": # If second string is OR, return the value of the first or the third string
- print("Interpretation: ", logicalExpression)
- return recursive(logicalExpression[0], interpretation) or recursive(logicalExpression[2], interpretation)
- elif logicalExpression[1] == "AND": # If the second string is AND, return the value of the first and the third string
- print("Interpretation: ", logicalExpression)
- return recursive(logicalExpression[0], interpretation) and recursive(logicalExpression[2], interpretation)
- else:
- print("CATCH!")
- logicalExpression1 = ["door_open", "AND", "cat_gone"]
- logicalExpression2 = ["cat_asleep", "OR", ["NOT", "cat_gone"]]
- logicalExpression3 = ["true", "OR", "true"]
- logicalExpression4 = "cat_gone"
- logicalExpression5 = ["NOT", ["NOT", ["NOT", ["cat_asleep", "OR", ["NOT", "cat_asleep"]]]]]
- logicalExpression6 = ["NOT", "AND", "true"]
- logicalExpression7 = ["NOT", "AND"]
- logicalExpression8 = ["door_open", "AND", "cat_asleep", "AND", "true", "AND", ["NOT", "true"]]
- interpretation1 = {"door_open" : "false", "cat_gone" : "true", "cat_asleep" : "true"}
- interpretation2 = {"door_open" : "false", "cat_gone" : "true", "cat_asleep" : "true"}
- interpretation3 = {}
- interpretation4 = {"door_open" : "false", "cat_gone" : "true"}
- interpretation5 = {"cat_asleep" : "false"}
- interpretation6 = {"NOT" : "true"}
- interpretation7 = {"AND" : "false"}
- interpretation8 = {"door_open" : "true", "cat_asleep" : "false"}
- def interpret():
- transformDict(interpretation1)
- transformDict(interpretation2)
- transformDict(interpretation3)
- transformDict(interpretation4)
- transformDict(interpretation5)
- transformDict(interpretation6)
- transformDict(interpretation7)
- transformDict(interpretation8)
- print("")
- print("1: ", recursive(logicalExpression1, interpretation1))
- print("")
- print("2: ", recursive(logicalExpression2, interpretation2))
- print("")
- print("3: ", recursive(logicalExpression3, interpretation3)) # // KeyError 'true'
- print("")
- print("4: ", recursive(logicalExpression4, interpretation4))
- print("")
- print("5: ", recursive(logicalExpression5, interpretation5))
- print("")
- print("6: ", recursive(logicalExpression6, interpretation6)) # // KeyError 'AND'
- print("7: ", recursive(logicalExpression7, interpretation7))
- print("")
- print("8: ", recursive(logicalExpression8, interpretation8))
- print("")
- print(True and False and True and (not True))
- interpret()
- # 1: False and True // False
- # 2: True or (not True) // True
- # 3: True or True // True
- # 4: True // True
- # 5: Not(Not(Not(False or True))) // False
- # 6: True and True // True
- # 7: True (Not False) // True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement