Skip to content

Instantly share code, notes, and snippets.

@rakin92
rakin92 / fromRoman.js
Created September 5, 2019 05:39
converts roman numeral to number.
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],'');
}
@rakin92
rakin92 / toRoman.js
Last active September 5, 2019 05:40
converts number to roman numeral
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];
}
}
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))
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));
// LRU CACHE
class Node {
constructor(key, value, next = null, prev = null) {
this.key = key;
this.value = value;
this.next = next;
this.prev = prev;
}
}
function isPalindrome(text) {
const reversedText = text.toLowerCase()
.split('').reverse().join('')
return text === reversedText
}
console.log(palindromeChecker('racecar'));
console.log(palindromeChecker('notapalindrome'));
/**
* @param {string} ransomNote
* @param {string} magazine
* @return {boolean}
*/
const canConstruct = function (ransomNote, magazine) {
const asciiPossible = 'abcdefghijklmnopqrstuvwxyz';
if(ransomNote.length > magazine.length){
return false
// 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]));
// 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;
function listMissingLetters(str) {
let alphabets = "abcdefghijklmnopqrstuvwxyz";
let missingChars = '';
for(let i = 0; i < alphabets.length; i++){
if(!str.includes(alphabets[i])){
missingChars += alphabets[i];
}
}