"""DuckieDai Descartes Rule Solver for TI-84 Plus CE Python."""


def duckiedai_intro(program_name, wait=True):
    title = program_name[:16]
    empty = 16 - len(title)
    left = empty // 2
    right = empty - left
    print("+----------------------+")
    print("|   " + " " * left + title + " " * right + "   |")
    print("|                      |")
    print("|       __             |")
    print("|   ___(o )>  quack!   |")
    print("|   \\ <_. )           |")
    print("|    `---'             |")
    print("|     by DuckieDai     |")
    print("+----------------------+")
    print("Loading...")
    if wait:
        input("Press enter to start ")
        print("\n" * 8)


def clear_screen():
    print("\n" * 8)


def count_changes(values):
    signs = []
    for value in values:
        if value > 0:
            signs.append(1)
        elif value < 0:
            signs.append(-1)
    changes = 0
    for i in range(1, len(signs)):
        if signs[i] != signs[i - 1]:
            changes += 1
    return changes


def possible_zeros(changes):
    answers = []
    while changes >= 0:
        answers.append(changes)
        changes -= 2
    return answers


def show_answers(title, answers):
    clear_screen()
    print(title)
    print("-------------")
    for i in range(len(answers)):
        if i > 0:
            print(",", end="")
        print(answers[i], end="")
    print("")
    input("\nPress enter ")


def run_test():
    clear_screen()
    print("DESCARTES RULE")
    print("Enter polynomial")
    print("")
    degree = int(input("Degree: "))
    coeffs = []
    power = degree
    while power >= 0:
        if power == 0:
            value = float(input("Const: "))
        elif power == 1:
            value = float(input("x coef: "))
        else:
            value = float(input("x^" + str(power) + ": "))
        coeffs.append(value)
        power -= 1
    pos_changes = count_changes(coeffs)
    neg_coeffs = []
    power = degree
    for value in coeffs:
        neg_coeffs.append(-value if power % 2 == 1 else value)
        power -= 1
    neg_changes = count_changes(neg_coeffs)
    show_answers("POSITIVE ZEROS", possible_zeros(pos_changes))
    show_answers("NEGATIVE ZEROS", possible_zeros(neg_changes))
    clear_screen()
    print("SIGN CHANGES")
    print("------------")
    print("Positive:", pos_changes)
    print("Negative:", neg_changes)


duckiedai_intro("Descartes Rule")
while True:
    run_test()
    print("")
    if input("Again? y/n: ").lower() != "y":
        break
clear_screen()
print("DuckieDai says: done!")
