-
Notifications
You must be signed in to change notification settings - Fork 0
✨ Add challenge-22 solution #38
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
70 changes: 70 additions & 0 deletions
2024/22-genera-combinaciones-de-regalos/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,70 @@ | ||
| # Reto 22: Genera-combinaciones-de-regalos | ||
|
|
||
| Santa Claus 🎅 está revisando una **lista de juguetes únicos que podría incluir en su bolsa mágica de regalos.** Quiere explorar todas las combinaciones posibles de juguetes. Quiere ver todas las combinaciones que realmente contengan al menos un juguete. | ||
|
|
||
| Tu tarea es escribir una función que, dado un array de juguetes, **devuelva todas las combinaciones posibles.** | ||
|
|
||
| Importante: Debes devolverlo en el orden que aparecen los juguetes y de combinaciones de 1 a n juguetes. | ||
|
|
||
| ```js | ||
| generateGiftSets(['car', 'doll', 'puzzle']) | ||
| // [ | ||
| // ['car'], | ||
| // ['doll'], | ||
| // ['puzzle'], | ||
| // ['car', 'doll'], | ||
| // ['car', 'puzzle'], | ||
| // ['doll', 'puzzle'], | ||
| // ['car', 'doll', 'puzzle'] | ||
| // ] | ||
|
|
||
| generateGiftSets(['ball']) | ||
| // [ | ||
| // ['ball'] | ||
| // ] | ||
|
|
||
| generateGiftSets(['game', 'pc']) | ||
| // [ | ||
| // ['game'], | ||
| // ['pc'], | ||
| // ['game', 'pc'] | ||
| // ] | ||
| ``` | ||
|
|
||
| **Nota: En el array de entrada siempre habrá al menos un juguete y nunca habrá juguetes duplicados.** | ||
|
|
||
| **Consejo**: Hay muchas formas de solucionar este problema, pero el backtracking puede ser una buena opción. 😉 | ||
|
|
||
| ## Mi solución explicada | ||
|
|
||
| ```js | ||
| /* eslint-disable no-inner-declarations */ | ||
| function generateGiftSets(gifts) { | ||
| const results = []; | ||
| function backtracking(start, currentSet) { | ||
| if (currentSet.length > 0) { | ||
| results.push([...currentSet]); | ||
| } | ||
|
|
||
| for (let i = start; i < gifts.length; i++) { | ||
| currentSet.push(gifts[i]); | ||
| backtracking(i + 1, currentSet); | ||
| currentSet.pop(); | ||
| } | ||
| } | ||
|
|
||
| backtracking(0, []); | ||
|
|
||
| return results.sort((a, b) => a.length - b.length); | ||
| } | ||
| ``` | ||
|
|
||
| Para poder resolver este problema, utilizaremos un enfoque de backtracking. A grandes rasgos, el backtracking es una técnica que consiste en probar todas las posibles combinaciones de una solución, y si en algún momento nos damos cuenta de que no es posible llegar a una solución válida, retrocedemos y probamos otra combinación. | ||
|
|
||
| En este caso, la función `generateGiftSets` recibe un array de juguetes y devuelve todas las combinaciones posibles. Para ello, creamos un array `results` que almacenará todas las combinaciones válidas. | ||
|
|
||
| Dentro de la función `generateGiftSets`, definimos una función interna llamada `backtracking` que se encargará de generar las combinaciones. Esta función recibe dos parámetros: `start` (que indica la posición inicial desde la que se deben considerar los juguetes) y `currentSet` (que es el conjunto actual de juguetes que estamos evaluando). | ||
|
|
||
| En la función `backtracking`, primero verificamos si el conjunto actual `currentSet` tiene al menos un juguete, y si es así, lo agregamos al array `results`. Luego, recorremos los juguetes a partir de la posición `start` y vamos generando las combinaciones de forma recursiva. Para ello, agregamos un juguete al conjunto actual, llamamos a la función `backtracking` con la siguiente posición y el conjunto actual, y luego eliminamos el último juguete agregado para probar con el siguiente. De esta forma, vamos generando todas las combinaciones posibles. | ||
|
|
||
| Finalmente, retornamos el array `results` ordenado por la longitud de cada combinación, de menor a mayor. |
19 changes: 19 additions & 0 deletions
2024/22-genera-combinaciones-de-regalos/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,19 @@ | ||
| function generateGiftSets(gifts) { | ||
| const results = []; | ||
| function backtracking(start, currentSet) { | ||
| if (currentSet.length > 0) { | ||
| results.push([...currentSet]); | ||
| } | ||
|
|
||
| for (let i = start; i < gifts.length; i++) { | ||
| currentSet.push(gifts[i]); | ||
| backtracking(i + 1, currentSet); | ||
| currentSet.pop(); | ||
| } | ||
| } | ||
|
|
||
| backtracking(0, []); | ||
|
|
||
| return results.sort((a, b) => a.length - b.length); | ||
| } | ||
| module.exports = generateGiftSets; |
99 changes: 99 additions & 0 deletions
2024/22-genera-combinaciones-de-regalos/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,99 @@ | ||
| const generateGiftSets = require('./index'); | ||
|
|
||
| describe('22 => Genera-combinaciones-de-regalos', () => { | ||
| const TEST_CASES = [ | ||
| { | ||
| input: ['car'], | ||
| output: [['car']], | ||
| }, | ||
| { | ||
| input: ['car', 'doll', 'puzzle'], | ||
| output: [ | ||
| ['car'], | ||
| ['doll'], | ||
| ['puzzle'], | ||
| ['car', 'doll'], | ||
| ['car', 'puzzle'], | ||
| ['doll', 'puzzle'], | ||
| ['car', 'doll', 'puzzle'], | ||
| ], | ||
| }, | ||
| { | ||
| input: ['car', 'doll'], | ||
| output: [['car'], ['doll'], ['car', 'doll']], | ||
| }, | ||
| { | ||
| input: ['apple', 'banana', 'cherry', 'date'], | ||
| output: [ | ||
| ['apple'], | ||
| ['banana'], | ||
| ['cherry'], | ||
| ['date'], | ||
| ['apple', 'banana'], | ||
| ['apple', 'cherry'], | ||
| ['apple', 'date'], | ||
| ['banana', 'cherry'], | ||
| ['banana', 'date'], | ||
| ['cherry', 'date'], | ||
| ['apple', 'banana', 'cherry'], | ||
| ['apple', 'banana', 'date'], | ||
| ['apple', 'cherry', 'date'], | ||
| ['banana', 'cherry', 'date'], | ||
| ['apple', 'banana', 'cherry', 'date'], | ||
| ], | ||
| }, | ||
| { | ||
| input: ['pen', 'notebook', 'eraser', 'pencil', 'marker'], | ||
| output: [ | ||
| ['pen'], | ||
| ['notebook'], | ||
| ['eraser'], | ||
| ['pencil'], | ||
| ['marker'], | ||
| ['pen', 'notebook'], | ||
| ['pen', 'eraser'], | ||
| ['pen', 'pencil'], | ||
| ['pen', 'marker'], | ||
| ['notebook', 'eraser'], | ||
| ['notebook', 'pencil'], | ||
| ['notebook', 'marker'], | ||
| ['eraser', 'pencil'], | ||
| ['eraser', 'marker'], | ||
| ['pencil', 'marker'], | ||
| ['pen', 'notebook', 'eraser'], | ||
| ['pen', 'notebook', 'pencil'], | ||
| ['pen', 'notebook', 'marker'], | ||
| ['pen', 'eraser', 'pencil'], | ||
| ['pen', 'eraser', 'marker'], | ||
| ['pen', 'pencil', 'marker'], | ||
| ['notebook', 'eraser', 'pencil'], | ||
| ['notebook', 'eraser', 'marker'], | ||
| ['notebook', 'pencil', 'marker'], | ||
| ['eraser', 'pencil', 'marker'], | ||
| ['pen', 'notebook', 'eraser', 'pencil'], | ||
| ['pen', 'notebook', 'eraser', 'marker'], | ||
| ['pen', 'notebook', 'pencil', 'marker'], | ||
| ['pen', 'eraser', 'pencil', 'marker'], | ||
| ['notebook', 'eraser', 'pencil', 'marker'], | ||
| ['pen', 'notebook', 'eraser', 'pencil', 'marker'], | ||
| ], | ||
| }, | ||
| { | ||
| input: [], | ||
| output: [], | ||
| }, | ||
| ]; | ||
|
|
||
| it('should return array type', () => { | ||
| const result = generateGiftSets(['car']); | ||
| expect(Array.isArray(result)).toBe(true); | ||
| }); | ||
|
|
||
| it.each(TEST_CASES)( | ||
| 'should return an array with $output.length elements', | ||
| ({ input, output }) => { | ||
| const result = generateGiftSets(input); | ||
| expect(result).toHaveLength(output.length); | ||
| }, | ||
| ); | ||
| }); |
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
Oops, something went wrong.
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.