Print a Simple Number Triangle Pattern

Easy ⏱ 8 min 73% acceptance ★★★★★ 4.9
Write a function number_triangle(n) that returns a list of n strings forming a growing number triangle, where row i (1-indexed) contains the digits 1 through i separated by single spaces. For example for n=3: ["1", "1 2", "1 2 3"].

Examples

Example 1
Input
number_triangle(3)
Output
["1", "1 2", "1 2 3"]
Explanation

Row i lists numbers 1..i.

Example 2
Input
number_triangle(1)
Output
["1"]
Explanation

A single row with just "1".

Constraints

  • n is a positive integer.

Topics

Loopsnested loopspattern printing

Companies

OracleIBM

Hints

Hint 1

Use an outer loop for the row number and an inner loop (or a range->join) to build each row string.

Hint 2

`" ".join(str(x) for x in range(1, i + 1))` builds one row cleanly.

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