79644404

Date: 2025-05-29 18:06:15
Score: 0.5
Natty:
Report link

I try this code on tests: 6 passed and 3 failed (test_sym_expr_eq_3, test_sym_expr_eq_6 and test_sym_expr_eq_7). I don't see how to manage theses cases. Have you any idea?

def syms(e):
    if isinstance(e,sympy.Symbol):
        return [e]
    rv=[]
    if isinstance(e,sympy.Number):
        return [e]
    for i in e.args:
        rv.extend(syms(i))
    return rv

def reps(a, b, symbols): # return mapping if coherent else None
    if len(a) == len(b):
        rv = {}
        for i,j in zip(a,b):
            if i in symbols or j in symbols:
                continue
            if isinstance(i,sympy.Number) and isinstance(j,sympy.Number): # numbers must be equal
                if i != j: return
                continue
            if rv.get(i,j)!=j: # symbols must be always the same
                return
            rv[i]=j
        return rv

def sym_expr_eq(a, b, symbols = []):
    a = sympy.sympify(a)
    b = sympy.sympify(b)

    d = reps(syms(a), syms(b), symbols)

    if (d):
        return a.xreplace(d) == b
    else:
        return a == b
Reasons:
  • Blacklisted phrase (1): any idea?
  • Whitelisted phrase (-2): try this code
  • Long answer (-1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: P'tit Ju