PIXELBANKv9.1.0
Menu

Pad or truncate a batch of integer sequences to a fixed length.

Given a list of sequences (lists of integers) and a target length:

  • If a sequence is shorter than the target, pad it with 0s at the end
  • If a sequence is longer than the target, truncate it to the target length
  • If a sequence is exactly the target length, leave it unchanged

Input format:

  • Line 1: Target length L
  • Line 2: Number of sequences N
  • Lines 3 to N+2: Space-separated integers for each sequence

Output: The padded/truncated sequences as a list of lists.

Example:

Input:
4
3
1 2 3
5 6 7 8 9
1 2 3 4
Output:
[[1, 2, 3, 0], [5, 6, 7, 8], [1, 2, 3, 4]]
Reasoning:

Target length: 4

  • Sequence [1, 2, 3]: length 3 < 4, pad with one 0 => [1, 2, 3, 0]
  • Sequence [5, 6, 7, 8, 9]: length 5 > 4, truncate => [5, 6, 7, 8]
  • Sequence [1, 2, 3, 4]: length 4 = 4, no change => [1, 2, 3, 4]

Constraints:

  • Pad with 0s at the END (post-padding)
  • Truncate from the END (keep first L elements)
  • L >= 1
  • Sequences contain non-negative integers
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.