def ncdegree(polynomial):
degree = <NUM_LIT:0><EOL>if is_number_type(polynomial):<EOL><INDENT>return degree<EOL><DEDENT>polynomial = polynomial.expand()<EOL>for monomial in polynomial.as_coefficients_dict():<EOL><INDENT>subdegree = <NUM_LIT:0><EOL>for variable in monomial.as_coeff_mul()[<NUM_LIT:1>]:<EOL><INDENT>if isinstance(variable, Pow):<EOL><INDENT>subdegree += variable.exp<EOL><DEDENT>elif not isinstance(variable, Number) and variable != I:<EOL><INDENT>subdegree += <NUM_LIT:1><EOL><DEDENT><DEDENT>if subdegree > degree:<EOL><INDENT>degree = subdegree<EOL><DEDENT><DEDENT>return degree<EOL>
Returns the degree of a noncommutative polynomial.

    :param polynomial: Polynomial of noncommutive variables.
    :type polynomial: :class:`sympy.core.expr.Expr`.

    :returns: int -- the degree of the polynomial.