PracticeArrays

Add a Tag After Every Move

BeginnerArraystraversalaccumulationindex arithmetic Time not estimated Not started

Context

In a level editor, each move in a combo earns a short tag for the replay board. Given the ordered move labels and the tag to attach, build the displayed replay sequence without changing the original move list.

Problem

Write a function that creates a new sequence from an ordered list of move labels and a supplied tag. For every move, place the move in the result and then place the same tag immediately after it. Do this for every occurrence, including repeated moves and the final move. If the move list is empty, return an empty list. Preserve the input list unchanged and keep all items in their original order.

Examples

Example 1
Input: moves = []tag = "x"
Output: []
Explanation: With no moves, the executed call returns an empty list.
Example 2
Input: moves = ["up", "left", "up"]tag = "step"
Output: ["up", "step", "left", "step", "up", "step"]
Explanation: The repeated move "up" appears twice, and each move is followed by "step", producing ["up", "step", "left", "step", "up", "step"].
Example 3
Input: moves = ["jump", "run", "slide"]tag = "ok"
Output: ["jump", "ok", "run", "ok", "slide", "ok"]
Explanation: Each of "jump", "run", and "slide" is followed by "ok", producing ["jump", "ok", "run", "ok", "slide", "ok"].

Constraints

  • 0 <= len(moves) <= 100
  • 1 <= len(moves[i]) <= 20 for every i
  • moves[i]: alphabet lowercase
  • 1 <= len(tag) <= 10
  • tag: alphabet lowercase
  • moves and tag contain only lowercase letters.
  • Types: moves is str[], tag is str; result is str[]

Function signature

def tag_moves(moves: list[str], tag: str) -> list[str]
moves list[str]
The ordered move labels from a level run.
tag str
The lowercase tag to place after every move.
returns list[str]
A new list containing each move followed immediately by the supplied tag, in original order.

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

Notes

  • The source list must not be mutated.
  • The tag is added after every move, including the last one.
Approved · cb057-abs_377_v314 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1