-
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
1 parent
da99e2e
commit 37e36a5
Showing
2 changed files
with
62 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,37 @@ | ||
/* es un evaluador de cadenas de parentesis */ | ||
/* recibe un string con parentesis y si los parentesis cierran correctamente devuelve true, como un analizador sintactico */ | ||
|
||
// isValidParentheses("()") // Output: true | ||
// isValidParentheses("()[]{}") // Output: true | ||
// isValidParentheses("(]") // Output: false | ||
// isValidParentheses("([)]") // Output: false | ||
// isValidParentheses("{[]}") // Output: true | ||
// isValidParentheses("[") // Output: false | ||
|
||
export const isValidParentheses = (str: string) => { | ||
const stack = []; | ||
|
||
// Creamos un mapa para mapear paréntesis de apertura a cierre | ||
const map = { | ||
'(': ')', | ||
'[': ']', | ||
'{': '}', | ||
}; | ||
|
||
// Recorremos cada carácter en la cadena de entrada | ||
for (const char of str) { | ||
// Si el carácter es un paréntesis de apertura, lo agregamos a la pila | ||
if (map[char]) { | ||
stack.push(char); | ||
} else { | ||
// Si el carácter es un paréntesis de cierre | ||
// verificamos si la pila está vacía o si el último paréntesis abierto no coincide | ||
if (stack.length === 0 || map[stack.pop()] !== char) { | ||
return false; // No coinciden los paréntesis | ||
} | ||
} | ||
} | ||
|
||
// Si la pila está vacía al final, todos los paréntesis se cerraron correctamente | ||
return stack.length === 0; | ||
}; |
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,25 @@ | ||
import { describe, test } from 'vitest'; | ||
|
||
import { isValidParentheses } from '../parentheses'; | ||
|
||
describe('Parentheses', () => { | ||
test('isValidParentheses function is defined', () => { | ||
expect(typeof isValidParentheses).toEqual('function'); | ||
}); | ||
|
||
test('"()" is a valid parentheses', () => { | ||
expect(isValidParentheses('()')).toBeTruthy(); | ||
}); | ||
|
||
test('"([)]" is not a valid parentheses', () => { | ||
expect(isValidParentheses('([)]')).toBeFalsy(); | ||
}); | ||
|
||
test('"[" is not a valid parentheses', () => { | ||
expect(isValidParentheses('[')).toBeFalsy(); | ||
}); | ||
|
||
test('"()[]{}" is a valid parentheses', () => { | ||
expect(isValidParentheses('()[]{}')).toBeTruthy(); | ||
}); | ||
}); |