Tous les défis
Facile 0 résolus 0 % de réussite

Inverted Right Triangle

Print a right triangle upside down: n stars on the first row, one on the last.

Ce que vous allez apprendre

  • Run a loop from n down to 1
  • Relate the row index to the number of symbols
  • Return a pattern as a list of rows

Problème

Afficher la traduction

Flip the right triangle: the first row is the longest, with n stars, and each following row has one star fewer, down to a single *. Counting the outer loop down instead of up is the whole trick.

Implement inverted_right_triangle(n).

Input

n: The number of rows.

Output

Return the rows from top to bottom. Row i (counting from 1) has n − i + 1 * characters.

Contraintes

0 ≤ n ≤ 100

Exemples

Exemple 1
Entrée :
n = 4

Sortie attendue :
expected = ["****", "***", "**", "*"]
Entrée :
long long n = 4;

Sortie attendue :
std::vector<std::string> expected = {"****", "***", "**", "*"};
Entrée :
long n = 4;

Sortie attendue :
string[] expected = new string[] {"****", "***", "**", "*"};

Explication

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

Exemple 2
Entrée :
n = 1

Sortie attendue :
expected = ["*"]
Entrée :
long long n = 1;

Sortie attendue :
std::vector<std::string> expected = {"*"};
Entrée :
long n = 1;

Sortie attendue :
string[] expected = new string[] {"*"};

Explication

With one row the triangle is just ["*"].

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

Let the outer loop count down from n to 1.

Indice 2

Each row has as many stars as the current loop value.

Indice 3

When n is 0, return an empty list.

Complexité attendue

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

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

La meilleure solution validée de chaque codeur. Les plus rapides, les plus légères et les mieux notées passent devant.

Aucune solution validée pour l'instant. Résolvez-le et prenez la première place.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.