-
Notifications
You must be signed in to change notification settings - Fork 0
✨ add challenge-10 solution #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
2023/10-crea-tu-propio-arbol-de-navidad/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| # Reto 10: Crea tu propio arbol de navidad | ||
|
|
||
| ## Problema | ||
|
|
||
| ¡Vaya idea ha tenido Sam Elfman! Quiere ofrecer un servicio que te crea un **árbol de Navidad 🎄 personalizado** en cuestión de segundos. | ||
|
|
||
| Para crearlo nos pasan una **cadena de caracteres para formar el árbol** y un **número que indica la altura del mismo.** | ||
|
|
||
| Cada carácter de la cadena representa un adorno del árbol, y vamos utilizándolos **de forma cíclica** hasta llegar a la altura indicada. **Como mínimo siempre nos pasarán uno.** | ||
|
|
||
| Debemos devolver un **string** multilínea con el árbol de Navidad formado con los adornos, la altura indicada **más una última línea con el tronco formado por el carácter |** en el centro y, finalmente, un salto de línea `\n.` | ||
|
|
||
| Por ejemplo si recibimos la cadena "123" y el número 4 como altura, tendríamos que construir este árbol: | ||
|
|
||
| ```txt | ||
| 1 | ||
| 2 3 | ||
| 1 2 3 | ||
| 1 2 3 1 | ||
| | | ||
| ``` | ||
|
|
||
| Si recibimos la cadena *@o y el número 3, el árbol que debemos devolver es: | ||
|
|
||
| ```txt | ||
| * | ||
| @ o | ||
| * @ o | ||
| | | ||
| ``` | ||
|
|
||
| Nota: | ||
|
|
||
| - **El árbol siempre debe estar centrado, para ello añade espacios en blanco a la izquierda de cada línea.** | ||
| - **Crea espacios sólo a la izquierda de cada línea del árbol. No dejes espacios en blanco a la derecha.** | ||
| - **Los adornos tienen un espacio en blanco entre ellos de separación.** | ||
|
|
||
| ## Mi solución | ||
|
|
||
| ```js | ||
| const createChristmasTree = (ornaments, height) => { | ||
| const heightSucessive = (height / 2) * (height + 1); | ||
| const repeatOrnaments = [ | ||
| ...ornaments.repeat((heightSucessive / ornaments.length) + 1), | ||
| ].join(' '); | ||
| const spaces = ' '.repeat(height - 1); | ||
|
|
||
| let tree = ''; | ||
| let i = 0; | ||
| let counter = 0; | ||
| while (counter < height) { | ||
| const ornamentsLine = repeatOrnaments.substring(i, i + 2 * counter + 1); | ||
| const level = `${spaces.substring(counter)}${ornamentsLine}\n`; | ||
| tree += level; | ||
| i += 2 * (counter + 1); | ||
| counter++; | ||
| } | ||
|
|
||
| return `${tree}${' '.repeat(height - 1)}|\n`; | ||
| }; | ||
| ``` | ||
|
|
||
| ## Explicación de mi solución | ||
|
|
||
| 1. Calculo la cantidad de adornos que necesito para la altura indicada, teniendo en cuenta que cada nivel tiene un adorno más que el anterior. | ||
| 2. Repito la cadena de adornos la cantidad de veces necesaria para tener la cantidad de adornos necesarios. | ||
| 3. Creo una variable para guardar el árbol. | ||
| 4. Creo un bucle que se repita tantas veces como niveles tenga el árbol. | ||
| 5. En cada iteración, creo una variable para guardar los adornos de ese nivel. | ||
| 6. Creo una variable para guardar los espacios en blanco que necesito para centrar los adornos. | ||
| 7. Creo una variable para guardar el nivel del árbol. | ||
| 8. Concateno el nivel del árbol a la variable del árbol. | ||
| 9. Incremento el contador de niveles. | ||
| 10. Devuelvo el árbol. |
22 changes: 22 additions & 0 deletions
2023/10-crea-tu-propio-arbol-de-navidad/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| const createChristmasTree = (ornaments, height) => { | ||
| const heightSucessive = (height / 2) * (height + 1); | ||
| const repeatOrnaments = [ | ||
| ...ornaments.repeat((heightSucessive / ornaments.length) + 1), | ||
| ].join(' '); | ||
| const spaces = ' '.repeat(height - 1); | ||
|
|
||
| let tree = ''; | ||
| let i = 0; | ||
| let counter = 0; | ||
| while (counter < height) { | ||
| const ornamentsLine = repeatOrnaments.substring(i, i + 2 * counter + 1); | ||
| const level = `${spaces.substring(counter)}${ornamentsLine}\n`; | ||
| tree += level; | ||
| i += 2 * (counter + 1); | ||
| counter++; | ||
| } | ||
|
|
||
| return `${tree}${' '.repeat(height - 1)}|\n`; | ||
| }; | ||
|
|
||
| module.exports = createChristmasTree; |
30 changes: 30 additions & 0 deletions
2023/10-crea-tu-propio-arbol-de-navidad/index.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| const createChristmasTree = require('./index'); | ||
|
|
||
| describe('10 => Crea tu propio arbol de navidad', () => { | ||
| const testCases = [ | ||
| { | ||
| input: ['x', 3], | ||
| output: ' x\n x x\nx x x\n |\n', | ||
| }, | ||
| { | ||
| input: ['xo', 4], | ||
| output: ' x\n o x\n o x o\nx o x o\n |\n', | ||
| }, | ||
| { | ||
| input: ['123', 5], | ||
| output: ' 1\n 2 3\n 1 2 3\n 1 2 3 1\n2 3 1 2 3\n |\n', | ||
| }, | ||
| { | ||
| input: ['*@o', 3], | ||
| output: ' *\n @ o\n* @ o\n |\n', | ||
| }, | ||
| ]; | ||
|
|
||
| it('should return a string type', () => { | ||
| expect(typeof createChristmasTree('x', 3)).toBe('string'); | ||
| }); | ||
|
|
||
| it.each(testCases)('should return the correct output', (testCase) => { | ||
| expect(createChristmasTree(...testCase.input)).toBe(testCase.output); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.