From 96f27e2b01a68707af5704124dba38a48b646994 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Wed, 22 Feb 2023 12:01:24 +0000 Subject: [PATCH 1/2] Fixed a bug that didn't allow you to negate variables used multiple times --- tools/truthtable/truthtable.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tools/truthtable/truthtable.py b/tools/truthtable/truthtable.py index f723e2e..d8316de 100644 --- a/tools/truthtable/truthtable.py +++ b/tools/truthtable/truthtable.py @@ -47,10 +47,14 @@ def getTable(variables, function): if index == len(tempFunction): break if var in "01" and tempFunction[index - 1] == '~': - tempFunction = tempFunction.replace('~' + var, str(1 - int(var))) - if var in variables: - tempFunction = tempFunction.replace(tempFunction[index], binary[variables.index(var)]) if tempFunction[index - 1] != '~' else tempFunction.replace(f"~{tempFunction[index]}", str(int(not int(binary[variables.index(tempFunction[index])])))+" ") - index += 1 + tempFunction = tempFunction.replace('~' + var, str(1 - int(var) + " ")) + try: + if var in variables: + tempFunction = tempFunction[:index] + binary[variables.index(var)] + tempFunction[index+1:] if tempFunction[index - 1] != '~' else tempFunction[:index-1] + str(int(not bool(int(binary[variables.index(tempFunction[index])]))))+" " + tempFunction[index+1:] + index += 1 + except ValueError: + index += 1 + continue try: truthTable[binary] = eval(tempFunction) except: @@ -58,6 +62,8 @@ def getTable(variables, function): return if truthTable[binary] > 1: truthTable[binary] = 1 + if truthTable[binary] < 0: + truthTable[binary] = 0 return truthTable From 7e6f6606e6f61052b2a0dc54998ed19628488b19 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Wed, 22 Feb 2023 12:05:30 +0000 Subject: [PATCH 2/2] Removed unnecessary lines --- tools/truthtable/truthtable.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/tools/truthtable/truthtable.py b/tools/truthtable/truthtable.py index d8316de..e9dc5be 100644 --- a/tools/truthtable/truthtable.py +++ b/tools/truthtable/truthtable.py @@ -48,13 +48,9 @@ def getTable(variables, function): break if var in "01" and tempFunction[index - 1] == '~': tempFunction = tempFunction.replace('~' + var, str(1 - int(var) + " ")) - try: - if var in variables: - tempFunction = tempFunction[:index] + binary[variables.index(var)] + tempFunction[index+1:] if tempFunction[index - 1] != '~' else tempFunction[:index-1] + str(int(not bool(int(binary[variables.index(tempFunction[index])]))))+" " + tempFunction[index+1:] - index += 1 - except ValueError: - index += 1 - continue + if var in variables: + tempFunction = tempFunction[:index] + binary[variables.index(var)] + tempFunction[index+1:] if tempFunction[index - 1] != '~' else tempFunction[:index-1] + str(int(not bool(int(binary[variables.index(tempFunction[index])]))))+" " + tempFunction[index+1:] + index += 1 try: truthTable[binary] = eval(tempFunction) except: