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
28 changes: 27 additions & 1 deletion src/brackets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,32 @@
* @param {string} str The string of brackets.
* @returns {"valid" | "invalid"} Whether or not the string is valid.
*/
function isValid(str) {}
function isValid(str) {
const openBrackets = [];

if(str.charAt(0) == '}' || str.charAt(0) == ')' || str.charAt(0) == ']'){
return 'invalid';
}else {
for(let bracket of str){
if(bracket == '{' || bracket == '(' || bracket == '['){
openBrackets.push(bracket);
}

let lastElement = openBrackets.slice(-1);

if(bracket == '}' && lastElement == '{'){
openBrackets.pop();
}else if(bracket == ')' && lastElement == '('){
openBrackets.pop();
}else if(bracket == ']' && lastElement == '['){
openBrackets.pop();
}
// else {
// return 'invalid'
// }
}
}
return openBrackets.length > 0 ? 'invalid' : 'valid';
}

module.exports = isValid;
36 changes: 36 additions & 0 deletions src/brackets/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Implement the solution to brackets
*
* @param {string} str The string of brackets.
* @returns {"valid" | "invalid"} Whether or not the string is valid.
*/
function isValid(str : string) {

const openBrackets : string[] = [];

if(str.charAt(0) == '}' || str.charAt(0) == ')' || str.charAt(0) == ']'){
return 'invalid';
}else {
for(let bracket of str){
if(bracket == '{' || bracket == '(' || bracket == '['){
openBrackets.push(bracket);
}

let lastElement = openBrackets[openBrackets.length - 1];

if(bracket == '}' && lastElement == '{'){
openBrackets.pop();
}else if(bracket == ')' && lastElement == '('){
openBrackets.pop();
}else if(bracket == ']' && lastElement == '['){
openBrackets.pop();
}
// else {
// return 'invalid'
// }
}
}
return openBrackets.length > 0 ? 'invalid' : 'valid';
}

module.exports = isValid;
24 changes: 23 additions & 1 deletion src/roman-numerals/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,28 @@
* @param {string} roman The all-caps Roman numeral between 1 and 3999 (inclusive).
* @returns {number} The decimal equivalent.
*/
function romanToDecimal(roman) {}
function romanToDecimal(roman) {

const romanNumerial = {'I' : 1, 'V' : 5, 'X' : 10, 'L' : 50, 'C' : 100, 'D' : 500, 'M' : 1000}

let lastStrIndex = roman.length - 1
let decimal = romanNumerial[roman[lastStrIndex]];

// Looping from back
for(let i = lastStrIndex; i >= 0; i--){

let current = romanNumerial[roman[i]];
let next = romanNumerial[roman[i - 1]];

if( next >= current){
decimal += next;
}else if(next < current){
decimal -= next;
}
}

return decimal;

}

module.exports = romanToDecimal;
31 changes: 31 additions & 0 deletions src/roman-numerals/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Implement the solution in this function
*
* @param {string} roman The all-caps Roman numeral between 1 and 3999 (inclusive).
* @returns {number} The decimal equivalent.
*/
function romanToDecimal(roman:string) {

const romanNumerial: any = {'I' : 1, 'V' : 5, 'X' : 10, 'L' : 50, 'C' : 100, 'D' : 500, 'M' : 1000}

let lastStrIndex: number = roman.length - 1
let decimal: any = romanNumerial[roman[lastStrIndex]];

// Looping from back
for(let i = lastStrIndex; i >= 0; i--){

let current: any = romanNumerial[roman[i]];
let next:any = romanNumerial[roman[i - 1]];

if( next >= current){
decimal += next;
}else if(next < current){
decimal -= next;
}
}

return decimal;

}

module.exports = romanToDecimal;
17 changes: 16 additions & 1 deletion src/transpose/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@
* @param {number[]} array The array to transpose
* @returns {number[]} The transposed array
*/
function transpose(array) {}
function transpose(array) {
const newArr = [];

for(let i = 0; i < array[0].length; i++){
newArr.push([]);
}

for(let i = 0; i < array[0].length; i++){
for(let j=0; j < array.length; j++){

newArr[i].push(array[j][i])

}
}
return newArr;
}

module.exports = transpose;
25 changes: 25 additions & 0 deletions src/transpose/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Implement the solution in this function
*
* @param {number[]} array The array to transpose
* @returns {number[]} The transposed array
*/
function transpose(array: number[][]) {

const newArr: number[][] = [];

for(let i = 0; i < array[0].length; i++){
newArr.push([]);
}

for(let i = 0; i < array[0].length; i++){
for(let j=0; j < array.length; j++){

newArr[i].push(array[j][i])

}
}
return newArr;
}

module.exports = transpose;