This file contains hidden or 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 reverse(s) { | |
return s.split("").reverse().join(""); | |
} | |
const engAlphabets = "abcdefghijklmnopqrstuvwxyz".repeat(3); | |
const reverseEngAlphabets = reverse("abcdefghijklmnopqrstuvwxyz").repeat(3); | |
const engAlIndexes = { | |
a: 0, | |
b: 1, | |
c: 2, | |
d: 3, |
This file contains hidden or 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 makeId(length) { | |
let result = ""; | |
const characters = | |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | |
const charactersLength = characters.length; | |
for (let i = 0; i < length; i++) { | |
result += characters.charAt(Math.floor(Math.random() * charactersLength)); | |
} | |
return result; | |
} |
This file contains hidden or 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
//get the nth number in fibonacci sequence, base case is 0 and 1. | |
//solution 1, easier to understand but it's not good. It has a time complexity of O(2^n). | |
function fib(num) { | |
if (num === 2) return 1; | |
if (num === 1) return 0; | |
return fib(num - 1) + fib(num - 2); | |
} | |
fib(6) //returns 5 |
This file contains hidden or 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
{ | |
"Status": "Connected", | |
"Adminstration Control": { | |
"Connection": "stable", | |
//Total number 100. 70 means adminstration can control most of stuff of that device, but he is restricted on some of the part. | |
//will need to think more about it | |
"Permission": "70" //rank system. 70 out of 100 | |
}, | |
"Device": { |
This file contains hidden or 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
//get the nth number in fibonacci sequence, base case is 0 and 1. | |
//solution 1, easier to understand but it's not good. It has a time complexity of O(2^n). | |
function fib(num) { | |
if (num === 2) return 1; | |
if (num === 1) return 0; | |
return fib(num - 1) + fib(num - 2); | |
} | |
fib(6) //returns 5 |
This file contains hidden or 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
// Get Fibonacci Sequence in an Array, base case is 0 and 1. | |
const fibSequence = (num) => { | |
if(num === 0) return 0; | |
const arr=[0,1]; | |
let counter=2; | |
while(counter <=num){ | |
arr[counter]=arr[counter -1] + arr[counter -2] | |
counter ++ | |
} |
This file contains hidden or 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
//Solution 1 | |
const isValidBST = function(root, min = Number.MIN_SAFE_INTEGER, max = Number.MAX_SAFE_INTEGER) { | |
if(root == null) return true | |
if(root.val >= max || root.val <= min) return false | |
return isValidBST(root.left, min, root.val) && isValidBST(root.right, root.val, max) | |
}; | |
This file contains hidden or 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
//Dijkstra algorithm is used to find the shortest distance between two nodes inside a valid weighted graph. Often used in Google Maps, Network Router etc. | |
//helper class for PriorityQueue | |
class Node { | |
constructor(val, priority) { | |
this.val = val; | |
this.priority = priority; | |
} | |
} |
This file contains hidden or 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
//Helper class. To get the most prioritize result out | |
class PriorityQueue { | |
constructor() { | |
this.values = []; | |
} | |
enqueue(val, priority) { | |
this.values.push({ val, priority }); | |
this.sort(); | |
} | |
dequeue() { |
This file contains hidden or 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
In graph theory, a tree is an undirected graph in which any two vertices are connected by exactly one path, | |
or equivalently a connected acyclic undirected graph. | |
Which means, in a graph, we can reach a certain node in many different ways, but in a tree there should be only ONE way to reach that node. |