Advertisement
Guest User

interpretation6/logicalExpression6

a guest
Sep 21st, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.60 KB | None | 0 0
  1. '''
  2. Created on 14 sep. 2017
  3.  
  4. @author: fredrikjansson
  5. '''
  6.  
  7. def length(seq):  # Returns the amount of items in a list
  8.     if not seq:
  9.         return 0
  10.     elif isinstance(seq[0], list):
  11.         return length(seq[0]) + length(seq[1:])
  12.     else:
  13.         return 1 + length(seq[1:])
  14.    
  15. def keys(interpretation):  # Returns the keys of a dictionary
  16.     keys = list(dict.keys(interpretation))
  17.     return keys
  18.  
  19. def transformDict(interpretation):  # Converting the interpretation from "true" and "false" to actually True and False
  20.     dictKeys = keys(interpretation)
  21.     print(dictKeys)
  22.     for i in range(length(dictKeys)):
  23.         if interpretation[dictKeys[i]] == "true":
  24.             interpretation[dictKeys[i]] = True
  25.         elif interpretation[dictKeys[i]] == "false":
  26.             interpretation[dictKeys[i]] = False
  27.         else:
  28.             print("The value in interpretation for this key is incorrect: ", interpretation[dictKeys[i]])
  29.  
  30.     return interpretation
  31.    
  32. def recursive(logicalExpression, interpretation):
  33.     if type(logicalExpression) is str:  # Checks if it's a string
  34.         if not interpretation:  # In order to avoid KeyError, took fucking three hours to fix this problem by adding two lines of code
  35.             return logicalExpression
  36.         print(logicalExpression)
  37.         return interpretation[logicalExpression]
  38.     # WORKS FINE FOR THE MOST PART, EXCEPT FOR logicalExpression6 WHEN IT PASSES THE elif FOR "NOT".
  39.     # THEN IT TRIES TO DO interpretation[AND] WHICH IS A BAD FUCKING IDEA.
  40.     elif logicalExpression[0] == "NOT":  # If first string is NOT, return not the value
  41.         return not recursive(logicalExpression[1], interpretation)
  42.     elif length(logicalExpression) == 1:
  43.         print("Interpretation: ", logicalExpression)
  44.         return recursive(logicalExpression[0], interpretation)
  45.     elif logicalExpression[1] == "OR":  # If second string is OR, return the value of the first or the third string
  46.         print("Interpretation: ", logicalExpression)
  47.         return recursive(logicalExpression[0], interpretation) or recursive(logicalExpression[2], interpretation)
  48.     elif logicalExpression[1] == "AND":  # If the second string is AND, return the value of the first and the third string
  49.         print("Interpretation: ", logicalExpression)
  50.         return recursive(logicalExpression[0], interpretation) and recursive(logicalExpression[2], interpretation)
  51.     else:
  52.         print("CATCH!")
  53.  
  54. logicalExpression1 = ["door_open", "AND", "cat_gone"]
  55. logicalExpression2 = ["cat_asleep", "OR", ["NOT", "cat_gone"]]
  56. logicalExpression3 = ["true", "OR", "true"]
  57. logicalExpression4 = "cat_gone"
  58. logicalExpression5 = ["NOT", ["NOT", ["NOT", ["cat_asleep", "OR", ["NOT", "cat_asleep"]]]]]
  59. logicalExpression6 = ["NOT", "AND", "true"]
  60. logicalExpression7 = ["NOT", "AND"]
  61. logicalExpression8 = ["door_open", "AND", "cat_asleep", "AND", "true", "AND", ["NOT", "true"]]
  62.  
  63. interpretation1 = {"door_open" : "false", "cat_gone" : "true", "cat_asleep" : "true"}
  64. interpretation2 = {"door_open" : "false", "cat_gone" : "true", "cat_asleep" : "true"}
  65. interpretation3 = {}
  66. interpretation4 = {"door_open" : "false", "cat_gone" : "true"}
  67. interpretation5 = {"cat_asleep" : "false"}
  68. interpretation6 = {"NOT" : "true"}
  69. interpretation7 = {"AND" : "false"}
  70. interpretation8 = {"door_open" : "true", "cat_asleep" : "false"}
  71.  
  72. def interpret():
  73.     transformDict(interpretation1)
  74.     transformDict(interpretation2)
  75.     transformDict(interpretation3)
  76.     transformDict(interpretation4)
  77.     transformDict(interpretation5)
  78.     transformDict(interpretation6)
  79.     transformDict(interpretation7)
  80.     transformDict(interpretation8)
  81.     print("")
  82.     print("1: ", recursive(logicalExpression1, interpretation1))
  83.     print("")
  84.     print("2: ", recursive(logicalExpression2, interpretation2))
  85.     print("")
  86.     print("3: ", recursive(logicalExpression3, interpretation3))  # // KeyError 'true'
  87.     print("")
  88.     print("4: ", recursive(logicalExpression4, interpretation4))
  89.     print("")
  90.     print("5: ", recursive(logicalExpression5, interpretation5))
  91.     print("")
  92.     print("6: ", recursive(logicalExpression6, interpretation6))  # // KeyError 'AND'
  93.     print("7: ", recursive(logicalExpression7, interpretation7))
  94.     print("")
  95.     print("8: ", recursive(logicalExpression8, interpretation8))
  96.     print("")
  97.     print(True and False and True and (not True))
  98.  
  99. interpret()
  100.  
  101.     # 1: False and True // False
  102.     # 2: True or (not True) // True
  103.     # 3: True or True // True
  104.     # 4: True // True
  105.     # 5: Not(Not(Not(False or True))) // False
  106.     # 6: True and True // True
  107.     # 7: True (Not False) // True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement