Skip to content

Instantly share code, notes, and snippets.

@Kakusakov
Last active February 4, 2022 07:26
Show Gist options
  • Save Kakusakov/bae9c156a60db7fa81b1048a5ae71298 to your computer and use it in GitHub Desktop.
Save Kakusakov/bae9c156a60db7fa81b1048a5ae71298 to your computer and use it in GitHub Desktop.
def simplifyExpr(target):
out = "" # output
brac = {} # dictionary matching ending positions of brackets to starting positions
bracStac = [] # stack containing positions of unclosed brackets
index = 0 # current position in target
outIndex = 0 # current position in out
skip = 0 # used to skip this many characters
for c in target:
app = ''
if(skip > 0):
skip -= 1
elif(c == ' '):
pass
elif(
c == 'x'
or c == 'y'
or c == 'z'
or c == 'w'
):
app = '(' + c + ') '
brac[outIndex + 2] = outIndex
elif(c == '('):
bracStac.append(outIndex)
app = c + ' '
elif(c == ')'):
brac[outIndex] = bracStac.pop(-1)
app = c + ' '
elif(c == '+'):
app = "or "
elif(c == '*'):
app = "and "
elif(c == '-'):
app = "not "
elif(c == '<' and target[index + 1] == '='):
skip += 1
#out.append("<=") # dont use unless brave or stupid
if(out[outIndex - 2] == ')'):
sbrIndex = brac[outIndex - 2]
if(sbrIndex > 0):
out = out[:sbrIndex] + "not" + out[sbrIndex:]
else:
out = "not" + out
app = "or "
outIndex += 3
else:
print("\nError in simplifyExpr(): implication failed\n")
print("index: ", index, "\nindexShift: ", outIndex, "\n")
raise
elif(c == '='):
if(target[index + 1] == '='): skip += 1
app = "== "
else:
print("\nError in simplifyExpr(): unknown symbol\n")
print("c: \'", c, "\'\nindex: ", index, "\n")
raise
index += 1
outIndex += len(app)
out = out + app
return out
def prExpr(varAmount, expr):
maxAmount = 4 # specify to set cap for varAmount
varNames = ['x', 'y', 'z', 'w']
if(varAmount > maxAmount):
print("Error in prExpr(): varAmount exciedes limit\n")
print("limit: ", maxAmount, "\nvarAmount: ", varAmount)
raise
varValues = [0] * varAmount
glob = {}
for i in range(varAmount):
print(varNames[i], ' ')
for t in range(2 ** varAmount):
for i in range(varAmount):
glob[varNames[i]] = varValues[i]
print("\n")
for i in range(varAmount):
print(varValues[i], ' ')
try:
print(bool(eval(expr, glob)))
except SyntaxError:
print("Error in prExpr(): eval() can't read target\n")
print("expr: \"", expr, "\"\n")
print("glob: ", glob, "\n")
raise
for i in range(varAmount):
if(varValues[i] == 1): varValues[i] = 0
else:
varValues[i] = 1
break
prExpr(3 , simplifyExpr(' x +( y <= z )')) # test
@KakusakovOffice
Copy link

def br(ch):
ch = ch.lower()
global x
if(ch == 'q'): return bool(37<=x<=37)
elif(ch == 'p'): return bool(17<=x<=54)
else: print(" error in br(): ch is of unknown value")

def simplifyExpr(target):
out = "" # output
brac = {} # dictionary matching ending positions of brackets to starting positions
bracStac = [] # stack containing positions of unclosed brackets
index = 0 # current position in target
outIndex = 0 # current position in out
skip = 0 # used to skip this many characters
for c in target:
app = ''
if (skip > 0):
skip -= 1
elif (c == ' '):
pass
elif (
c == 'x'
or c == 'y'
or c == 'z'
or c == 'w'
):
app = '(' + c + ') '
brac[outIndex + 2] = outIndex
elif (c == '('):
bracStac.append(outIndex)
app = c + ' '
elif (c == ')'):
brac[outIndex] = bracStac.pop(-1)
app = c + ' '
elif (
c == '+'
or c == '∨'
):
app = "or "
elif (c == '*'
or c == '∧'
):
app = "and "
elif (
c == '-'
or c == '¬'
):
app = "not"
elif (
(c == '<' and target[index + 1] == '=')
or c == '→'
):
if(c == '<' and target[index + 1] == '='): skip += 1
# out.append("<=") # dont use unless brave or stupid
if (out[outIndex - 2] == ')'):
sbrIndex = brac[outIndex - 2]
if (sbrIndex > 0):
out = out[:sbrIndex] + "not" + out[sbrIndex:]
else:
out = "not" + out
app = "or "
outIndex += 3
else:
print("\nError in simplifyExpr(): implication failed\n")
print("index: ", index, "\nindexShift: ", outIndex, "\n")
raise
elif (c == '='
or c == '≡'
):
if (target[index + 1] == '='): skip += 1
app = "== "
else:
print("\nError in simplifyExpr(): unknown symbol\n")
print("c: '", c, "'\nindex: ", index, "\n")
raise

    index += 1
    outIndex += len(app)
    out = out + app
return out

def prExpr(varAmount, expr):
maxAmount = 4 # specify to set cap for varAmount
varNames = ['x', 'y', 'z', 'w']

if (varAmount > maxAmount):
    print("Error in prExpr(): varAmount exciedes limit\n")
    print("limit: ", maxAmount, "\nvarAmount: ", varAmount)
    raise

varValues = [0] * varAmount
glob = {}

tempOut1 = ""
for i in range(varAmount):
    tempOut1 += varNames[i] + ' '
print(tempOut1)
for t in range(2 ** varAmount):
    for i in range(varAmount):
        glob[varNames[i]] = varValues[i]

    #print("\n")
    tempOut2 = ""
    for i in range(varAmount):
        tempOut2 += str(varValues[i]) + ' '
    try:
        print(tempOut2 + str(bool(eval(expr, glob))))
    except SyntaxError:
        print("Error in prExpr(): eval() can't read target\n")
        print("expr: \"", expr, "\"\n")
        print("glob: ", glob, "\n")
        raise

    for i in range(varAmount):
        if (varValues[i] == 1):
            varValues[i] = 0
        else:
            varValues[i] = 1
            break

def evBruteIneq(expr):
res = ""
for A in range(100):
Skip = False
for m in range(100):
for n in range(100):
glob = {
'A':A, 'm':m, 'n':n
}
try:
if (not(bool(eval(expr, glob)))):
Skip = True
break
except SyntaxError:
print("Error in evBruteIneq(): eval() can't read target\n")
print("expr: "", expr, ""\n")
print("glob: ", glob, "\n")
raise
if (Skip): break
if (not Skip):
res = str(A)
break
if(len(res) != 0): return res
else: return "evBruteIneq() failed"

tst = simplifyExpr("(¬ x → y) → (x → z)")
print(tst) # test
#evBruteIneq("(3m + 4n > 66) and (m <= A) and (n < A)")

@KakusakovOffice
Copy link

def br(ch):
ch = ch.lower()
global x
if(ch == 'q'): return bool(37<=x<=37)
elif(ch == 'p'): return bool(17<=x<=54)
else: print(" error in br(): ch is of unknown value")

def simplifyExpr(target):
out = "" # output
brac = {} # dictionary matching ending positions of brackets to starting positions
bracStac = [] # stack containing positions of unclosed brackets
index = 0 # current position in target
outIndex = 0 # current position in out
skip = 0 # used to skip this many characters
for c in target:
app = ''
chOrd = -1
isASCII = all(ord(s) < 128 for s in c)
if(isASCII): chOrd = ord(c)
if (skip > 0):
skip -= 1
elif (c == ' '):
pass
elif (
isASCII
and ((41 <= chOrd <= 90)
or (67 <= chOrd <= 122))
#c == 'x'
#or c == 'y'
#or c == 'z'
#or c == 'w'
):
app = '(' + c + ') '
brac[outIndex + 2] = outIndex
elif (c == '('):
bracStac.append(outIndex)
app = c + ' '
elif (c == ')'):
brac[outIndex] = bracStac.pop(-1)
app = c + ' '
elif (
c == '+'
or c == '∨'
):
app = "or "
elif (c == '*'
or c == '∧'
):
app = "and "
elif (
c == '-'
or c == '¬'
):
app = "not"
elif (
(c == '<' and target[index + 1] == '=')
or c == '→'
):
if(c == '<' and target[index + 1] == '='): skip += 1
# out.append("<=") # dont use unless brave or stupid
if (out[outIndex - 2] == ')'):
sbrIndex = brac[outIndex - 2]
'''if (sbrIndex > 0):
out = out[:sbrIndex] + "not" + out[sbrIndex:]
if(out[sbrIndex - 1] == 't'):
else:

                else:
                    out = "not" + out
            '''
            app = "or "
            outIndex += 3
        else:
            print("\nError in simplifyExpr(): implication failed\n")
            print("index: ", index, "\nindexShift: ", outIndex, "\n")
            raise
    elif (c == '='
        or c == '≡'
    ):
        if (target[index + 1] == '='): skip += 1
        app = "== "
    else:
        print("\nError in simplifyExpr(): unknown symbol\n")
        print("c: \'", c, "\'\nindex: ", index, "\n")
        raise

    index += 1
    outIndex += len(app)
    out = out + app
return out

def prExpr(varAmount, expr):
maxAmount = 4 # specify to set cap for varAmount
varNames = ['x', 'y', 'z', 'w']

if (varAmount > maxAmount):
    print("Error in prExpr(): varAmount exciedes limit\n")
    print("limit: ", maxAmount, "\nvarAmount: ", varAmount)
    raise

varValues = [0] * varAmount
glob = {}

tempOut1 = ""
for i in range(varAmount):
    tempOut1 += varNames[i] + ' '
print(tempOut1)
for t in range(2 ** varAmount):
    for i in range(varAmount):
        glob[varNames[i]] = varValues[i]

    #print("\n")
    tempOut2 = ""
    for i in range(varAmount):
        tempOut2 += str(varValues[i]) + ' '
    try:
        print(tempOut2 + str(bool(eval(expr, glob))))
    except SyntaxError:
        print("Error in prExpr(): eval() can't read target\n")
        print("expr: \"", expr, "\"\n")
        print("glob: ", glob, "\n")
        raise

    for i in range(varAmount):
        if (varValues[i] == 1):
            varValues[i] = 0
        else:
            varValues[i] = 1
            break

def evBruteIneq(expr):
res = ""
for A in range(100):
Skip = False
for m in range(100):
for n in range(100):
glob = {
'A':A, 'm':m, 'n':n
}
try:
if (not(bool(eval(expr, glob)))):
Skip = True
break
except SyntaxError:
print("Error in evBruteIneq(): eval() can't read target\n")
print("expr: "", expr, ""\n")
print("glob: ", glob, "\n")
raise
if (Skip): break
if (not Skip):
res = str(A)
break
if(len(res) != 0): return res
else: return "evBruteIneq() failed"

tst = simplifyExpr("(¬ R → y) → (x → z)")
print(tst) # test
#evBruteIneq("(3m + 4n > 66) and (m <= A) and (n < A)")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment