Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added src/.DS_Store
Binary file not shown.
31 changes: 30 additions & 1 deletion src/brackets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;





31 changes: 30 additions & 1 deletion src/roman-numerals/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
21 changes: 19 additions & 2 deletions src/transpose/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;