-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,56 @@ | ||
# Reto 2: Mini compilador | ||
|
||
## Problema | ||
|
||
En el mundo del espionaje, se utiliza un lenguaje de codificación con símbolos que realizan operaciones matemáticas simples. | ||
|
||
Tu tarea es crear un mini compilador que interprete y ejecute programas escritos en este lenguaje de símbolos. | ||
|
||
Las operaciones del lenguaje son las siguientes: | ||
|
||
- "#" Incrementa el valor numérico en 1. | ||
- "@" Decrementa el valor numérico en 1. | ||
- "*" Multiplica el valor numérico por sí mismo. | ||
- "&" Imprime el valor numérico actual. | ||
|
||
El valor numérico inicial es 0 y las operaciones deben aplicarse en elorden en que aparecen en la cadena de símbolos. | ||
|
||
**Ejemplos de entrada:** | ||
|
||
- Entrada: "##*&" | ||
- Salida esperada: "4" | ||
- Explicación: Incrementa (1), incrementa (2), multiplica (4), imprime (4). | ||
|
||
- Entrada: "&##&*&@&" | ||
- Salida esperada: "0243" | ||
- Explicación: Imprime (0), incrementa (1), incrementa (2), imprime (2), multiplica (4), imprime (4), decrementa (3), imprime (3). | ||
|
||
**Tu desafío:** | ||
Desarrolla un mini compilador que tome una cadena de texto y devuelva otra cadena de texto con el resultado de ejecutar el programa. | ||
|
||
**Cómo resolverlo** | ||
1. Resuelve el mensaje que encontrarás en este archivo: https://codember.dev/data/message_02.txt | ||
|
||
2. Crea un programa al que le pases como entrada el mensaje anterior. Envía la salida con el comando "submit" en la terminal, por ejemplo así: | ||
submit 024899488 | ||
|
||
## Mi Solución | ||
|
||
```js | ||
const decode = (str) => { | ||
let resultStr = ''; | ||
const operations = { | ||
'#': (r) => ++r, | ||
'@': (r) => --r, | ||
'*': (r) => r * r, | ||
'&': (r) => { | ||
resultStr = resultStr.concat(r.toString()); | ||
return r; | ||
}, | ||
}; | ||
|
||
[...str].reduce((acc, char) => operations[char](acc), 0); | ||
|
||
return resultStr; | ||
}; | ||
``` |
This file contains 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 @@ | ||
&###@&*&###@@##@##&######@@#####@#@#@#@##@@@@@@@@@@@@@@@*&&@@@@@@@@@####@@@@@@@@@#########&#&##@@##@@##@@##@@##@@##@@##@@##@@##@@##@@##@@##@@##@@##@@##@@& |
This file contains 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,18 @@ | ||
const decode = (str) => { | ||
let resultStr = ''; | ||
const operations = { | ||
'#': (r) => ++r, | ||
'@': (r) => --r, | ||
'*': (r) => r * r, | ||
'&': (r) => { | ||
resultStr = resultStr.concat(r.toString()); | ||
return r; | ||
}, | ||
}; | ||
|
||
[...str].reduce((acc, char) => operations[char](acc), 0); | ||
|
||
return resultStr; | ||
}; | ||
|
||
module.exports = decode; |
This file contains 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,31 @@ | ||
const fs = require('fs'); | ||
const decode = require('./solution'); | ||
|
||
describe('decode', () => { | ||
const testsCases = [ | ||
{ | ||
input: '&##&*&@&', | ||
output: '0243', | ||
}, | ||
{ | ||
input: '##*&', | ||
output: '4', | ||
}, | ||
]; | ||
|
||
it('should return a string type', () => { | ||
expect(typeof decode('&')).toBe('string'); | ||
}); | ||
|
||
it.each(testsCases)('should return $output', (testCase) => { | ||
expect(decode(testCase.input)).toBe(testCase.output); | ||
}); | ||
|
||
describe('file callenge', () => { | ||
const expectedOutput = '024899455'; | ||
it(`should return ${expectedOutput}`, () => { | ||
const str = fs.readFileSync('./2023/02-mini-compiler/message_02.txt', 'utf8'); | ||
expect(decode(str)).toBe(expectedOutput); | ||
}); | ||
}); | ||
}); |