【每日一题20220601】精挑细选

:mage:‍ 给定一个数字列表,请编写一个函数,从左往右,遇到是偶数的则一直舍弃;直到遇到奇数,返回余下的所有元素。

【示例】
输入:[2,6,4,10,1,5,4,3]
输出:[1,5,4,3]
解释:列表的前2,6,4,10均为偶数,1为奇数,因此得到余下的[1,5,4,3]

题目难度:简单
题目来源:codewars-The dropWhile Function

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

assert solution([2,6,4,10,1,5,4,3]) == [1,5,4,3]
assert solution([1,4,2,3,5,4,5,6,7]) == [1,4,2,3,5,4,5,6,7]
assert solution([2,100,1000,10000,10000,5,3,4,6]) == [5,3,4,6]
def solution(nums: list) -> list:
    for index_num, item in enumerate(nums):
        if item % 2 == 1:
            break
    return nums[index_num:]
def solution(nums: list) -> list:
    # your code here
    return [nums[i:] for i, e in enumerate(nums) if e % 2 != 0][0]


assert solution([2, 6, 4, 10, 1, 5, 4, 3]) == [1, 5, 4, 3]
assert solution([1, 4, 2, 3, 5, 4, 5, 6, 7]) == [1, 4, 2, 3, 5, 4, 5, 6, 7]
assert solution([2, 100, 1000, 10000, 10000, 5, 3, 4, 6]) == [5, 3, 4, 6]
def drop_while(nums):
    for i, v in enumerate(nums):
        if v%2==1:
            return arr[i:]
    return []

def solution(nums: list)-> list:
    for index,value in enumerate(nums):
        if value%2 == 1:
            return nums[index:]

assert solution([2,6,4,10,1,5,4,3]) == [1,5,4,3]
assert solution([1,4,2,3,5,4,5,6,7]) == [1,4,2,3,5,4,5,6,7]
assert solution([2,100,1000,10000,10000,5,3,4,6]) == [5,3,4,6]
def solution(nums: list)-> list:
    for i in nums:
        if i %2!=0:
            return nums[nums.index(i):]
def solution(nums: list)-> list:
    # your code here
    # while len(nums):
    #     if nums[0] % 2 != 0:
    #         nums.remove(nums[0])
    #         continue
    #     else:
    #         break
    # return nums
    for i in range(len(nums)):
        if nums[i] % 2 != 0:
            return nums[i:]
    return []


assert solution([2,6,4,10,1,5,4,3]) == [1,5,4,3]
assert solution([1,4,2,3,5,4,5,6,7]) == [1,4,2,3,5,4,5,6,7]
assert solution([2,100,1000,10000,10000,5,3,4,6]) == [5,3,4,6]
assert solution([2,6]) == []
def solution(nums: list) -> list:
    i = 0
    while i < len(nums):
        if nums[i] % 2 != 0:
            return nums[i:]
        else:
            i += 1

assert solution([2, 6, 4, 10, 1, 5, 4, 3]) == [1, 5, 4, 3]
assert solution([1, 4, 2, 3, 5, 4, 5, 6, 7]) == [1, 4, 2, 3, 5, 4, 5, 6, 7]
assert solution([2, 100, 1000, 10000, 10000, 5, 3, 4, 6]) == [5, 3, 4, 6]