【每日一题20220228】赛季积分

:woman_mage: 中国女足刚刚完成了本赛季的锦标赛,每场比赛的结果是采用“x:y”的格式记录,所有场次的结果都记录在一个列表,例如:["3:1", "2:2", "1:0", ...]。我们的任务是计算出球队的积分,规则是:

  • 如果x>y,那么积3分
  • 如果x<y,那么积0分
  • 如果x=y,那么积1分

请编写一个函数,统计女足本赛季的总积分。

题目难度:简单
题目来源:CodeWars: Total amount of points

def solution(nums: list) -> int:
    # your code here

assert solution(['1:0','2:0','3:0','4:0','2:1','3:1','4:1','3:2','4:2','4:3']) == 30
assert solution(['1:1','2:2','3:3','4:4','2:2','3:3','4:4','3:3','4:4','4:4']) == 10
assert solution(['1:0','2:0','3:0','4:0','2:1','1:3','1:4','2:3','2:4','3:4']) == 15
def solution(nums: list) -> int:
    result = 0
    for x in nums:
        n = x.split(':')
        if int(n[0]) > int(n[1]):
            result += 3
        elif int(n[0]) == int(n[1]):
            result += 1
        else:
            result += 0
    return result


assert solution(['1:0','2:0','3:0','4:0','2:1','3:1','4:1','3:2','4:2','4:3']) == 30
assert solution(['1:1','2:2','3:3','4:4','2:2','3:3','4:4','3:3','4:4','4:4']) == 10
assert solution(['1:0','2:0','3:0','4:0','2:1','1:3','1:4','2:3','2:4','3:4']) == 15
def solution(nums: list) -> int:
    success_num = 0
    fail_num = 0
    tie_num = 0
    for score in nums:
        score_list = score.split(':')
        if score_list[0] > score_list[-1]:
            success_num = success_num + 1
        elif score_list[0] == score_list[-1]:
            tie_num = tie_num + 1
        else:
            fail_num = fail_num + 1

    return success_num * 3 + tie_num * 1


assert solution(['1:0','2:0','3:0','4:0','2:1','3:1','4:1','3:2','4:2','4:3']) == 30
assert solution(['1:1','2:2','3:3','4:4','2:2','3:3','4:4','3:3','4:4','4:4']) == 10
assert solution(['1:0','2:0','3:0','4:0','2:1','1:3','1:4','2:3','2:4','3:4']) == 15
def solution(nums: list) -> int:
    score = 0
    for j in nums:
        if int(j.split(":")[0]) > int(j.split(":")[1]):
            score = score + 3
        elif int(j.split(":")[0]) < int(j.split(":")[1]):
            score = score
        else:
            score = score + 1

    return score

assert solution(['1:0','2:0','3:0','4:0','2:1','3:1','4:1','3:2','4:2','4:3']) == 30
assert solution(['1:1','2:2','3:3','4:4','2:2','3:3','4:4','3:3','4:4','4:4']) == 10
assert solution(['1:0','2:0','3:0','4:0','2:1','1:3','1:4','2:3','2:4','3:4']) == 15
    def solution(nums: list) -> int:
        core = 0
        for i in nums:
            if i[0] < i[2]:
                continue
            elif i[0] > i[2]:
                core += 3
            else:
                core += 1
        return core
def solution(nums: list) -> int:
    # your code here
    num = 0
    for i in nums:
        s = i.split(":")
        if s[0] > s[1]:
            num += 3
        elif s[0] == s[1]:
            num += 1
    return num
def solution(nums: list) -> int:
    # your code here
    num0 = 0
    for i in nums:
        if int(i[0]) > int(i[2]):
            num0 += 3
        elif int(i[0]) < int(i[2]):
            num0 += 0
        else:
            num0 += 1
    return num0


assert solution(['1:0','2:0','3:0','4:0','2:1','3:1','4:1','3:2','4:2','4:3']) == 30
assert solution(['1:1','2:2','3:3','4:4','2:2','3:3','4:4','3:3','4:4','4:4']) == 10
assert solution(['1:0','2:0','3:0','4:0','2:1','1:3','1:4','2:3','2:4','3:4']) == 15
def solution(nums: list) -> int:
    res = 0
    for i in nums:
        if i[0] > i[2]:
            res = res + 3
        elif i[0] == i[2]:
            res = res + 1
    return res


assert solution(['1:0', '2:0', '3:0', '4:0', '2:1', '3:1', '4:1', '3:2', '4:2', '4:3']) == 30
assert solution(['1:1', '2:2', '3:3', '4:4', '2:2', '3:3', '4:4', '3:3', '4:4', '4:4']) == 10
assert solution(['1:0', '2:0', '3:0', '4:0', '2:1', '1:3', '1:4', '2:3', '2:4', '3:4']) == 15

当比分有超过两位数,这程序就会报错了,所以应该不能这么写吧.

所以应该规定,足球比赛最大分手不能超过9 :joy:

def solution(nums: list) → int:
# your code here
# 先实例化一个为0的对象
b = 0
for i in nums:
c = i.split(":")
if c[0]>c[1]:
b = b+3
elif c[0]<c[1]:
b = b+0
elif c[0]==c[1]:
b = b + 1
return b
assert solution([‘1:0’,‘2:0’,‘3:0’,‘4:0’,‘2:1’,‘3:1’,‘4:1’,‘3:2’,‘4:2’,‘4:3’]) == 30
assert solution([‘1:1’,‘2:2’,‘3:3’,‘4:4’,‘2:2’,‘3:3’,‘4:4’,‘3:3’,‘4:4’,‘4:4’]) == 10
assert solution([‘1:0’,‘2:0’,‘3:0’,‘4:0’,‘2:1’,‘1:3’,‘1:4’,‘2:3’,‘2:4’,‘3:4’]) == 15

def test_solution(nums: list) -> int:
    num =0
    for i in nums:
        x,y = int(i.split(":")[0]),int(i.split(":")[1])
        if x>y:
            num+=3
        elif x<y:
             num=num
        else:
            num+=1
    return num
if __name__ == '__main__':
    assert test_solution(['1:0','2:0','3:0','4:0','2:1','3:1','4:1','3:2','4:2','4:3']) == 30
    assert test_solution(['1:1','2:2','3:3','4:4','2:2','3:3','4:4','3:3','4:4','4:4']) == 10
    assert test_solution(['1:0','2:0','3:0','4:0','2:1','1:3','1:4','2:3','2:4','3:4']) == 15
def solution(nums: list) -> int:
    res_num = 0
    for i in range(len(nums)):
        x, y = int(nums[i][0]), int(nums[i][2])
        if x > y:
            res_num += 3
        elif x == y:
            res_num += 1
        else:
            continue
    return res_num


assert solution(['1:0', '2:0', '3:0', '4:0', '2:1', '3:1', '4:1', '3:2', '4:2', '4:3']) == 30
assert solution(['1:1', '2:2', '3:3', '4:4', '2:2', '3:3', '4:4', '3:3', '4:4', '4:4']) == 10
assert solution(['1:0', '2:0', '3:0', '4:0', '2:1', '1:3', '1:4', '2:3', '2:4', '3:4']) == 15