PracticeArrays
Uniform Move Codes
Context
In the arcade game Neon Vault, each player records a sequence of moves as a lowercase word. The game designer wants to activate a bonus only when every recorded move sequence uses exactly the same number of characters as the chosen move-length setting.
Problem
Write a function that decides whether every move sequence in a collection has exactly the required length. Return true when all sequences match the required length. Return false as soon as any sequence has a different length. If the collection is empty, return true because no sequence violates the requirement.
Examples
Example 1
Input:
moves = ["up", "down", "left"]required_length = 2Output:
FalseExplanation: The first sequence has length 2, but the required length is 2 and the executed result is false because a later sequence has length 4.
Example 2
Input:
moves = ["run", "jump", "turn"]required_length = 4Output:
FalseExplanation: The first sequence has length 3, while the required length is 4, so the executed result is false.
Example 3
Input:
moves = []required_length = 3Output:
TrueExplanation: The collection is empty, so no sequence violates the requirement and the executed result is true.
Constraints
0 <= len(moves) <= 1000 <= len(moves[i]) <= 20 for every i0 <= required_length <= 20moves contains only lowercase letters.- Types:
movesis str[],required_lengthis int; result is bool
Function signature
def all_moves_have_length(moves: list[str], required_length: int) -> bool
moveslist[str]- The recorded lowercase move sequences.
required_lengthint- The exact number of characters every move sequence must contain.
- returns bool
- True if every move sequence has exactly required_length characters, including when moves is empty; otherwise false.
Adapted from MBPP problem task_70 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Notes
- The empty collection is considered valid because there is no mismatching sequence.
- Do not reorder or copy the collection.
Code
Saved