Skip to content

Instantly share code, notes, and snippets.

View FelixLuciano's full-sized avatar
🔭
Discovering

Luciano Felix FelixLuciano

🔭
Discovering
View GitHub Profile
@FelixLuciano
FelixLuciano / range.js
Created January 26, 2021 08:53
Javascript implementation of Python's range
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)]
@FelixLuciano
FelixLuciano / rotate90.js
Last active January 25, 2021 17:40
rendezvous with cassidoo #180 - Interview question of the week
/* 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]
@FelixLuciano
FelixLuciano / hashmap.js
Created January 18, 2021 23:52
rendezvous with cassidoo #179 - Interview question of the week
//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
@FelixLuciano
FelixLuciano / to-romans.js
Created January 16, 2021 08:42
A function that takes in a number from 1 to 1000 and returns that number in Roman Numerals.
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
}
@FelixLuciano
FelixLuciano / can-toggle.js
Created January 16, 2021 05:53
You’re trying to build an IoT mesh network. Signals can only travel the maximum of 5 units. You’re given coordinates for the switch, the light, and the mesh hubs (which capture and forward signals). Return true if the switch can successfully toggle the light.
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)
})
}
@FelixLuciano
FelixLuciano / largest-rect.js
Last active January 16, 2021 05:51
Given a n x m binary matrix filled with 0s and 1s, find the largest rectangle containing only 1s and return its area.
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))
}
}
@FelixLuciano
FelixLuciano / calculate-bhaskara.js
Last active July 17, 2020 17:01
Reads a second degree equation in a string and calculates its root using the Bhaskara formula
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)
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
@FelixLuciano
FelixLuciano / single-div-spinner.js
Last active June 17, 2020 00:11
Single div Spinner SFC
"use strict"
class SingleDivSpinner extends HTMLDivElement {
get value () {
return parseInt(this.style.getPropertyValue('--progress'))
}
set value (newValue) {
const animate = (value) => {
value += (newValue - value) / 4
@FelixLuciano
FelixLuciano / scroll-transform.js
Last active May 27, 2020 16:28
Scroll Transform 💨
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