PracticeArrays
Count Shipments for a Warehouse
Context
A regional logistics center records the destination code for each shipment loaded onto a truck. Given the ordered list of destination codes and one warehouse code, determine how many shipments are headed to that warehouse.
Problem
Implement a function that receives an integer array of shipment destination codes and a target warehouse code. Scan the shipment list from left to right and count every element equal to the target code. Return the final count. An empty shipment list has a count of zero, and repeated matching codes must each contribute to the result.
Examples
Example 1
Input:
destination_codes = [101, 203, 101, 404, 101]target_code = 101Output:
3Explanation: Example 1 executes with output 3. (fact_ids: )
Example 2
Input:
destination_codes = [0, -12, 500000, 0, 7, -12]target_code = -12Output:
2Explanation: Example 2 executes with output 2. (fact_ids: )
Example 3
Input:
destination_codes = []target_code = 42Output:
0Explanation: Example 3 executes with output 0 for the empty array. (fact_ids:, )
Constraints
0 <= len(destination_codes) <= 1000-1000000000 <= destination_codes[i] <= 1000000000 for every i-1000000000 <= target_code <= 1000000000- Types:
destination_codesis int[],target_codeis int; result is int
Function signature
def count_shipments_for_warehouse(destination_codes: list[int], target_code: int) -> int
destination_codeslist[int]- Codes identifying the destination warehouse for each shipment.
target_codeint- The warehouse code whose shipments must be counted.
- returns int
- The number of shipment destination codes equal to target_code.
Adapted from MBPP problem task_168 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Notes
- Use only constant auxiliary storage besides the input array.
- The order of destination_codes does not change the count.
Code
Saved