【每日一题20220216】求导函数

:woman_mage: 给定一个方程式:f(x) = axn ,其中只包含一个未知项,并且an 都是整数。例如:f(x) = 3x2,f(x) = 5。

我们的任务是编写一个函数,把这个f(x)方程式的内容作为参数,格式为字符串。返回求导后的结果,字符串格式。

数学中的求导公式是:f(x)=axn,则f(x) = naxn-1。若n=0,则结果始终为0.

示例:
输入: “3x^2”,输出:“6x”。
输入: “6x^-2” ,输出: “-12x^-3”。
输入: “5x”,输出:“5”。
输入: “42”,输出:“0”。

题目难度:简单
题目来源:CodeWars:Derivatives of type x^n

def solution(fx: str) -> str
    # your code here

assert solution("3x^2") == "6x"
assert solution("6x^-2") == "-12x^-3"
assert solution("5x") == "5"
assert solution("42") == "0"
def solution(fx: str) -> str:
    if '^' in fx:
        f = fx.split('x^')
        if f[1] == "2":
            return str(int(f[0]) * int(f[1])) + 'x'
        else:
            return str(int(f[0]) * int(f[1])) + 'x^' + str(int(f[1]) - 1)
    elif 'x' in fx:
        f = fx.split('x')
        return f[0]
    else:
        return '0'


assert solution("3x^2") == "6x"
assert solution("6x^-2") == "-12x^-3"
assert solution("5x") == "5"
assert solution("42") == "0"
    if "^" in fx:
        list1 = fx.split("x^")
        n = int(list1[0]) * int(list1[1])
        m = int(list1[1]) - 1
        if m == 1:return "".join(str(n) + "x")
        else:return "".join(str(n) + "x^" + str(m))
    elif "x" in fx:return fx[:-1]
    else:return "0"
def solution(fx: str) -> str:
    n = 1 if ('x' in fx and '^' not in fx) else 0 if 'x^' not in fx else None
    f_list = fx.split('x') if n == 1 else fx.split('x^')
    if n is None:
        n = int(f_list[-1])
    return str(int(f_list[0]) * n) + ('x' if n not in [0, 1] else '') + ('' if n in [1, 2, 0, None] else '^' + str(n - 1))


assert solution("3x^2") == "6x"
assert solution("6x^-2") == "-12x^-3"
assert solution("5x") == "5"
assert solution("42") == "0"

def solution4(fx: str) → str:
if “x” not in fx:
return “0”
else:
if “^” not in fx:
num = fx.split(“x”)
return num[0]
else:
num = fx.split(“x^”)
a = int(num[0])
b = int(num[1])
if b == 2:
return str(ab) + “x”
else:
return str(a
b) + “x^” + str(b-1)

assert solution4(“3x^2”) == “6x”
assert solution4(“6x^-2”) == “-12x^-3”
assert solution4(“5x”) == “5”
assert solution4(“42”) == “0”

def solution(fx: str) -> str:
    params = fx.split('x^')
    if len(params) == 1:
        if 'x' in fx:
            return fx.replace('x', '')
        else:
            return '0'
    elif params[1] == '2':
        return str(eval(f"{params[0]} * {params[1]}")) + 'x'
    else:
        return str(eval(f"{params[0]} * {params[1]}")) + 'x^' + str(eval(f"{params[1]}- 1"))


assert solution("3x^2") == "6x"
assert solution("6x^-2") == "-12x^-3"
assert solution("5x") == "5"
assert solution("42") == "0"
def solution(f: str) -> str:
    if "^" not in f:
        if "x" not in f:
            return "0"
        return f[0]
    if "^" in f:
        a, b = f.split("^")
        if b == '2':
            return str(int(a[:-1]) * int(b)) + "x"
        return str(int(a[:-1]) * int(b)) + "x^" + str(int(b) - 1)
def solution(fx: str) -> str:
    x_posi = fx.find('x')
    if 'x' in fx:
        if '^' in fx:
            coeff = int(fx[:x_posi])
            exp = int(fx[x_posi + 2:])
            coeff *= exp
            exp -= 1
            deri = str(coeff)
            if exp:
                deri += 'x'
                if exp != 1:    deri += '^' + str(exp)

            return deri
        else:
            return fx[:-1]
        pass
    else:
        return '0'
    # your code here


assert solution("3x^2") == "6x"
assert solution("6x^-2") == "-12x^-3"
assert solution("5x") == "5"
assert solution("42") == "0"

    def solution(fx: str) -> str:
        if "x" in fx:
            lis = fx.split("x")
            if "^" not in fx:
                return lis[0]
            else:
                a = int(lis[0])
                b = int(lis[1][1:])
                if b == 2:
                    return f"{a * b}x"
                return f"{a * b}x^{b - 1}"
        else:
            return "0"
def solution(fx: str) -> str:
    an = fx.split('x')
    if len(an)==1:
        return '0'
    elif an[1]=='':
        return an[0]
    else:
        a = int(an[0])
        xn = an[1].split('^')
        n = int(xn[1])
        return str(a*n)+'x' if n-1 == 1 else str(a*n)+'x^'+str(n-1)
def solution(fx: str) -> str:
    if '^' in fx:
        fx_list = fx.split('x')
        if fx_list[1] == '^2':
            return str(int(fx_list[0]) * int(fx_list[1][fx_list[1].index('^') + 1:])) + 'x'
        else:
            return str(int(fx_list[0]) * int(fx_list[1][fx_list[1].index('^') + 1:])) + 'x' + '^' + str(
                int(fx_list[1][1:]) - 1)
    else:
        if 'x' in fx:
            return fx[:fx.index('x')]
        else:
            return '0'


assert solution("3x^2") == "6x"
assert solution("6x^-2") == "-12x^-3"
assert solution("5x") == "5"
assert solution("42") == "0"