PracticeSimulation

Predict the Next Draft Token Count

EasySimulationtraversalaccumulationstate tracking Time not estimated Not started

Context

A document-processing pipeline tracks token counts for the first two versions of an automatically expanded draft. Later versions follow a fixed recurrence, so the archive team wants to predict the count at a requested version without storing every earlier count.

Problem

The first two draft versions have token counts initial_terms[0] and initial_terms[1], corresponding to positions 0 and 1. For every position n at least 2, the count is defined as twice the count at position n-1 plus the count at position n-2. Return the count at position index. Compute the sequence forward from the two supplied initial counts; do not treat the input list as a complete sequence. Position 0 and position 1 must return their supplied values directly. The requested index is always valid and nonnegative.

Examples

Example 1
Input: index = 0initial_terms = [3, 5]
Output: 3
Explanation: For initial terms [3, 5] and index 0, the function returns 3 directly.
Example 2
Input: index = 1initial_terms = [2, 7]
Output: 7
Explanation: For initial terms [2, 7] and index 1, the function returns 7 directly.
Example 3
Input: index = 5initial_terms = [4, 6]
Output: 222
Explanation: For initial terms [4, 6] and index 5, the executed result is 222; the final traced current value is 222 at position 6.

Constraints

  • 2 <= len(initial_terms) <= 2
  • 0 <= initial_terms[i] <= 1000 for every i
  • 0 <= index <= 30
  • Types: initial_terms is int[], index is int; result is int

Function signature

def predict_token_count(initial_terms: list[int], index: int) -> int
initial_terms list[int]
The token counts at draft positions 0 and 1, in order.
index int
The zero-based draft position whose token count is requested.
returns int
The token count at the requested draft position under the stated second-order recurrence.

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

Notes

  • The recurrence uses the two immediately preceding positions.
  • Python integer arithmetic is suitable for the permitted range.
Approved · c012r1-abs_623_v114 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1