【每日一题20220419】未知数X

:mage:‍ 哈利波特最近收到一堆的数学作业,全是含有一个未知数x的等式,让我们编写一个函数帮他求出每个等式中的x的值吧。

【示例】
输入:x + 1 = 9 - 2
输出:6
解释:根据数学知识可以推断出x = 9-2-1,所以未知数x的值为6

题目难度:中等
题目来源:CodeWars-Value of x

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

assert solution('x + 1 = 9 - 2') == 6
assert solution('x - 2 + 3 = 2') == 1
assert solution('- 10 = x') == -10

def solution(eq: str) -> int:
    rz = eval(f'{eq.replace("=", "-(")})', {"x": 1j})
    return -rz.real / rz.imag


assert solution('x+1=9-2') == 6
assert solution('x-2+3=2') == 1
assert solution('-10=x') == -10
assert solution('3*x - 123 + 12* (3 - x) = 12') == -11

4 Likes

12後面少個*

def solution(eq: str)-> int:
    q = eval(eq.replace("=", "-(").replace("x", "0") + ")")
    if eq.find("x") > eq.find("="):
        return q
    else:
        return -q

assert solution('x + 1 = 9 - 2') == 6
assert solution('x - 2 + 3 = 2') == 1
assert solution('- 10 = x') == -10

def str_to_expreession(str):
    str_old = str.replace('x', '0').split('=')
    str_new = eval(str_old[1]) - eval(str_old[0])
    if str.find("x") < str.find("="):
        return str_new
    else:
        return -str_new
def solution(eq: str) -> int:
    str1, str2 = eq[:eq.find("=")], eq[eq.find("=") + 1:]
    if "x" not in str1:
        str1, str2 = str2, str1
    index2 = str1.find("x")
    str1 = str1.replace("x", "0")
    if index2 > 0 and str1[index2 - 2] == "-":
        x = eval(str1) - eval(str2)
    else:
        x = eval(str2) - eval(str1)
    return x

# assert solution('x + 1 = 9 - 2') == 6
# assert solution('9 - x = 1 + 4') == 4
# assert solution('4 - 1 = 8 - x') == 5
# assert solution('4 - 1 = 1 + x') == 2
# assert solution('x - 2 + 3 = 2') == 1
# assert solution('- 10 = x') == -10

牛逼

头像吓人 一身正义

def solution(eq: str)-> int:
    eq1=eq.replace('=','-(')+')'
    c=eval(eq1,{'x':1j})
    return c.real/ -c.imag
import re


def solution(eq: str) -> int:
    left, right = eq.split('=')
    x_repr = re.compile(r'([+-]? ?x)')
    if 'x' in right:
        left, right = right, left

    x_expr = x_repr.search(left)
    left = x_repr.split(left)
    if (left[0] + left[-1]).strip() == '':
        c_left = 0
    else:
        c_left = eval(left[0] + left[-1])
    c_right = eval(right)

    if '-' in x_expr.group():
        return c_left - c_right
    return c_right - c_left


assert solution('x + 1 = 9 - 2') == 6
assert solution('x - 2 + 3 = 2') == 1
assert solution('- 10 = x') == -10
assert solution('- x = - 1') == 1

assert solution('x + 1 - 5 = 9 - 2') == 11
assert solution('- x + 1 - 5 = 9 - 2') == -11
assert solution('1 + x - 5 = 9 - 2') == 11
assert solution('1 - x - 5 = 9 - 2') == -11
assert solution('1 - 5 + x = 9 - 2') == 11
assert solution('1 - 5 - x = 9 - 2') == -11

assert solution('1 - 5 = x + 9 - 2') == -11
assert solution('1 - 5 = - x + 9 - 2') == 11
assert solution('1 - 5 = 9 + x - 2') == -11
assert solution('1 - 5 = 9 - x - 2') == 11
assert solution('1 - 5 = 9 - 2 + x') == -11
assert solution('1 - 5 = 9 - 2 - x') == 11

偷着乐?

def solution(eq: str)-> int:
    # your code here
    re = eval(f"{eq.replace('=', '-(')})", {'x': 1j})
    return -re.real / re.imag    

assert solution('x + 1 = 9 - 2') == 6
assert solution('x - 2 + 3 = 2') == 1
assert solution('- 10 = x') == -10
def solution(eq: str):
    eq_old = eq.replace(' ','').replace('x', '0').split('=')
    result = eval(eq_old[0]) - eval(eq_old[1])
    if 'x' != eq[0] and eq[eq.find('x')-2] == '-':
        if eq.find("x") < eq.find("="):
            return result
        else:
            return -result
    else:
        if eq.find("x") < eq.find("="):
            return -result
        else:
            return result
def solution(eq: str) -> int:
    result = eval(eq.replace("=", "-(").replace("x", "0") + ')')
    if eq.index('x') < eq.index('='):
        return -result
    else:
        return result


assert solution('x + 1 = 9 - 2') == 6
assert solution('x - 2 + 3 = 2') == 1

def solution(eq: str) -> int:
    str = eq.replace(" ","")
    re = int(eval(eq.replace("=","-(").replace("x","0") + ")"))
    if str[str.index("x")-1]=="-":
        if str.index("x") < str.index("="):
            return re
        else:
            return -re
    else:
        if str.index("x") < str.index("="):
            return -re
        else:
            return re

assert solution('x + 1 = 9 - 2') == 6
assert solution('x - 2 + 3 = 2') == 1
assert solution('- 10 = x') == -10
assert solution('- 10 = 1- x') == 11
def solution(eq: str)-> int:
    a, b = eq.replace('x', '0').split(' = ')
    x = eval(b) - eval(a)
    if '- x' in eq: x = x * -1
    return x if eq.find('x') < eq.find('=') else -x
关闭