【每日一题20220628】判断是否有优惠

:mage:‍店铺周年庆,给定一个所有商品列表,商品名以 j、z、d、f 和 h 开头的将会打折扣,请你统计一下打折扣的商品,并以列表的方式打印到控制台。

样例一:
输入:

['jiu', 'zhangyu', 'luosifen']

输出:

['jiu', 'zhangyu']

样例二:
输入:

['laobie', 'tudou', 'liyu', 'huangyu']

输出:

['huangyu']

题目难度:简单

题目来源: 力扣

def solution(nums:list) -> list :
    """your code"""

assert solution(['jiu', 'zhangyu', 'luosifen']) == ['jiu', 'zhangyu']
assert solution(['laobie', 'tudou', 'liyu', 'huangyu']) == ['huangyu']

def solution(nums:list) → list :

tuple1=('j', 'z', 'd', 'f', 'h')
return [nums[i] for i in range(len(nums)) if str(nums[i]).startswith(tuple1)]

assert solution([‘jiu’, ‘zhangyu’, ‘luosifen’]) == [‘jiu’, ‘zhangyu’]
assert solution([‘laobie’, ‘tudou’, ‘liyu’, ‘huangyu’]) == [‘huangyu’]

def solution(nums:list) -> list :
    """your code"""
    result = []
    start_alphabet = ['j', 'z', 'd', 'f', 'h']
    for i in nums:
        for n in start_alphabet:
            if i.startswith(n):
                result.append(i)
                break
    return result
def solution(nums:list) -> list :
    nums_count = []
    for i in range(len(nums)):
        if nums[i][0] in ['j','z','d','f','h']:
            nums_count.append(nums[i])
    return nums_count

assert solution(['jiu', 'zhangyu', 'luosifen']) == ['jiu', 'zhangyu']
assert solution(['laobie', 'tudou', 'liyu', 'huangyu']) == ['huangyu']
assert solution(['laobie', 'tudou', 'liyu', 'huangyu','huh']) == ['huangyu','huh']
def solution(nums:list) -> list :
    return [i for i in nums if i[0] in ("j", "z", "d", "f", "h")]

assert solution(['jiu', 'zhangyu', 'luosifen']) == ['jiu', 'zhangyu']
assert solution(['laobie', 'tudou', 'liyu', 'huangyu']) == ['huangyu']
def solution(nums: list) -> list:
    """your code"""
    # l = ['j', 'z', 'd', 'f', 'h']
    # for i in nums:
    #     if i[0] not in l:
    #         l.remove(i)
    nums = [i for i in nums if i[0] in ['j', 'z', 'd', 'f', 'h']]
    return nums


assert solution(['jiu', 'zhangyu', 'luosifen']) == ['jiu', 'zhangyu']
assert solution(['laobie', 'tudou', 'liyu', 'huangyu']) == ['huangyu']
def solution(nums:list) -> list :
    return [i for i in nums if i[0] in ['j','z','d','f','h']]
关闭