Skip to content

Instantly share code, notes, and snippets.

@ivabus
Last active October 12, 2023 06:11
Show Gist options
  • Save ivabus/d95ac62ffee85b7b058effc573760ae9 to your computer and use it in GitHub Desktop.
Save ivabus/d95ac62ffee85b7b058effc573760ae9 to your computer and use it in GitHub Desktop.
Print truth table for given expression
def iterate(l, ost):
if ost == 0: return l
newL = []
for i in l:
for k in "01": newL.append(i + k)
return iterate(newL, ost - 1)
func = input("Input function (in eval format): ")
variables = []
for i in func:
if i in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" and not i in variables: variables.append(i)
iterList = iterate([""], len(variables))
print(*variables, sep="", end=" F\n")
for i in iterList:
currFunc = func
for k in range(len(variables)): currFunc = currFunc.replace(variables[k], i[k])
print(i, str(eval(currFunc)).replace("True", "1").replace("False", "0"))
@jbruws
Copy link

jbruws commented Oct 6, 2022

оформление 10/10, не переделывай

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