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 bfs(graph, root) { | |
// Distance object returned | |
var nodesLen = {}; | |
graph.forEach((_, i) => { | |
nodesLen[i] = Infinity; | |
}); | |
nodesLen[root] = 0; | |
const v = {[root]: true}; |
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 dfs = (graph, root) => { | |
const v = {[root]: true}; | |
const r = [root]; | |
const s = [graph[root]]; | |
while(s.length) { | |
let cur = s.pop(); | |
cur.forEach((c) => { | |
if (!v[c]) { |
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
// check if array is sorted | |
function isSorted(arr) { | |
var check = i => | |
i == arr.length - 1 ? true : arr[i] > arr[i + 1] ? false : check(i + 1); | |
return check(0); | |
} | |
// generate a randomly filled array | |
var array = new Array(); | |
(function createArray(size = 5) { | |
array.push(+(Math.random() * 100).toFixed(0)); |
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
var MaxHeap = function() { | |
// change code below this line | |
this.heap = [undefined]; | |
this.insert = (ele) => { | |
var index = this.heap.length; | |
var arr = [...this.heap]; | |
arr.push(ele); | |
while (ele > arr[Math.floor(index / 2)]) { | |
arr[index] = arr[Math.floor(index / 2)]; |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> |
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
// https://www.hackerrank.com/challenges/sherlock-and-anagrams/problem?isFullScreen=true | |
const charIndex = (c) => Math.abs(97 - c.charCodeAt(0)); | |
const newSignature = () => { | |
const ALPHABET = "abcdefghijklmnopqrstuvwxyz"; | |
return ALPHABET.split('').map(_ => 0); | |
} | |
const h = (arr) => arr.join('-'); |
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 modulo (n, p, m){ | |
var result = 1; | |
while(p--) { | |
result = (result * n) % m; | |
} | |
return result; | |
} | |
console.log(modulo(7, 16971, 25777)); |
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
# Quiz : Don't Get Volunteered! | |
# ====================== | |
# As a henchman on Commander Lambda's space station, you're expected to be resourceful, smart, and a quick thinker. It's not easy building a doomsday device and capturing bunnies at the same time, after all! In order to make sure that everyone working for her is sufficiently quick-witted, Commander Lambda has installed new flooring outside the henchman dormitories. It looks like a chessboard, and every morning and evening you have to solve a new movement puzzle in order to cross the floor. That would be fine if you got to be the rook or the queen, but instead, you have to be the knight. Worse, if you take too much time solving the puzzle, you get "volunteered" as a test subject for the LAMBCHOP doomsday device! | |
# To help yourself get to and from your bunk every day, write a function called answer(src, dest) which takes in two parameters: the source square, on which you start, and the destination square, which is where you need to land to solve the puz |
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
matrix = Array.from({length: 8}, (_, row) => { | |
return Array(8).fill(0).map((el, index) => index + (row * 8)); | |
}); |
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
cs = (arr) => { | |
const index = []; | |
const places = Array(arr.length).fill(0); | |
for(let i=0; i<arr.length; i++) index[arr[i]] = index[arr[i]] + 1 || 1; | |
for(let i=0; i<index.length-1; i++) index[i+1] = (index[i] || 0) + (index[i+1] || 0); | |
for(let i=0; i<arr.length; i++) { | |
places[index[ arr[i] ] - 1] = arr[i]; |