From 814fe772226e20a5bd07e14ca05df45eed1729e4 Mon Sep 17 00:00:00 2001 From: user Date: Tue, 8 Aug 2023 19:26:18 +0100 Subject: [PATCH] new commit --- src/brackets/index.js | 37 +++++++++++++++++++++++++++++++++++-- src/roman-numerals/index.js | 32 ++++++++++++++++++++++++++++++-- src/transpose/index.js | 7 +++++-- 3 files changed, 70 insertions(+), 6 deletions(-) diff --git a/src/brackets/index.js b/src/brackets/index.js index 96b6766..51c1a33 100644 --- a/src/brackets/index.js +++ b/src/brackets/index.js @@ -4,6 +4,39 @@ * @param {string} str The string of brackets. * @returns {"valid" | "invalid"} Whether or not the string is valid. */ -function isValid(str) {} -module.exports = isValid; +function isValid(str) { + let stack = []; + // For each char in the bracket + for(let i = 0; i < str.length; i++) { + let x = str[i]; + + if( x == '[' || x == '(' || x == '{' ) { + stack.push(x); + continue; + } + + if (stack.length === 0 ) { + return "invalid" + } + + const top = stack.pop(); + if( x == ')' && top != '(' ) { + return "invalid"; + } + if( x == ']' && top != '[' ) { + return "invalid"; + } + if( x == '}' && top != '{' ) { + return "invalid"; + } + + } + + return stack.length === 0 ? "valid" : "invalid"; + + }; + + console.log(isValid("]]")); + + module.exports = isValid; \ No newline at end of file diff --git a/src/roman-numerals/index.js b/src/roman-numerals/index.js index 38afb19..d3595eb 100644 --- a/src/roman-numerals/index.js +++ b/src/roman-numerals/index.js @@ -4,6 +4,34 @@ * @param {string} roman The all-caps Roman numeral between 1 and 3999 (inclusive). * @returns {number} The decimal equivalent. */ -function romanToDecimal(roman) {} -module.exports = romanToDecimal; + +function romanToDecimal(roman) { + let romanToDecimal = { + I : 1, + V : 5, + X : 10, + L : 50, + C : 100, + D : 500, + M : 1000, + } + let result = 0; + + for( let i = 0; i < roman.length; i++){ + let currSym = romanToDecimal[roman[i]]; + let nextSym = romanToDecimal[roman[i + 1]]; + + if(nextSym && nextSym > currSym) { + result += nextSym - currSym + i++ + } else { + result += currSym + } + + }; + return result +}; + + +module.exports = romanToDecimal; \ No newline at end of file diff --git a/src/transpose/index.js b/src/transpose/index.js index adec201..43ce221 100644 --- a/src/transpose/index.js +++ b/src/transpose/index.js @@ -4,6 +4,9 @@ * @param {number[]} array The array to transpose * @returns {number[]} The transposed array */ -function transpose(array) {} -module.exports = transpose; +function transpose(array) { + return array[0].map((item, index) => array.map((cur) => cur[index])); +}; + +module.exports = transpose; \ No newline at end of file