Build a Padded Tuple with + and *

Medium ⏱ 12 min 54% acceptance ★★★★☆ 4.3
Implement pad_tuple(values, length, fill=0) that returns a tuple of exactly length elements: values followed by enough copies of fill to reach length, built purely with tuple concatenation (+) and repetition (*). If values already has length or more elements, return only its first length elements (via slicing).

Examples

Example 1
Input
pad_tuple((1, 2), 5)
Output
(1, 2, 0, 0, 0)
Explanation

(1, 2) concatenated with (0,) * 3.

Example 2
Input
pad_tuple((1, 2, 3), 2)
Output
(1, 2)
Explanation

Already long enough, so it is trimmed to length 2.

Constraints

  • length >= 0
  • Use + and * for the padding, not a loop.

Topics

TuplesOperators

Companies

IntelCisco

Hints

Hint 1

(fill,) * n repeats the fill value n times as a tuple.

Hint 2

values + (fill,) * (length - len(values)) concatenates the two.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.