PracticeArrays

Keep Even Shipment Values

BeginnerArraystraversalaccumulationfiltering Time not estimated Not started

Context

A warehouse records one integer handling value for each shipment on a dispatch manifest. The operations team wants a new manifest containing only values divisible by two. The retained values must stay in their original order, and repeated values must remain repeated.

Problem

Write a function that receives the shipment handling values from a warehouse manifest and returns a new array containing exactly the values divisible by two. Inspect the input from left to right. Keep zero and negative values when they are divisible by two, preserve the order of all retained values, and preserve their multiplicity. If no value qualifies, return an empty array. Do not modify the input array.

Examples

Example 1
Input: shipment_values = [7, 12, 3, 8, 5]
Output: [12, 8]
Explanation: The values divisible by two in [7, 12, 3, 8, 5] are 12 and 8, so the executed result is [12, 8].
Example 2
Input: shipment_values = [0, -6, 4, -6, 9]
Output: [0, -6, 4, -6]
Explanation: The executed result [0, -6, 4, -6] retains zero, negative values, and the repeated -6 from [0, -6, 4, -6, 9].
Example 3
Input: shipment_values = [1, -3, 5, 7]
Output: []
Explanation: No value in [1, -3, 5, 7] qualifies, so the executed result is [].

Constraints

  • 0 <= len(shipment_values) <= 100000
  • -1000000000 <= shipment_values[i] <= 1000000000 for every i
  • Types: shipment_values is int[]; result is int[]

Function signature

def filter_even_shipments(shipment_values: list[int]) -> list[int]
shipment_values list[int]
Integer handling values listed in manifest order.
returns list[int]
A new array containing the divisible-by-two shipment values in their original order, including duplicates.

Adapted from MBPP problem task_629 (CC BY 4.0). Rewritten, extended and verified by Iksha.

Approved · cb052r1-abs_29_v214 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1