Facile 4 résolus 100.0 % de réussite

Pyramid Rows

Draw a right-aligned pyramid of # blocks.

Ce que vous allez apprendre

  • Build rows with padding and blocks
  • Compute spaces and symbols per row
  • Return lines as a list

Problème

Afficher la traduction

Draw a right-aligned pyramid like the one in Super Mario: row i (counting from 1) has height − i spaces followed by i # characters.

Implement pyramid_rows(height).

Input

height: The number of rows.

Output

Return the rows from top to bottom.

Contraintes

0 ≤ height ≤ 50

Exemples

Exemple 1
Entrée :
height = 3

Sortie attendue :
expected = ["  #", " ##", "###"]
Entrée :
long long height = 3;

Sortie attendue :
std::vector<std::string> expected = {"  #", " ##", "###"};
Entrée :
long height = 3;

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

Explication

Rows are " #", " ##" and "###": each row has one more block and one less space.

Exemple 2
Entrée :
height = 1

Sortie attendue :
expected = ["#"]
Entrée :
long long height = 1;

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

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

Explication

A single row with one block: "#".

Comment l'aborder

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

Indice 1

Each row combines leading spaces with a growing run of hash characters.

Indice 2

Build rows one by one from top to bottom so the alignment stays correct.

Indice 3

If the height is zero, return no rows rather than an empty-looking pyramid.

Complexité attendue

O(height^2) time, O(height^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.

# Codeur Langage Temps Mémoire Note de l'IA Solution
1 HJhettory Jurash Python 778 ms - 4/5 Voir le code
2 LMLaura Mahlow Python 814 ms - 4/5 Voir le code
3 KKKaneki Ken Python 849 ms - 5/5 Voir le code
4 ДЗДавид Зайдулин Python 874 ms - 5/5 Voir le code

Apprenez des autres

Solutions de la communauté

Attention spoiler : 4 solutions complètes ci-dessous Essayez d'abord de le résoudre vous-même. Vous pouvez toujours jeter un œil au code des autres développeurs et aux avis IA. Connectez-vous et résolvez le défi pour rejoindre la discussion.