Flip Neighboring Log Tokens
Context
Mira is cleaning short event logs before displaying them. Each log is stored as an ordered list of tokens. To create a mirrored rhythm, she wants every neighboring pair exchanged: the first and second tokens trade places, then the third and fourth, and so on. If the log has an odd number of tokens, its final token has no partner and stays at the end.
Problem
Write a function that transforms the supplied token list by reversing each consecutive non-overlapping pair from left to right. Process positions 0 and 1 as one pair, positions 2 and 3 as the next, and continue in the same way. Include every token exactly once in the returned list, including repeated tokens. When the input length is odd, preserve the final unpaired token at the end. An empty list must produce an empty list, and a one-token list must preserve that token.
Examples
tokens = ["login", "user", "success", "audit"]["user", "login", "audit", "success"]tokens = ["open", "file", "read"]["file", "open", "read"]tokens = ["retry", "retry", "close", "error", "retry"]["retry", "retry", "error", "close", "retry"]Constraints
0 <= len(tokens) <= 1000001 <= len(tokens[i]) <= 40 for every i- Types:
tokensis str[]; result is str[]
Function signature
def flip_log_tokens(tokens: list[str]) -> list[str]
tokenslist[str]- The ordered tokens from one event log.
- returns list[str]
- A new token list in which each complete adjacent pair from the input appears in reversed order, with any final unpaired token unchanged at the end.
Adapted from MBPP problem task_648 (CC BY 4.0). Rewritten, extended and verified by Iksha.