【每日一题1223】欧姆定律

:woman_mage: 已知欧姆定律公式:V = IR,其中V表示电压,单位是V;I表示电流,单位是A;R表示电阻,单位是R。

给定一个字符串,其中包含带有V,A或者R描述的任意两个单位信息,并且使用空格相隔。例如"2R 10V"和"1V 1A"。请编写一个函数,计算出欧姆定律公式中缺少的单位和值,最多保留6位小数。

示例:
输入:“2200R 5V”,输出:“0.002273A”。
输入:“25V 1e-2A”,输出:“2500.0R”。

题目难度:简单
题目来源:CodeWars:Ohm’s Law

def solution(info: str) -> str:
    # your code

assert solution("2200R 5V") == "0.002273A"
assert solution("0.005A 30V") == "6000.0R"
assert solution("30V 5000R") == "0.006A"
assert solution("0R 0A") == "0.0V"
def solution(info: str) -> str:
    R, V, I = None, None, None
    for i in info.split():
        if 'R' in i:
            R = float(i.replace('R', ''))
        elif 'V' in i:
            V = float(i.replace('V', ''))
        elif 'A' in i:
            I = float(i.replace('A', ''))
        else:
            raise ValueError("传值格式错误")
    if R is None:
        return str(round(V / I, 6)) + 'R'
    elif V is None:
        return str(round(I*R, 6)) + 'V'
    elif I is None:
        return str(round(V / R, 6)) + 'A'
    else:
        raise ValueError("解析错误")


assert solution("2200R 5V") == "0.002273A"
assert solution("0.005A 30V") == "6000.0R"
assert solution("30V 5000R") == "0.006A"
assert solution("0R 0A") == "0.0V"
def solution(info: str) -> str:
    str_arr = info.split(" ")
    _dict = {}
    _dict[str_arr[0][-1]] = float(str_arr[0][:-1])
    _dict[str_arr[1][-1]] = float(str_arr[1][:-1])
    result = -1
    if "R" not in _dict:
        result = str(round(_dict["V"] / _dict["A"], 6)) + "R"
    elif "V" not in _dict:
        result = str(round(_dict["A"] * _dict["R"], 6)) + "V"
    else:
        result = str(round(_dict["V"] / _dict["R"], 6)) + "A"
    return result
1 Like
def solution(info: str) -> str:
    V, A, R = None, None, None
    for i in info.split(" "):
        l = len(i) - 1
        if 'R' in i:
            R = float(i[0:l])
        elif 'A' in i:
            A = float(i[0:l])
        elif 'V' in i:
            V = float(i[0:l])
        else:
            raise ValueError("传值格式错误")
    if A is None:
        return str(round(V / R, 6)) + "A"
    elif V is None:
        return str(round(A*R, 6)) + "V"
    else:
        return str(round(V/A, 6)) + "R"


assert solution("2200R 5V") == "0.002273A"
assert solution("0.005A 30V") == "6000.0R"
assert solution("30V 5000R") == "0.006A"
assert solution("0R 0A") == "0.0V"

def solution(info: str) -> str:
    info_list = info.split(" ")
    a = info_list[0]
    b = info_list[1]
    if a[-1] in ['R', 'V'] and b[-1] in ['R', 'V']:
        if a[-1] == 'R':
            result = str(round(float(b[:-1]) / float(a[:-1]), 6)) + "A"
        else:
            result = str(round(float(a[:-1]) / float(b[:-1]), 6)) + "A"
    elif a[-1] in ['V', 'A'] and b[-1] in ['V', 'A']:
        if a[-1] == 'A':
            result = str(round(float(b[:-1]) / float(a[:-1]), 6)) + "R"
        else:
            result = str(round(float(a[:-1]) / float(b[:-1]), 6)) + "R"
    else:
        result = str(round(float(a[:-1]) * float(b[:-1]), 6)) + "V"
    return result


assert solution("2200R 5V") == "0.002273A"
assert solution("0.005A 30V") == "6000.0R"
assert solution("30V 5000R") == "0.006A"
assert solution("0R 0A") == "0.0V"
import re


def solution(info: str) -> str:
    re_v = re.search(r'(\S+)V', info)
    re_i = re.search(r'(\S+)A', info)
    re_r = re.search(r'(\S+)R', info)
    if not re_v:
        return str(round(float(re_i.group(1)) * float(re_r.group(1)), 6)) + 'V'
    if not re_i:
        return str(round(float(re_v.group(1)) / float(re_r.group(1)), 6)) + 'A'
    if not re_r:
        return str(round(float(re_v.group(1)) / float(re_i.group(1)), 6)) + 'R'


assert solution("2200R 5V") == "0.002273A"
assert solution("0.005A 30V") == "6000.0R"
assert solution("30V 5000R") == "0.006A"
assert solution("0R 0A") == "0.0V"
def solution(info: str) -> str:
    s1, s2 = info.split(" ")
    a, b = float(s1[:-1]), float(s2[:-1])
    if 'V' in info:
        if 'V' in s1:
            r = a / b
        else:
            r = b / a
        return str(round(r, 6)) + ('A' if 'R' in info else 'R')
    else:
        return str(round(a*b, 6)) + 'V'


assert solution("2200R 5V") == "0.002273A"
assert solution("0.005A 30V") == "6000.0R"
assert solution("30V 5000R") == "0.006A"
assert solution("0R 0A") == "0.0V"
def solution7(ss:str):
    try:
        R,V,A='','',''
        for i in ss.split(' '):
            if 'R'in i:
                R=float(i.replace('R',''))
            elif 'V' in i:
                V=float(i.replace('V',''))
            elif 'A' in i:
                A=float(i.replace('A',''))
        if R=='':
            return str(round(V/A,6))+'R'
        elif V=='':
            return str(round(A*R,6)) + 'V'
        elif A=='':
            return str(round(V/R,6)) + 'A'
    except:
        raise ValueError
assert solution7("2200R 5V") == "0.002273A"
assert solution7("0.005A 30V") == "6000.0R"
assert solution7("30V 5000R") == "0.006A"
assert solution7("0R 0A") == "0.0V"
关闭