PracticeArrays
Add a Tag After Every Move
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) <= 1001 <= len(moves[i]) <= 20 for every imoves[i]: alphabet lowercase1 <= len(tag) <= 10tag: alphabet lowercasemoves and tag contain only lowercase letters.- Types:
movesis str[],tagis str; result is str[]
Function signature
def tag_moves(moves: list[str], tag: str) -> list[str]
moveslist[str]- The ordered move labels from a level run.
tagstr- 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.
Code
Saved