import random N = 100 def genere(nb): assert nb in (0, 1, 2) a = 0 if nb == 0: while True: a, b, c = (random.randint(-20, 20)/5 for _ in range(3)) if a == 0: continue if b ** 2 - 4 * a * c < 0: return ((a, b, c), ()) if nb == 1: while a == 0: a, alpha = (random.randint(-5, 5) for _ in range(2)) return ((a, -2*a*alpha, a*alpha**2), (alpha, )) if nb == 2: x1 = x2 = 0 while a == 0 or x1 == x2: a, x1, x2 = (random.randint(-10, 10)/2 for _ in range(3)) return ((a, -a*(x1+x2), a*x1*x2), (x1, x2)) questions, reponses = zip(*[ genere(random.randint(0, 2)) for _ in range(N) ]) def format_nombre(n): string = str(n).replace(".", ",") if string.endswith(",0"): return string[:-2] if string.endswith(",00"): return string[:-3] if string.endswith(",000"): return string[:-4] return string def xcarre(a): if a == 1: return r"x^2" if a == -1: return r"-x^2" return r"{}x^2".format(format_nombre(a)) def xsimple(b): if b == 1: return "+x" if b == 0: return "" if b == -1: return "-x" if b < 0: return r"{}x".format(format_nombre(b)) return r"+{}x".format(format_nombre(b)) def constante(c): if c == 0: return "" if c > 0: return "+{}".format(format_nombre(c)) return format_nombre(c) for a, b, c in questions: print(r"\item ${} {} {}$".format(xcarre(a), xsimple(b), constante(c))) for racines in reponses: if len(racines) == 0: print(r"\item Pas de racines.") if len(racines) == 1: print(r"\item Une seule racine : ${}$.".format(format_nombre(racines[0]))) if len(racines) == 2: print(r"\item Deux racines : ${}$ et ${}$.".format(format_nombre(racines[0]), format_nombre(racines[1])))