Skip to content

Instantly share code, notes, and snippets.

View samelliottdlt's full-sized avatar

Sam Elliott De La Torre Babá samelliottdlt

View GitHub Profile
// leetcode answer for https://leetcode.com/problems/flood-fill/
/**
* @param {number[][]} image
* @param {number} sr
* @param {number} sc
* @param {number} newColor
* @return {number[][]}
*/
var floodFill = function(image, sr, sc, newColor) {
// it is fairly common in web land to need to get some data from a server, then do something else async with that data
// this is accomplished with the switchMap operator
// the switchMap operator takes a function that returns another observable.
// it then subscribes (or 'switches') to the returned observable
this.http.get(uri)
.pipe(
switchMap(response => {
const data = doSomething(response);
const a = [1, [2, 3], [[4, 5]]];
function flatten(arr) {
if(!Array.isArray(arr)) {
return [arr];
}
return arr.reduce((accumulator, current) =>
[...accumulator, ...(flatten(current))], []);
}
function asyncSetInterval(f: any, condition: () => boolean, delay: number) {
return new Promise(((resolve, reject) => {
const id = setInterval(() => {
f();
if (condition()) {
resolve();
clearInterval(id);
}
}, delay);
}
import { swapArrayIndexes, indexOfFirstOccurrence, indexOfLastOccurrence } from 'ArrayHelpers';
function bits(numbers) {
return numbers.map(n => maximizeBinaryNumber(n));
}
function maximizeBinaryNumber(number) {
const binaryValue = Number(number).toString(2).split('');
const firstZero = indexOfFirstOccurence(binaryValue, 0);
const lastOne = indexOfLastOccurence(binaryValue, 1);
// original function
function bruh() {
if( x !== 1) doSomething();
}
// obfuscated function results in something like this
function bruh(){if(x!==1)doSomething()}
// note the lack of white space in the if check. this will be relevant soon
export class WindowResizeListener extends Component {
state = {
height: window.innerHeight,
width: window.innerWidth
}
componentDidMount() {
Observable.fromEvent(window, 'resize')
.debounceTime(250)
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function(s) {
var openingStack = [];
var closingStack = [];
var openingPair = ['(', '{', '['];
var closingPair = [')', '}', ']'];
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} n
/**
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function(nums) {
var maximumSum = -Infinity;
nums.forEach((num, index) => {
var accumulator = num;
if(accumulator > maximumSum) maximumSum = accumulator;