PracticeArrays

Find a Warehouse Key by Position

BeginnerArraysindex arithmeticboundary handlingcomparison Time not estimated Not started

Context

A logistics system stores warehouse codes in the order they were registered. A dispatcher asks for the code at a particular position, where counting starts at zero. Negative positions count backward from the final code, following ordinary Python-style indexing.

Problem

Given an ordered list of distinct warehouse codes and a requested position, return the code at that position. Position 0 refers to the first code, and negative positions count from the end, so -1 refers to the last code. If the position is outside the valid range, return an empty string. Do not change the input list.

Examples

Example 1
Input: position = 0warehouse_keys = ["alpha", "bravo", "charlie", "delta"]
Output: "alpha"
Explanation: For position 0, the function returns "alpha", which is the first key in the supplied list.
Example 2
Input: position = -1warehouse_keys = ["north", "south", "east", "west"]
Output: "west"
Explanation: For position -1, the function returns "west", which is the last key in the supplied list.
Example 3
Input: position = 4warehouse_keys = ["red", "green", "blue"]
Output: ""
Explanation: For position 4, the function returns an empty string because that position is outside the valid range of the three-key list.

Constraints

  • 1 <= len(warehouse_keys) <= 100
  • 1 <= len(warehouse_keys[i]) <= 20 for every i
  • warehouse_keys: non empty
  • warehouse_keys: distinct elements
  • warehouse_keys[i]: alphabet lowercase
  • -100 <= position <= 99
  • warehouse_keys contains only lowercase letters.
  • Types: warehouse_keys is str[], position is int; result is str

Function signature

def find_warehouse_key(warehouse_keys: list[str], position: int) -> str
warehouse_keys list[str]
Distinct warehouse codes in their established registration order.
position int
The zero-based position to retrieve; negative values count from the end.
returns str
The warehouse code at the requested position, or an empty string when the position is invalid.

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

Notes

  • The list order is the mapping's established iteration order.
  • An empty string is reserved for an invalid position.
Approved · c028-abs_79_v414 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1