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

Double Staircase

Print two staircases of # blocks that face each other across a two-space gap.

Ce que vous allez apprendre

  • Right-align one part of a row and left-align another
  • Assemble a row from several pieces
  • Avoid trailing spaces in output rows

Problème

Afficher la traduction

Two staircases of height n stand back to back with a gap between them, like the facing pyramids in an old platform game. Row i (counting from 1) has n − i leading spaces, then i # characters, then exactly two spaces, then i more # characters. The left staircase is aligned to the right edge and the right one to the left edge.

Implement double_staircase(n).

Input

n: The height of each staircase.

Output

Return the rows from top to bottom. Every row ends with a #, so there are no trailing spaces.

Contraintes

0 ≤ n ≤ 50

Exemples

Exemple 1
Entrée :
n = 3

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

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

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

Explication

Row 1 is 2 spaces, #, 2 spaces, #; row 3 has no leading spaces: [" # #", " ## ##", "### ###"].

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

One block on each side of the gap: ["# #"].

Comment l'aborder

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

Indice 1

Build the left staircase first: it is a right-aligned triangle.

Indice 2

The gap is always exactly two spaces.

Indice 3

The right staircase has the same width as the left one on the same row and needs no padding after it.

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.