"""Lab to Code — Q vs K: which way will the reaction go?

Reaction quotient: Q = product of (concentration ** coefficient) over all species,
with negative coefficients for reactants. Compare Q with K to get the direction.
"""
from math import isclose


def reaction_quotient(conc, coeffs):
    q = 1.0
    for species, nu in coeffs.items():  # nu > 0: product, nu < 0: reactant
        q *= conc[species] ** nu
    return q


def direction(Q, K):
    if isclose(Q, K):
        return "equilibrium"
    return "forward" if Q < K else "reverse"


# A + B <=> C   with K = 4
coeffs = {"A": -1, "B": -1, "C": 1}
conc = {"A": 1.0, "B": 1.0, "C": 2.0}  # mol/L, right now

Q = reaction_quotient(conc, coeffs)
print(f"Q = {Q}")
print(direction(Q, K=4))
