Decoding and sampling · Easy

Completed Stop Sequences

Problem 22 of 50 · Python and NumPy

Problem

Check whether the current generated token list ends with a complete stop sequence. Return the index of the matching stop sequence in stop_sequences, choosing the lowest index if several match. Return -1 if none matches. Partial prefixes do not count, and matches earlier in the output do not count unless they reach the end.

def completed_stop_sequence(tokens, stop_sequences):

Constraints

  • tokens contains 0..64 integer IDs; stop_sequences contains 0..16 nonempty lists of IDs.
  • Stop sequences may overlap or be duplicates; their input order defines priority.
  • Inputs satisfy these constraints; invalid-input handling is not graded. Use only Python standard library and NumPy.

Examples

Example 1

Arguments: [
  [
    1,
    2,
    3
  ],
  [
    [
      2,
      3
    ],
    [
      9
    ]
  ]
]
Expected result: 0

The output ends in [2,3], which is rule index 0.

Example 2

Arguments: [
  [
    1,
    2
  ],
  [
    [
      2,
      3
    ]
  ]
]
Expected result: -1

Only token 2 has arrived; the required following 3 is missing.

Sign in to solve this problem

Browse all coding problems · Learn the foundations