PracticeSorting

Alphabetize Each Document's Tokens

EasySortingsortingcomparisontraversal Time not estimated Not started

Context

A document archive stores a short label for each document and the tokens extracted from it. The archive keeps labels in a fixed order, with each token list paired to the label at the same position. Clean the archive by alphabetizing every document's tokens without mixing documents or removing repeated tokens.

Problem

Write a function that receives document labels and their token lists in matching order. For every position, sort that document's tokens into nondecreasing lexicographic order. Return the sorted token lists in the same order, so position i still belongs to the original label at position i. Preserve every token occurrence, including duplicates. Labels are identifiers only and must not appear in the returned lists. The input lists are paired and have equal outer lengths; either outer list may be empty, and an individual document may have no tokens.

Examples

Example 1
Input: labels = ["invoice", "memo", "report"]token_lists = [["total", "due", "amount"], ["draft", "final", "draft"], ["summary", "annual", "data"]]
Output: [["amount", "due", "total"], ["draft", "draft", "final"], ["annual", "data", "summary"]]
Explanation: The three input sequences become ["amount", "due", "total"], ["draft", "draft", "final"], and ["annual", "data", "summary"] after sorting, while the duplicate "draft" occurrence remains present.
Example 2
Input: labels = ["alpha", "beta"]token_lists = [["zebra", "apple", "kiwi", "apple"], []]
Output: [["apple", "apple", "kiwi", "zebra"], []]
Explanation: The first sequence becomes ["apple", "apple", "kiwi", "zebra"], and the empty second sequence remains empty.
Example 3
Input: labels = []token_lists = []
Output: []
Explanation: With no labels and no token lists, the returned output is empty.

Constraints

  • 0 <= len(labels) <= 40
  • 1 <= len(labels[i]) <= 16 for every i
  • labels: distinct elements
  • labels[i]: alphabet lowercase
  • 0 <= len(token_lists) <= len(labels)
  • 0 <= len(token_lists[i]) <= 30 for every i
  • 1 <= len(token_lists[i][j]) <= 20 for every i, j
  • token_lists[i][j]: alphabet lowercase
  • labels and token_lists contain only lowercase letters
  • token_lists contains no spaces or punctuation.
  • Types: labels is str[], token_lists is str[][]; result is str[][]

Function signature

def sort_document_tokens(labels: list[str], token_lists: list[list[str]]) -> list[list[str]]
labels list[str]
Lowercase document identifiers, in archive order.
token_lists list[list[str]]
Token sequences paired with labels by position.
returns list[list[str]]
The token lists in the original document order, with each individual list sorted lexicographically in nondecreasing order and retaining all original occurrences.

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

Notes

  • The two outer inputs always have equal lengths, pairing each label with one token sequence.
  • An empty token sequence remains empty.
  • The returned outer list follows the original label order.
Approved · cb030-abs_62_v414 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1