【每日一题20220621】点赞点踩

:mage:‍ 在一些社交媒体App的博文中,会提供:+1:【点赞】和:-1:【点踩】的按钮,用来表达观众的态度。默认按钮的默认值都是空的。具体规则如下:点赞和点踩是互斥的,同一时间只能赞或者踩;连续点赞或者点踩按钮两次会在高亮和取消之间翻转。 请编写一个函数,接收用户的动作列表,返回最终的态度结果。如果是点赞则返回“赞”,如果是点踩则返回“踩”,如果既没有“赞”或者“踩”则返回无。

【示例】
输入:["踩", "赞"]
输出:
解释:用户首先点了踩,然后点了赞,最终这篇博文收到了赞。

题目难度:简单
题目来源:codewars-Likes Vs Dislikes

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

assert solution(["踩", "赞"]) == "赞"
assert solution(["赞", "赞"]) == "无"
assert solution(["踩"]) == "踩"
def solution(words: list) -> str:
    # your code here
    result = "无"
    for i in words:
        result = "无" if result == i else i
    return result


assert solution(["踩", "赞"]) == "赞"
assert solution(["赞", "赞"]) == "无"
assert solution(["踩"]) == "踩"
def solution(words:list)-> str:
    word = words[-1]
    count = words.count(word)
    if count % 2 == 0:
        return "无"
    return word


if __name__ == '__main__':
    assert solution(["踩", "赞"]) == "赞"
    assert solution(["赞", "赞"]) == "无"
    assert solution(["踩"]) == "踩"
def solution(words:list)-> str:
    tmp = "无"
    for i in words:
        tmp = "无" if i == tmp else i
    return tmp


assert solution(["踩", "赞"]) == "赞"
assert solution(["赞", "赞"]) == "无"
assert solution(["踩"]) == "踩"
def solution(words:list)-> str:
    return '无' if words[1:]==words[:-1] and len(words)>1 else words[-1]
def solution(words:list)-> str:
    return '无' if len(set(words))==1 and len(words)>1 else words[-1]
def solution(words: list) -> str:
    # your code here
    length = len(words)
    if length == 1:
        return words[0]
    elif length >= 2 and words[-1] != words[-2]:
        return words[-1]
    else:
        return '无'

assert solution([“赞”, “赞”]) == “无”

这条case执行不同通过

列表

关闭