Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.11 KB | None | 0 0
  1. # Exercise 2 Task 5
  2. # Axel Hoffmann, Johannes Wendel, Jonas Zeunert
  3.  
  4. import sys
  5. import collections
  6. from itertools import izip, product
  7.  
  8. # Some magic to define new operators
  9. class Infix:
  10.     def __init__(self, function):
  11.         self.function = function
  12.     def __ror__(self, other):
  13.         return Infix(lambda x, self=self, other=other: self.function(other, x))
  14.     def __or__(self, other):
  15.         return self.function(other)
  16.     def __rlshift__(self, other):
  17.         return Infix(lambda x, self=self, other=other: self.function(other, x))
  18.     def __rshift__(self, other):
  19.         return self.function(other)
  20.     def __call__(self, value1, value2):
  21.         return self.function(value1, value2)
  22.  
  23. # define 2 new operators |implies| and |equiv|
  24. implies = Infix(lambda x,y: not x or y)
  25. equiv = Infix(lambda x, y: (x |implies| y) and (y |implies| x))
  26.  
  27. # possible logic functions
  28. logical_operators = ["not", "and", "or", "implies", "equiv"]
  29.  
  30. #convert given formula to DNF
  31. def convert(formula):
  32.     result = "("
  33.     literals = extract_literals(formula)
  34.     truthtable = create_truthtable(len(literals))
  35.  
  36.     # replace for usage with Infix
  37.     formula = formula.replace("implies", "|implies|")
  38.     formula = formula.replace("equiv", "|equiv|")
  39.  
  40.     # for right output 
  41.     firstrun = True
  42.     # try all allocations in truthtable
  43.     for allocation in truthtable:
  44.         # Update global variables
  45.         for literal, value in izip(literals, allocation):
  46.             literals[literal] = value
  47.         globals().update(literals)
  48.  
  49.         # Evaluate formula with given truthtable allocation and add DNF
  50.         if(eval(formula)):
  51.             if(not firstrun):
  52.                 result += " or ("
  53.  
  54.             print_and = False
  55.             for key, value in literals.iteritems():
  56.                 if(print_and):
  57.                     result += " and "
  58.  
  59.                 if(not value):
  60.                     result += "not "
  61.                 result += key
  62.  
  63.                 print_and = True
  64.  
  65.             firstrun = False
  66.             result += ")"
  67.     return result
  68.  
  69. # Extracts all literals (every substring thats not a logical_operator) from given string
  70. # Returns a dictionary with all literals in the string
  71. def extract_literals(string):
  72.     literals = {}
  73.     # ignore brackets and split string
  74.     literals_str = string.replace('(', ' ').replace(')', ' ').split()
  75.     for l in literals_str:
  76.         if(not(l in logical_operators)):
  77.             literals[l] = False
  78.     # sort literals
  79.     sorted_literals = collections.OrderedDict(sorted(literals.items()))
  80.     return sorted_literals
  81.  
  82. def create_truthtable(num):
  83.     return list(product([True, False], repeat=num))
  84.  
  85. def utf8_convert(formula):
  86.     formula = formula.replace("and", "\\u2227")
  87.     formula = formula.replace("or", "\\u2228")
  88.     formula = formula.replace("not ", "\\u00ac")
  89.     formula = formula.replace("implies", "\\u2192")
  90.     formula = formula.replace("equiv", "\\u2194")
  91.     return formula.decode('unicode-escape')
  92.  
  93. if __name__ == "__main__":
  94.     if(len(sys.argv) != 2):
  95.         print("Usage: " +
  96.               sys.argv[0] + " \"formula\" \n" +
  97.               "Where usable logical operators are:")
  98.         print(logical_operators)
  99.         quit()
  100.  
  101.     input = sys.argv[1]
  102.    
  103.     print("Input:")
  104.     print(utf8_convert(input))
  105.  
  106.     converted_formula = convert(input)
  107.  
  108.     print("\nConverted formula:")
  109.     print(utf8_convert(converted_formula))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement