【每日一题20220506】Bingo游戏

:mage:‍ 给定一个包含10个数字的列表,数字的范围是1~26(含),分别对应大写字母表A~Z。请编写一个函数, 判断这些数字是否能拼出一个’BINGO’,如果可以返回’WIN’,否则返回’LOSE’。数字允许重复,并且出现顺序不要求。

【示例】
输入:[1, 2, 3, 7, 5, 14, 7, 15, 9, 10]
输出:‘WIN’
解释:‘BINGO’字母对应的编号依次为2、9、14、7和15,输入列表中包含这些数字,因此返回’WIN’

题目难度:简单
题目来源:CodeWars-Bingo ( Or Not )

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

assert solution([1, 2, 3, 7, 5, 14, 7, 15, 9, 10]) == "WIN"
assert solution([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) == "LOSE"
assert solution([5, 2, 13, 7, 5, 14, 17, 15, 9, 10]) == "WIN"

def solution(num: list) -> str:
    def inner(target: str = "BINGO"):
        from string import ascii_uppercase
        num_str = [ascii_uppercase.index(i) + 1 for i in target]
        return all(map(lambda i: i in num, num_str))

    return "WIN" if inner() else "LOSE"

def solution(nums: list)-> str:
    str='BINGO'
    a=[string.ascii_uppercase.index(i)+1 for i in str if i in string.ascii_uppercase]
    if all(i in nums for i in a ):
        return 'WIN'
    else:
        return 'LOSE'
def solution(nums: list)-> str:
    # your code here
    if all(item in [chr(i + 64) for i in nums] for item in "BINGO"):
        return "WIN"
    else:
        return "LOSE"


assert solution([1, 2, 3, 7, 5, 14, 7, 15, 9, 10]) == "WIN"
assert solution([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) == "LOSE"
assert solution([5, 2, 13, 7, 5, 14, 17, 15, 9, 10]) == "WIN"

1 Like

还能更简洁 :joy: :joy: :joy:

def solution(nums: list) -> str:
   li = [2, 9, 14, 7, 15]
   n = 0
   for i in li:
      if i in nums:
         n += 1
   if n>=5:
      return 'WIN'
   else:
      return "LOSE"

assert solution([1, 2, 3, 7, 5, 14, 7, 15, 9, 10]) == "WIN"
assert solution([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) == "LOSE"
assert solution([5, 2, 13, 7, 5, 14, 17, 15, 9, 10]) == "WIN"
def solution(nums: list)-> str:
    str='BINGO'
    a={string.ascii_uppercase.index(i)+1 for i in str if i in string.ascii_uppercase}
    return 'WIN' if a & set(nums) == a else 'LOSE'
def solution(nums:list):
    if set([ord(i)-64 for i in "BINGO"])<=set(nums):
        return "WIN"
    else:
        return "LOSE"

if __name__ == '__main__':
    print(solution([5, 2, 13, 7, 5, 14, 17, 15, 9, 10]))
def solution(nums: list) -> str:
    a = [2, 9, 14, 7, 5]
    for i in a:
        if i not in nums:
            return "LOSE"
    return "WIN"


assert solution([1, 2, 3, 7, 5, 14, 7, 15, 9, 10]) == "WIN"
assert solution([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) == "LOSE"
assert solution([5, 2, 13, 7, 5, 14, 17, 15, 9, 10]) == "WIN"

from string import ascii_uppercase
def bingo(array):
# your code here
list_bingo=set([2,9,7,14,15])
set_array=set(array)
return ‘WIN’ if list_bingo<=set_array else ‘LOSE’

def solution(nums: list)-> str:
    # your code here
    target_li = [2, 9, 14, 7, 15]
    return "WIN" if set(target_li).issubset(set(nums)) else "LOSE"


assert solution([1, 2, 3, 7, 5, 14, 7, 15, 9, 10]) == "WIN"
assert solution([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) == "LOSE"
assert solution([5, 2, 13, 7, 5, 14, 17, 15, 9, 10]) == "WIN"
def solution(nums):
    target = [ord(letter)-64 for letter in "BINGO"]
    if any(item for item in target if item not in nums):
        return "LOSE"
    return "WIN"
关闭