Exemple #1
0
def pretty_print_CNF(input_string):
    """
    SymPy returns the CNF information in a form that is
    more line with its implementation that what is easily readable
    Modify the string to make it more readable in text.

    :returns: Converted CNF string.
    """
    output_str = MutableString()
    input_string_mutable = MutableString(input_string)
    # Remove nots and white space.
    input_string_mutable = input_string_mutable.replace("Not", "!")
    input_string_mutable = input_string_mutable.replace(" ", "")
    # Remove outer "And"
    input_string_mutable = input_string_mutable.replace("And(", "")
    # Remove trailing parenthesis
    input_string_mutable = input_string_mutable[: len(input_string_mutable) - 1]
    # Split based off OR
    all_clauses = input_string_mutable.split("),Or(")
    for clause in all_clauses:
        # Remove any preceding ors
        clause = clause.replace("Or(", "")
        # Remove any trailing parenthesis
        if clause[len(clause) - 1] == ")":
            clause = clause[: len(clause) - 1]
        clause = clause.replace(",", "+")
        clause = clause.replace("(", "")
        clause = clause.replace(")", "")
        # Build output string.
        if len(output_str) == 0:
            output_str += "(" + clause + ")"
        else:
            output_str += "&(" + clause + ")"

    # Replace period with commas
    output_str = output_str.replace(".", ",")
    return str(output_str)
def solve(a):
	a = MutableString(a)
	ans = 0
	while len(a) > 0:
		a = a.replace("{}","")
		if a and "{}" not in a:
			if a[0] == '}' and a[-1] == '{':
				ans += 2
				a[0]  = '{'
				a[-1] = '}'
			elif a[0] == '}':
				ans += 1
				a[0] = '{'
			elif a[-1] == '{':
				ans += 1
				a[-1] = '}'
	return ans