【每日一题0908】寻找末位偶数

:woman_mage:已知一个由整数组成的列表nums,假设给定一个长度n,请编写一个函数,在列表中找出位置靠后的n个偶数,并且保持原来的出现顺序。

示例:
输入: ([1, 2, 3, 4, 5, 6, 7, 8, 9], 3),输出: [4, 6, 8]

def even_numbers(nums: list, n: int) -> list:
    pass

assert even_numbers([1, 2, 3, 4, 5, 6, 7, 8, 9], 3) == [4, 6, 8]
assert even_numbers([-22, 5, 3, 11, 26, -6, -7, -8, -9, -8, 26], 2) == [-8, 26]
assert even_numbers([6, -25, 3, 7, 5, 5, 7, -3, 23], 1) == [6]
def even_numbers(nums:list,n:int):
    """

    :param nums:
    :param n:
    :return:
    """
    list=[]
    for i in nums:
        if i%2==0:
            list.append(i)
 
    return list[-n:]
# return [i for i in nums if i%2==0][-n:]
   /**
     * 已知一个由整数组成的列表nums,假设给定一个长度n,请编写一个函数,在列表中找出位置靠后的n个偶数,并且保持原来的出现顺序。
     * <p>
     * 示例:
     * 输入: ([1, 2, 3, 4, 5, 6, 7, 8, 9], 3),输出: [4, 6, 8]。
     *
     * @param arrayList
     * @param integer
     * @return
     */
    public List<Integer> evenNumbers(List<Integer> arrayList, Integer integer) {
        List<Integer> collect = arrayList.stream().filter(i -> i % 2 == 0).collect(Collectors.toList());
        long count = collect.stream().count();
        if (count - integer >= 0) {
            List<Integer> result = collect.stream().skip(count - integer).collect(Collectors.toList());
            return result;
        } else {
            throw new RuntimeException("没有符合条件的返回值");
        }
    }

    @Test
    public void testEvenNumbers() {
        List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
        List<Integer> result1 = evenNumbers(integerList, 3);
        List<Integer> except1 = Arrays.asList(4, 6, 8);
        assert result1.equals(except1);
    }

image

def even_numbers(nums: list, n: int) -> list:
    return [num for num in nums if num%2 ==0][-n:]

assert even_numbers([1, 2, 3, 4, 5, 6, 7, 8, 9], 3) == [4, 6, 8]
assert even_numbers([-22, 5, 3, 11, 26, -6, -7, -8, -9, -8, 26], 2) == [-8, 26]
assert even_numbers([6, -25, 3, 7, 5, 5, 7, -3, 23], 1) == [6]
def even_numbers(nums: list, n: int) -> list:
    list = []
    for i in nums:
        if i % 2 == 0:
            list.append(i)
    return list[-n:]


assert even_numbers([1, 2, 3, 4, 5, 6, 7, 8, 9], 3) == [4, 6, 8]
assert even_numbers([-22, 5, 3, 11, 26, -6, -7, -8, -9, -8, 26], 2) == [-8, 26]
assert even_numbers([6, -25, 3, 7, 5, 5, 7, -3, 23], 1) == [6]
def even_numbers(nums: list, n: int) -> list:
    return [i for i in nums if i % 2 == 0][-n:]


assert even_numbers([1, 2, 3, 4, 5, 6, 7, 8, 9], 3) == [4, 6, 8]
assert even_numbers([-22, 5, 3, 11, 26, -6, -7, -8, -9, -8, 26], 2) == [-8, 26]
assert even_numbers([6, -25, 3, 7, 5, 5, 7, -3, 23], 1) == [6]
def even_numbers(nums: list, n: int) -> list:
    if n<1: return []
    even_list = [i for i in nums if i%2==0]
    return even_list if n>len(even_list) else even_list[-n:]

assert even_numbers([1, 2, 3, 4, 5, 6, 7, 8, 9], 3) == [4, 6, 8]
assert even_numbers([-22, 5, 3, 11, 26, -6, -7, -8, -9, -8, 26], 2) == [-8, 26]

assert even_numbers([6, -25, 3, 7, 5, 5, 7, -3, 23],2) == [6]
assert even_numbers([6, -25, 3, 7, 5, 5, 7, -3, 23],1) == [6]
assert even_numbers([6, -25, 3, 7, 5, 5, 7, -3, 23],-1) == []
assert even_numbers([6, -25, 3, 7, 5, 5, 7, -3, 23],0) == []

使用list.sort实现内部稳定排序

def even_numbers(nums: list, n: int) -> list:
    nums.sort(key=lambda x: x % 2, reverse=True)
    return nums[-n:]
def even_numbers(nums: list, n: int) -> list:
    res_list=[]
    for i in nums[::-1]:
        if i%2==0:
            res_list.append(i)
        if len(res_list)==n:
            return res_list[::-1]

assert even_numbers([1, 2, 3, 4, 5, 6, 7, 8, 9], 3) == [4, 6, 8]
assert even_numbers([-22, 5, 3, 11, 26, -6, -7, -8, -9, -8, 26], 2) == [-8, 26]
assert even_numbers([6, -25, 3, 7, 5, 5, 7, -3, 23], 1) == [6]

okok