PracticeArrays

Uniform Move Codes

BeginnerArraystraversalstate trackingcomparison Time not estimated Not started

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 = 2
Output: False
Explanation: 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 = 4
Output: False
Explanation: The first sequence has length 3, while the required length is 4, so the executed result is false.
Example 3
Input: moves = []required_length = 3
Output: True
Explanation: The collection is empty, so no sequence violates the requirement and the executed result is true.

Constraints

  • 0 <= len(moves) <= 100
  • 0 <= len(moves[i]) <= 20 for every i
  • 0 <= required_length <= 20
  • moves contains only lowercase letters.
  • Types: moves is str[], required_length is int; result is bool

Function signature

def all_moves_have_length(moves: list[str], required_length: int) -> bool
moves list[str]
The recorded lowercase move sequences.
required_length int
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.
Approved · c019-abs_524_v114 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1