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
function fromRoman(str) { | |
let result = 0; | |
// the result is now a number, not a string | |
const decimal = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]; | |
const roman = ["M", "CM","D","CD","C", "XC", "L", "XL", "X","IX","V","IV","I"]; | |
for (let i = 0;i<=decimal.length;i++) { | |
while (str.indexOf(roman[i]) === 0){ | |
result += decimal[i]; | |
str = str.replace(roman[i],''); | |
} |
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
function toRoman(num) { | |
let result = ''; | |
const decimal = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]; | |
const roman = ["M", "CM","D","CD","C", "XC", "L", "XL", "X","IX","V","IV","I"]; | |
for (let i = 0;i<=decimal.length;i++) { | |
while (num%decimal[i] < num) { | |
result += roman[i]; | |
num -= decimal[i]; | |
} | |
} |
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
const deepEqual = function (x, y) { | |
if (x === y) { | |
return true; | |
} | |
else if ((typeof x == "object" && x != null) && (typeof y == "object" && y != null)) { | |
if (Object.keys(x).length != Object.keys(y).length) | |
return false; | |
for (let prop in x) { | |
if (y.hasOwnProperty(prop)) |
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
const arr1 = [3, 5, 6, 10, 11, 18, 21]; | |
const arr2 = [1, 2, 7, 8, 15, 19]; | |
// O(n log n) | |
function mergeTwo(arr1, arr2) { | |
let result = [...arr1, ...arr2]; | |
return result.sort((a,b) => a-b); | |
} | |
console.log(mergeTwo(arr1, arr2)); |
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
// LRU CACHE | |
class Node { | |
constructor(key, value, next = null, prev = null) { | |
this.key = key; | |
this.value = value; | |
this.next = next; | |
this.prev = prev; | |
} | |
} |
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
function isPalindrome(text) { | |
const reversedText = text.toLowerCase() | |
.split('').reverse().join('') | |
return text === reversedText | |
} | |
console.log(palindromeChecker('racecar')); | |
console.log(palindromeChecker('notapalindrome')); |
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
/** | |
* @param {string} ransomNote | |
* @param {string} magazine | |
* @return {boolean} | |
*/ | |
const canConstruct = function (ransomNote, magazine) { | |
const asciiPossible = 'abcdefghijklmnopqrstuvwxyz'; | |
if(ransomNote.length > magazine.length){ | |
return false |
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
// Using Set | |
function uniqArray(a) { | |
return [...new Set(a)]; | |
} | |
console.log(uniqArray([])) | |
console.log(uniqArray([1, 2, 3, 4])) | |
console.log(uniqArray([1, 2, 1, 3, 2, 4])); | |
console.log(uniqArray([1, 1, 1])); | |
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
// FIBONACCI RECURSION O(2^N) | |
function fibonacciRecursion(num) { | |
if (num <= 1) return 1; | |
return fibonacci(num - 1) + fibonacci(num - 2); | |
} | |
// FIBONACCI LOOP O(N) | |
function fibonacciLoop(n) { | |
let a = 1, b = 0, temp; |
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
function listMissingLetters(str) { | |
let alphabets = "abcdefghijklmnopqrstuvwxyz"; | |
let missingChars = ''; | |
for(let i = 0; i < alphabets.length; i++){ | |
if(!str.includes(alphabets[i])){ | |
missingChars += alphabets[i]; | |
} | |
} |
NewerOlder