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* range(start, stop, step = 1) { | |
if (stop === undefined) stop = start, start = 0; | |
for (let i = start; step > 0 ? i < stop : step < 0 ? i > stop : false; i += step) yield i; | |
} | |
// for (let index of range(0, 10, 1) | |
// Array.from(range(0, 10, 1)) | |
// or | |
// [...range(0, 10, 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
/* Given an n x n array, rotate it 90 degrees without making a new array. | |
* rotate90([[1,2,3], [4,5,6], [7,8,9]]) | |
* -> [[7,4,1], [8,5,2], [9,6,3]] | |
*/ | |
function rotate90(arr, isClockwise = true) { | |
const size = arr.length - 1 | |
for (let y = 0, length = size; y <= size && length >= 0; y++, length--) { | |
for (let x = y; x < length; x++) { | |
let [x1, y1] = [x, y] |
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
//Design a hashmap without using any built-in libraries. You should include the following functions: | |
// - put(key, value): Insert a (key, value) pair into the hashmap. If the value already exists, update the value. | |
// - get(key): Returns the value to which key is mapped, or -1 if this map contains nothing for key. | |
// - remove(key): Remove the mapping for the value in key if it exists. | |
class HashMap { | |
#buffer = [] | |
put(key, value) { | |
let index = 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
Number.prototype.toRomans = function () { | |
let result = "" | |
for (let [i, alpha] of [...this.toString()].reverse().entries()) { | |
let set = "" | |
if (alpha > 0 && alpha <= 4) set += "IXCM"[i].repeat((alpha - 1) % 3 + 1) | |
if (alpha > 3 && alpha < 9) set += "VLD"[i] | |
if (alpha > 5 && alpha <= 9) set += "IXC"[i].repeat(alpha % 3 + 1) | |
if (alpha === "9") set += "XCM"[i] | |
result = set + result | |
} |
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 distance = ([x1, y1], [x2, y2]) => Math.hypot(x1-x2, y1-y2) | |
function canToggle ({ switch:toggle, hub, light }, radius = 5) { | |
if (distance(toggle, light) <= radius) return true | |
if (!hub.length) return false | |
const steps = hub.filter(step => distance(step, toggle) <= radius) | |
return steps.some(step => { | |
const next = hub.filter(point => !point.every((a, i) => a === step[i])) | |
return canToggle({switch:step, hub:next, light}, radius) | |
}) | |
} |
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 largestRect(matrix, match = "1") { | |
let maxArea = 0 | |
for (let y1 of matrix.keys()) { | |
for (let x1 of matrix[y1].keys()) { | |
if (matrix[y1][x1] !== match) continue | |
for (var x2 = x1; matrix[y1][x2 + 1] === match; x2++) continue | |
for (var y2 = y1; matrix[y2 + 1]?.slice(x1, x2 + 1).every(a => a === match); y2++) continue | |
maxArea = Math.max(maxArea, (x2 - x1 + 1) * (y2 - y1 + 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
function calculateBhaskara (a, b, c) { | |
if (a === 0) throw(`the coefficient 'a' cannot be 0 in a second degree equation`) | |
const delta = calculateDelta (a, b, c) | |
const solution = [] | |
if (delta < 0) return solution | |
const sqrtDelta = Math.sqrt(delta) | |
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 rationalize (value) { | |
let numerator = 0 | |
let denominator = 1 | |
while (value != numerator / denominator) { | |
const ratio = numerator / denominator | |
if (ratio < value) numerator++ | |
else if (ratio > value) denominator++ | |
else break |
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
"use strict" | |
class SingleDivSpinner extends HTMLDivElement { | |
get value () { | |
return parseInt(this.style.getPropertyValue('--progress')) | |
} | |
set value (newValue) { | |
const animate = (value) => { | |
value += (newValue - value) / 4 |
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 minmax = (value, min = 0, max = 1) => Math.min(Math.max(value, min), max) | |
function scrollTransform (selector, callback) { | |
const element = document.querySelector(selector) | |
const anchor = document.createElement('div') | |
const scroll = { | |
get offsetOut () { | |
value = 1 - (window.scrollY - anchor.offsetTop) / element.offsetHeight | |