Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Here's a draft for an improved version of your code. I'll comment on things I have changed as soon as I have time. Also, many things are still to be done like getting rid of exec.

Well, actually, 200_success 200_success said it all.

import copy
import itertools
import sys
def get_equation_from_user():
 print("This program reads boolean equations and outputs the truth table.")
 print("You may use the operators ~, ∨, and ∧, or")
 print("their text equivalents, not, or, and and.")
 print("You may use parenthesis, braces, and brackets to group your equations,")
 print("and you must use single letters for your variable names, as 'a∨b'\n")
 # commented for testing purposes: return input()
 return "(a ∧ b) ∨ c"
def convert_equation_to_python(equation):
 new_eq = equation
 new_eq = new_eq.replace("~", " not ")
 new_eq = new_eq.replace("∨", " or ")
 new_eq = new_eq.replace("∧", " and ")
 new_eq = new_eq.replace("[", "(")
 new_eq = new_eq.replace("]", ")")
 new_eq = new_eq.replace("{", "(")
 new_eq = new_eq.replace("}", ")")
 new_eq = " " + new_eq + " "
 return new_eq
def get_sorted_variables(python_equation):
 variables = set()
 for index in range(len(python_equation)):
 c = python_equation[index]
 if c not in " ()^" and python_equation[index + 1] in " ()" and python_equation[index - 1] in " ()":
 variables.add(c)
 return sorted(variables)
equation = get_equation_from_user()
pyt_equation = convert_equation_to_python(equation)
equation_vars = get_sorted_variables(pyt_equation)
print('\t'.join(equation_vars) + '\t' + equation)
for vals in itertools.product(['0', '1'], repeat=len(equation_vars)):
 for eq, val in zip(equation_vars, vals):
 exec(eq + " = " + val)
 exec("result = " + pyt_equation)
 print('\t'.join(vals) + '\t' + str(1 if result else 0))

Here's a draft for an improved version of your code. I'll comment on things I have changed as soon as I have time. Also, many things are still to be done like getting rid of exec.

Well, actually, 200_success said it all.

import copy
import itertools
import sys
def get_equation_from_user():
 print("This program reads boolean equations and outputs the truth table.")
 print("You may use the operators ~, ∨, and ∧, or")
 print("their text equivalents, not, or, and and.")
 print("You may use parenthesis, braces, and brackets to group your equations,")
 print("and you must use single letters for your variable names, as 'a∨b'\n")
 # commented for testing purposes: return input()
 return "(a ∧ b) ∨ c"
def convert_equation_to_python(equation):
 new_eq = equation
 new_eq = new_eq.replace("~", " not ")
 new_eq = new_eq.replace("∨", " or ")
 new_eq = new_eq.replace("∧", " and ")
 new_eq = new_eq.replace("[", "(")
 new_eq = new_eq.replace("]", ")")
 new_eq = new_eq.replace("{", "(")
 new_eq = new_eq.replace("}", ")")
 new_eq = " " + new_eq + " "
 return new_eq
def get_sorted_variables(python_equation):
 variables = set()
 for index in range(len(python_equation)):
 c = python_equation[index]
 if c not in " ()^" and python_equation[index + 1] in " ()" and python_equation[index - 1] in " ()":
 variables.add(c)
 return sorted(variables)
equation = get_equation_from_user()
pyt_equation = convert_equation_to_python(equation)
equation_vars = get_sorted_variables(pyt_equation)
print('\t'.join(equation_vars) + '\t' + equation)
for vals in itertools.product(['0', '1'], repeat=len(equation_vars)):
 for eq, val in zip(equation_vars, vals):
 exec(eq + " = " + val)
 exec("result = " + pyt_equation)
 print('\t'.join(vals) + '\t' + str(1 if result else 0))

Here's a draft for an improved version of your code. I'll comment on things I have changed as soon as I have time. Also, many things are still to be done like getting rid of exec.

Well, actually, 200_success said it all.

import copy
import itertools
import sys
def get_equation_from_user():
 print("This program reads boolean equations and outputs the truth table.")
 print("You may use the operators ~, ∨, and ∧, or")
 print("their text equivalents, not, or, and and.")
 print("You may use parenthesis, braces, and brackets to group your equations,")
 print("and you must use single letters for your variable names, as 'a∨b'\n")
 # commented for testing purposes: return input()
 return "(a ∧ b) ∨ c"
def convert_equation_to_python(equation):
 new_eq = equation
 new_eq = new_eq.replace("~", " not ")
 new_eq = new_eq.replace("∨", " or ")
 new_eq = new_eq.replace("∧", " and ")
 new_eq = new_eq.replace("[", "(")
 new_eq = new_eq.replace("]", ")")
 new_eq = new_eq.replace("{", "(")
 new_eq = new_eq.replace("}", ")")
 new_eq = " " + new_eq + " "
 return new_eq
def get_sorted_variables(python_equation):
 variables = set()
 for index in range(len(python_equation)):
 c = python_equation[index]
 if c not in " ()^" and python_equation[index + 1] in " ()" and python_equation[index - 1] in " ()":
 variables.add(c)
 return sorted(variables)
equation = get_equation_from_user()
pyt_equation = convert_equation_to_python(equation)
equation_vars = get_sorted_variables(pyt_equation)
print('\t'.join(equation_vars) + '\t' + equation)
for vals in itertools.product(['0', '1'], repeat=len(equation_vars)):
 for eq, val in zip(equation_vars, vals):
 exec(eq + " = " + val)
 exec("result = " + pyt_equation)
 print('\t'.join(vals) + '\t' + str(1 if result else 0))
Source Link
SylvainD
  • 29.8k
  • 1
  • 49
  • 93

Here's a draft for an improved version of your code. I'll comment on things I have changed as soon as I have time. Also, many things are still to be done like getting rid of exec.

Well, actually, 200_success said it all.

import copy
import itertools
import sys
def get_equation_from_user():
 print("This program reads boolean equations and outputs the truth table.")
 print("You may use the operators ~, ∨, and ∧, or")
 print("their text equivalents, not, or, and and.")
 print("You may use parenthesis, braces, and brackets to group your equations,")
 print("and you must use single letters for your variable names, as 'a∨b'\n")
 # commented for testing purposes: return input()
 return "(a ∧ b) ∨ c"
def convert_equation_to_python(equation):
 new_eq = equation
 new_eq = new_eq.replace("~", " not ")
 new_eq = new_eq.replace("∨", " or ")
 new_eq = new_eq.replace("∧", " and ")
 new_eq = new_eq.replace("[", "(")
 new_eq = new_eq.replace("]", ")")
 new_eq = new_eq.replace("{", "(")
 new_eq = new_eq.replace("}", ")")
 new_eq = " " + new_eq + " "
 return new_eq
def get_sorted_variables(python_equation):
 variables = set()
 for index in range(len(python_equation)):
 c = python_equation[index]
 if c not in " ()^" and python_equation[index + 1] in " ()" and python_equation[index - 1] in " ()":
 variables.add(c)
 return sorted(variables)
equation = get_equation_from_user()
pyt_equation = convert_equation_to_python(equation)
equation_vars = get_sorted_variables(pyt_equation)
print('\t'.join(equation_vars) + '\t' + equation)
for vals in itertools.product(['0', '1'], repeat=len(equation_vars)):
 for eq, val in zip(equation_vars, vals):
 exec(eq + " = " + val)
 exec("result = " + pyt_equation)
 print('\t'.join(vals) + '\t' + str(1 if result else 0))
lang-py

AltStyle によって変換されたページ (->オリジナル) /