"""DuckieDai Cubic Inequality 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("| hiddenwordscanner.com|")
    print("+----------------------+")
    print("Loading...")

    if wait:
        input("Press enter to start ")
        print("\n" * 8)


def factors(n):
    n=abs(n)
    if n==0:
        return [0]
    f=[]
    for i in range(1,n+1):
        if n%i==0:
            f.append(i)
    return f


def frac(x):
    for d in range(1,101):
        n=round(x*d)
        if abs(x-n/d)<0.000001:
            if d==1:
                return str(n)
            return str(n)+"/"+str(d)
    return str(round(x,4))


def divide(c,r):
    q=[c[0]]
    for i in range(1,len(c)-1):
        q.append(c[i]+q[-1]*r)
    rem=c[-1]+q[-1]*r
    return q,rem


duckiedai_intro("Cubic Inequality")

a=int(input("a: "))
b=int(input("b: "))
c=int(input("c: "))
d=int(input("d: "))

print("1 >0")
print("2 >=0")
print("3 <0")
print("4 <=0")
op=input("Choice: ")

coef=[a,b,c,d]

# Rational Zero Theorem candidates
cand=[]

if d==0:
    cand.append(0)

p=factors(d)
q=factors(a)

for x in p:
    for y in q:
        if y!=0:
            r=x/y
            if r not in cand:
                cand.append(r)
            if -r not in cand:
                cand.append(-r)

roots=[]
mult=[]

# Find roots and multiplicities
for r in cand:
    count=0

    while len(coef)>1:
        new,rem=divide(coef,r)

        if abs(rem)<0.000001:
            count+=1
            coef=new
        else:
            break

    if count>0:
        roots.append(r)
        mult.append(count)

if len(coef)>1:
    print("Could not factor")
else:

    # sort roots
    for i in range(len(roots)):
        for j in range(i+1,len(roots)):
            if roots[i]>roots[j]:
                roots[i],roots[j]=roots[j],roots[i]
                mult[i],mult[j]=mult[j],mult[i]

    n=len(roots)

    print("Roots:")
    for i in range(n):
        print(frac(roots[i]),"mult",mult[i])

    # Interval signs
    signs=[0]*(n+1)

    if a>0:
        signs[n]=1
    else:
        signs[n]=-1

    for i in range(n-1,-1,-1):
        signs[i]=signs[i+1]

        if mult[i]%2==1:
            signs[i]=-signs[i]

    if op=="1" or op=="2":
        want=1
    else:
        want=-1

    include=(op=="2" or op=="4")

    print("Solution:")

    found=False

    for i in range(n+1):

        if signs[i]==want:
            found=True

            if i==0:
                left="-inf"
                lb="("
            else:
                left=frac(roots[i-1])
                if include:
                    lb="["
                else:
                    lb="("

            if i==n:
                right="inf"
                rb=")"
            else:
                right=frac(roots[i])
                if include:
                    rb="]"
                else:
                    rb=")"

            print(lb+left+","+right+rb)

    # Include isolated zeros for >= or <=
    if include:
        for i in range(n):
            if signs[i]!=want and signs[i+1]!=want:
                found=True
                s=frac(roots[i])
                print("["+s+","+s+"]")

    if not found:
        print("EMPTY SET")
