-
Notifications
You must be signed in to change notification settings - Fork 0
✨ Add challenge-11 solution #29
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
3 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
89 changes: 89 additions & 0 deletions
2024/11-nombres-de-archivos-codificados/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,89 @@ | ||
| # Reto 11: Nombres-de-archivos-codificados | ||
|
|
||
| **El Grinch ha hackeado 🏴☠️ los sistemas del taller de Santa Claus** y ha codificado los nombres de todos los archivos importantes. Ahora los elfos no pueden encontrar los archivos originales y necesitan tu ayuda para descifrar los nombres. | ||
|
|
||
| Cada archivo sigue este formato: | ||
|
|
||
| - Comienza con un número (puede contener cualquier cantidad de dígitos). | ||
| - Luego tiene un guion bajo `_`. | ||
| - Continúa con un **nombre de archivo y su extensión.** | ||
| - Finaliza con una extensión extra al final (que no necesitamos). | ||
|
|
||
| Ten en cuenta que el nombre de los archivos pueden contener letras (a-z, A-Z), números (0-9), **otros guiones bajos** (_) y guiones (-). | ||
|
|
||
| Tu tarea es implementar una función que reciba un string con el nombre de un archivo codificado y devuelva solo la parte importante: **el nombre del archivo y su extensión.** | ||
|
|
||
| Ejemplos: | ||
|
|
||
| ```js | ||
| decodeFilename('2023122512345678_sleighDesign.png.grinchwa') | ||
| // ➞ "sleighDesign.png" | ||
|
|
||
| decodeFilename('42_chimney_dimensions.pdf.hack2023') | ||
| // ➞ "chimney_dimensions.pdf" | ||
|
|
||
| decodeFilename('987654321_elf-roster.csv.tempfile') | ||
| // ➞ "elf-roster.csv" | ||
| ``` | ||
|
|
||
| ## Mi solución explicada | ||
|
|
||
| ```js | ||
| function decodeFilename(filename) { | ||
| const underscoreIndex = filename.indexOf('_'); | ||
| const lastDotIndex = filename.lastIndexOf('.'); | ||
| return filename.slice(underscoreIndex + 1, lastDotIndex); | ||
| } | ||
| ``` | ||
|
|
||
| Para resolver este reto, de manera sencilla, he utilizado el método `slice()` para extraer la parte del string que necesitamos. ¿Como es que funciona `slice()`? Lo que hace es devolver una copia de una parte del string, desde el índice inicial hasta el índice final (sin incluirlo). Ejemplo: | ||
|
|
||
| ```js | ||
| const str = 'Hello, World!'; | ||
| console.log(str.slice(7, 12)); // "World" | ||
| ``` | ||
|
|
||
| En este caso, `slice(7, 12)` extrae la parte del string desde el índice 7 hasta el índice 12 (sin incluirlo), devolviendo `"World"`. | ||
|
|
||
| Ahora, para resolver este reto, he utilizado `slice()` de la siguiente manera: | ||
|
|
||
| Primero, obtengo el índice del guion bajo `_` con el método `indexOf()`. ¿Por qué el primer guion bajo? Porque el número que está al principio del string es lo que no necesitamos. | ||
|
|
||
| Luego, obtengo el índice del último punto `.` con el método `lastIndexOf()`. ¿Por qué el último punto? Porque el punto que está al final del string es lo que no necesitamos. | ||
|
|
||
| Finalmente, utilizo el método `slice()` para extraer la parte del string que necesitamos. | ||
|
|
||
| Veamos con un ejemplo: | ||
|
|
||
| Supongamos que tengo la siguiente cadena: | ||
|
|
||
| ```js | ||
| const filename = '2023122512345678_sleighDesign.png.grinchwa'; | ||
| ``` | ||
|
|
||
| Primero, obtengo el índice del primer guion bajo `_` con el método `indexOf()`: | ||
|
|
||
| ```js | ||
| // const underscoreIndex = '2023122512345678_sleighDesign.png.grinchwa'.indexOf('_'); | ||
| const underscoreIndex = filename.indexOf('_'); // 16 | ||
| ``` | ||
|
|
||
| Luego, obtengo el índice del último punto `.` con el método `lastIndexOf()`: | ||
|
|
||
| ```js | ||
| // const lastDotIndex = '2023122512345678_sleighDesign.png.grinchwa'.lastIndexOf('.'); | ||
| const lastDotIndex = filename.lastIndexOf('.'); // 33 | ||
| ``` | ||
|
|
||
| Finalmente, utilizo el método `slice()` para extraer la parte del string que necesitamos: | ||
|
|
||
| ```js | ||
| // return '2023122512345678_sleighDesign.png.grinchwa'.slice(underscoreIndex + 1, lastDotIndex); | ||
| // return '2023122512345678_sleighDesign.png.grinchwa'.slice(16 + 1, 33); | ||
| // return '2023122512345678_sleighDesign.png.grinchwa'.slice(17, 33); | ||
| return filename.slice(underscoreIndex + 1, lastDotIndex); // "sleighDesign.png" | ||
| ``` | ||
|
|
||
| Y eso es todo lo que necesitamos para resolver este reto. 🎉 | ||
|
|
||
| **Igual podemos hacerlo con expresiones regulares, pero esta solución es más sencilla y fácil de entender.** |
7 changes: 7 additions & 0 deletions
2024/11-nombres-de-archivos-codificados/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,7 @@ | ||
| function decodeFilename(filename) { | ||
| const underscoreIndex = filename.indexOf('_'); | ||
| const lastDotIndex = filename.lastIndexOf('.'); | ||
| return filename.slice(underscoreIndex + 1, lastDotIndex); | ||
| } | ||
|
|
||
| module.exports = decodeFilename; |
35 changes: 35 additions & 0 deletions
2024/11-nombres-de-archivos-codificados/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,35 @@ | ||
| const decodeFilename = require('./index'); | ||
|
|
||
| describe('11 => Nombres-de-archivos-codificados', () => { | ||
| const TEST_CASES = [ | ||
| { | ||
| input: '2023122512345678_sleighDesign.png.grinchwa', | ||
| output: 'sleighDesign.png', | ||
| }, | ||
| { | ||
| input: '42_chimney_dimensions.pdf.hack2023', | ||
| output: 'chimney_dimensions.pdf', | ||
| }, | ||
| { | ||
| input: '987654321_elf-roster.csv.tempfile', | ||
| output: 'elf-roster.csv', | ||
| }, | ||
| { | ||
| input: '2024120912345678_magic_wand-blueprint.jpg.grinchbackup', | ||
| output: 'magic_wand-blueprint.jpg', | ||
| }, | ||
| { | ||
| input: '51231_trainSchedule.txt.extra', | ||
| output: 'trainSchedule.txt', | ||
| }, | ||
| ]; | ||
|
|
||
| it('should return string type', () => { | ||
| const testCase = TEST_CASES[0]; | ||
| expect(typeof decodeFilename(testCase.input)).toBe('string'); | ||
| }); | ||
|
|
||
| it.each(TEST_CASES)('should return $output', ({ input, output }) => { | ||
| expect(decodeFilename(input)).toBe(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
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.