PracticeArrays

Count Shipments for a Warehouse

BeginnerArraystraversalaccumulationstate tracking Time not estimated Not started

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 = 101
Output: 3
Explanation: Example 1 executes with output 3. (fact_ids: )
Example 2
Input: destination_codes = [0, -12, 500000, 0, 7, -12]target_code = -12
Output: 2
Explanation: Example 2 executes with output 2. (fact_ids: )
Example 3
Input: destination_codes = []target_code = 42
Output: 0
Explanation: 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_codes is int[], target_code is int; result is int

Function signature

def count_shipments_for_warehouse(destination_codes: list[int], target_code: int) -> int
destination_codes list[int]
Codes identifying the destination warehouse for each shipment.
target_code int
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.
Approved · cb003-abs_622_v214 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1