All challenges
Easy 0 solved 0% acceptance

Right Triangle of Stars

Print the classic right triangle of stars: one star on the first row, n on the last.

What you'll learn

  • Use the row number to decide how many symbols to draw
  • Build a row with an inner loop or string repetition
  • Return a pattern as a list of rows

Problem

The right triangle is usually the first pattern program in an intro course: row 1 has one *, row 2 has two, and so on down to row n. The outer loop picks the row and the inner loop (or string repetition) builds it.

Implement right_triangle_stars(n).

Input

n: The number of rows.

Output

Return the rows from top to bottom. Row i (counting from 1) is i * characters with no spaces.

Constraints

0 ≤ n ≤ 100

Examples

Example 1
Input:
n = 3

Expected output:
expected = ["*", "**", "***"]
Input:
long long n = 3;

Expected output:
std::vector<std::string> expected = {"*", "**", "***"};
Input:
long n = 3;

Expected output:
string[] expected = new string[] {"*", "**", "***"};

Explanation

Rows grow by one star: "*", "**", "***".

Example 2
Input:
n = 1

Expected output:
expected = ["*"]
Input:
long long n = 1;

Expected output:
std::vector<std::string> expected = {"*"};
Input:
long n = 1;

Expected output:
string[] expected = new string[] {"*"};

Explanation

A single row with one star: ["*"].

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

Row i needs exactly i stars, so the row number is all you need.

Hint 2

Repeat the star character instead of adding one character at a time if your language allows it.

Hint 3

When n is 0, return an empty list.

Expected complexity

O(n^2) time, O(n^2) extra space

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

The best passing solution of each coder. Faster, smaller and better-reviewed solutions rank higher.

No passing solutions yet. Solve it to take first place.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.