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

Butterfly Pattern

Print a butterfly: two star triangles facing each other that grow until they touch, then shrink again.

Ce que vous allez apprendre

  • Build a row from three parts: left wing, gap and right wing
  • Mirror a half to get the full shape
  • Spot the row where the gap disappears

Problème

Afficher la traduction

The butterfly has 2n rows. In the top half, row i (i = 1 to n) is i stars, then 2(n − i) spaces, then i stars again, so the two wings meet in the middle on row n. The bottom half repeats the same rows in reverse order, from i = n back down to i = 1, so the widest row appears twice in the middle.

Implement butterfly_pattern(n).

Input

n: The number of rows in each half.

Output

Return the 2n rows from top to bottom. Every row ends with a star, so no row has trailing spaces. When n is 0, return an empty list.

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

Top half: "* *", "** **", "******"; the bottom half is the same three rows reversed.

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

Each half is one row of 1 + 0 + 1 characters: ["**", "**"].

Comment l'aborder

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

Indice 1

In the top half, row i has i stars on each side.

Indice 2

The gap shrinks by two each row and is empty on row n.

Indice 3

The bottom half is the top half in reverse, including row n again.

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.