From f16cd04a3ca1e3e6244cabe5a493c1e176b96805 Mon Sep 17 00:00:00 2001 From: ANYIDOLLAR Date: Mon, 7 Aug 2023 21:33:26 +0100 Subject: [PATCH] this is my first mini-challenge --- .DS_Store | Bin 0 -> 6148 bytes src/.DS_Store | Bin 0 -> 6148 bytes src/brackets/index.js | 31 ++++++++++++++++++++++++++++++- src/roman-numerals/index.js | 31 ++++++++++++++++++++++++++++++- src/transpose/index.js | 21 +++++++++++++++++++-- 5 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 .DS_Store create mode 100644 src/.DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..758ef57321ab97d7f44e53ea0868ad8f929a5bee GIT binary patch literal 6148 zcmeHK%Wl&^6upy#)}|B%sYu-*Sz=p4XsbvS8GBol2ZCe@rt)?unJfOHcbKkcJI;!4Jf0W7Qf#iY8R9}Ph>AjWCTAB63>tt zzm=gxbdN?9;;X+zKEx=GC_x0^x?*!QD2?#FG{T+I2siM=gA#9xUeY;c86n;bm*)^) zOTI2V&#?9my#O}oiuxtSuM(pQJ;u~q_NolmdqvZ1WHkB_jgmOao6Vo1Qmx(E+IF^` z9p`=UNlt@2%qLkp9KYq-D=Cxcx*SHY(cltO#*`0GD@f;6}4qMFw@94NSKk#NJhbSK(wdQlzx&QF-)AR0Sa+S(2SUF4}PAl6r zcnz$W+4JXMoF+2my|VW%&PBEm*Uh}>EF?b56QR)K$` z0LKRpiP1M$XjGvCnR)^MYiL%6GXG6rj%Uy}SZG8KOlT@lQ-yhA2u(-3XZ(DFg+@&$ zVICjC%q+|cMX1>^zNg$t_!@0)6|f2{D^S(dI^X|iH^2XvNw#JcunPQF3W!=K=(MpV zbGNQ+j_+Cz=>rlQ`xP1$1(~^yRe`VKeI#Y*^SA)?4Hg>F0<(Vvlnger3j9?C{s4%H BsYCz( literal 0 HcmV?d00001 diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..f1fc30189b7290fad9985c347cfac772990df764 GIT binary patch literal 6148 zcmeHKL2J}N6n?Ya+6hJ3gF-Jtz-!gDEeq01+}49vBYIF-lWx$^bY|IP-NUkwv;8q% z{U!b#{oc$}x2y_M5Gn7$oA1q=_f5!`WF|vIYB(D|BI*&5hBI~|G`}z&XJ4~U$ZP_Y zo8y!UYN&oz*^{+uN&Jrr@UzpD)08qgp_S*CS2o8OzZg&85574=M05+)md?O1bRRxL zT(}u3|Lj+|pZ|uN&+AFxe#s|QSvURuSJm0*Zr<3^TY6i+O+VOq+GNeF9%j?m+ z+8e5A0#$1&`0M$ppt_5yRrS#o69q&8QQ(RSgf&cWUvVCxivps+^;3Y?hZxS71Z+LJ zr2~yS0sy;6J44LBjLeAxCIMTI7=bBI1$wFqM-1iZ$j6~B3D|n{bW-8)p~96_IH4$a zb<7{Ta#D#$X+;51;IaZ6zT4yd|LO0~|CdS969q(pYo&nd9;e4c+)~(E*KUsY+8F*0 q&c=1E#~%~~ZYxGDZ^b)sXUNAK0F!{NM`U2~Bj99^Mils`3Va7_GJDGa literal 0 HcmV?d00001 diff --git a/src/brackets/index.js b/src/brackets/index.js index 96b6766..5590fb7 100644 --- a/src/brackets/index.js +++ b/src/brackets/index.js @@ -4,6 +4,35 @@ * @param {string} str The string of brackets. * @returns {"valid" | "invalid"} Whether or not the string is valid. */ -function isValid(str) {} + +function isValid(str) { + let stack = [] + for(let i in str) { + let x = stack.length - 1 + if(str[i]=="(" || str[i]=="{" || str[i]=="[") { + stack.push(str[i]) + } + else{ + if(str[i]== ")" && stack[x]=="(") { + stack.pop() + } + else if(str[i]=="}" && stack[x]=="{") { + stack.pop() + } + else if(str[i]=="]" && stack[x]=="[") { + stack.pop() + } + else{ + return "invalid" + } + } + } + return stack.length > 0 ? "invalid" : "valid" +} module.exports = isValid; + + + + + diff --git a/src/roman-numerals/index.js b/src/roman-numerals/index.js index 38afb19..9f4d99e 100644 --- a/src/roman-numerals/index.js +++ b/src/roman-numerals/index.js @@ -4,6 +4,35 @@ * @param {string} roman The all-caps Roman numeral between 1 and 3999 (inclusive). * @returns {number} The decimal equivalent. */ -function romanToDecimal(roman) {} + +function romanToDecimal(roman) { + let stack = []; + let total = 0; + + const romanNumerals = { + I: 1, + V: 5, + X: 10, + L: 50, + C: 100, + D: 500, + M: 1000, + }; + + for (let i = 0; i < roman.length; i++) { + const currentSymbol = roman[i]; + const currentValue = romanNumerals[currentSymbol]; + const nextValue = romanNumerals[roman[i + 1]]; + + if (nextValue && currentValue < nextValue) { + total -= currentValue; + } else { + total += currentValue; + } + } + + return total; +} + module.exports = romanToDecimal; diff --git a/src/transpose/index.js b/src/transpose/index.js index adec201..64a49c5 100644 --- a/src/transpose/index.js +++ b/src/transpose/index.js @@ -4,6 +4,23 @@ * @param {number[]} array The array to transpose * @returns {number[]} The transposed array */ -function transpose(array) {} -module.exports = transpose; +function transpose(array) { + const numRows = array.length; + const numCols = array[0].length; + + const transposed = new Array(numCols).fill(null).map(() => new Array(numRows)); + + for (let row = 0; row < numRows; row++) { + for (let col = 0; col < numCols; col++) { + transposed[col][row] = array[row][col]; + } + } + + return transposed; + } + + + + module.exports = transpose; + \ No newline at end of file