【每日一题20220307】字母大战

:woman_mage: 目前,一些字符群组在伺机而动,准备battle一把,局面现在很焦灼。让我们来编写一个函数,按照评估规则,帮助吃瓜群众分辨下目前哪边更具有优势。返回胜方对象,如果是平局则返回“Tie!”表示平局。

评估规则:字母严格区分大小写,按照字母表出场顺序统计战力,A=1,B=2,···,Z=26,a=0.5, b = 1, ···, z=13。

示例:
输入: "One", "Two" ,输出:"Two"
输入:"ONE", "NEO" ,输出: "Tie!"

题目难度:简单
题目来源:CodeWars: Battle of the characters

def solution(x: str, y: str) -> str:
    # your code here

assert solution("One", "Two") == "Two"
assert solution("One", "Neo") == "One"
assert solution("ONE", "NEO") == "Two"

题错了吧!assert solution(“ONE”, “NEO”) == “Two”

def solution(x: str, y: str) -> str:
    x_value = sum([(26 - (ord('Z') - ord(char.upper()))) * (1 if char.isupper() else 0.5) for char in x])
    y_value = sum([(26 - (ord('Z') - ord(char.upper()))) * (1 if char.isupper() else 0.5) for char in y])
    if x_value > y_value:
        return x
    elif x_value < y_value:
        return y
    return "Tie!"
1 Like
def solution(x: str, y: str) -> str:
    sum_x = sum([ord(i) - 64 if i.isupper() else (ord(i) - 96) / 2 for i in x])
    sum_y = sum([ord(i) - 64 if i.isupper() else (ord(i) - 96) / 2 for i in y])
    if sum_x > sum_y:
        return x
    elif sum_x < sum_y:
        return y
    else:
        return "Tie!"


assert solution("One", "Two") == "Two"
assert solution("One", "Neo") == "One"
assert solution("ONE", "NEO") == "Tie!"
    def solution(x: str, y: str) -> str:
        #ascii  a-z:97-122 A-Z:65-90
        """
        遍历字符串中的字符
        判断字符是否大写
        大写64-ord(i)
        小写96-ord(i)
        求和,比较
        """
        xa,ya = 0,0
        for i in x:
            if i.isupper():
                xa += 64 - ord(i)
            else:
                xa += (96 - ord(i))/2
        for i in y:
            if i.isupper():
                ya += 64 - ord(i)
            else:
                ya += (96 - ord(i)) / 2
        return y if xa > ya else (x if xa < ya else "Tie!")


    assert solution("One", "Two") == "Two"
    assert solution("One", "Neo") == "One"
    assert solution("ONE", "NEO") == "Tie!"

def solution(a,b):
“”"
1、取出大写字母所代表的值
2、遍历字符串,把小写字母转化为大写后再除以2,
3、小写字母与大写字母进行相加
4、a 和 b 两值进行逻辑比较
:param a:
:param b:
:return:
“”"
def calc(str):
a_res = 0
for i in str:
if i.isupper():
a_res += ord(i)-64
else:
i = i.upper()
a_res += (ord(i)-64)/2
return a_res

if calc(a) > calc(b):
    return a,calc(a),calc(b)
elif calc(a) == calc(b):
    return "Tie"

else:
    return b,calc(a),calc(b)

print(solution(“ONE”, “HELLO”))

def solution(x: str, y: str) -> str:
    sum_x = sum([ord(i) - 64 if i.isupper() else (ord(i) - 96) / 2 for i in x])
    sum_y = sum([ord(i) - 64 if i.isupper() else (ord(i) - 96) / 2 for i in y])
    return x if sum_x > sum_y else (y if sum_x < sum_y else 'Tie!')


assert solution("One", "Two") == "Two"
assert solution("One", "Neo") == "One"
assert solution("ONE", "NEO") == "Tie!"
def solution(x: str, y: str) -> str:
    dic = {chr(i + 65): i + 1 for i in range(26)}
    for i in range(26):
        dic[chr(i + 97)] = (i + 1) / 2
    sum_x = sum([dic[x[i]] for i in range(len(x))])
    sum_y = sum([dic[y[i]] for i in range(len(y))])
    if sum_x > sum_y:
        return x
    elif sum_x < sum_y:
        return y
    else:
        return "Tie!"


assert solution("One", "Two") == "Two"
assert solution("One", "Neo") == "One"
assert solution("ONE", "NEO") == "Tie!"