PY-28

Count Fixed-Length Token Groups

  • Medium
  • Text Processing
  • Python

Task

Write count_token_groups(tokens, size). It must return a dictionary that counts every consecutive group of exactly size tokens in tokens.

Groups overlap. The group beginning at position 0 contains positions 0 through size - 1; the next group begins at position 1, and so on. The dictionary key for a group is a tuple. size is a positive integer. If tokens is shorter than size, there are no groups and the result is empty. Do not change tokens.

Example

The first and third positions begin the same two-token group, so that tuple gets count 2. The group beginning at position 1 overlaps both of them.

Your implementation

Edit solution.py and keep this function name and signature:

Return a new dictionary whose keys are tuples of length size and whose values are their complete counts. Preserve the order in which tuple keys are first encountered. An empty input, or an input shorter than size, returns an empty dictionary. Do not print the result or ask for input.