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
// Here we'd do a basic queue | |
export default class Queue { | |
private queue = []; | |
private offset = 0; | |
constructor(array = []) { | |
// Init the queue using the contents of the array | |
for (const item of array) { | |
this.enqueue(item); |
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
// Implement a queue using Delayed shift arrays - It consists of associating an index with the array. | |
// When an element is dequeued, the index moves forward. When the index reaches the middle of the array, | |
// the array is sliced in two to remove the first half. | |
// This is a FIFO data structure | |
/** Queue contains a linked list of Subqueue */ | |
class Subqueue <T> { | |
public full() { | |
return this.array.length >= 1000; |
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
// Queue using two stacks | |
function createDoubleStackQueue() { | |
var that = {}; | |
var pushContainer = []; | |
var popContainer = []; | |
function moveElementToPopContainer() { | |
while (pushContainer.length !==0 ) { | |
var element = pushContainer.pop(); |
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
// Safe await | |
// This is from a brilliant article written by David Wells on a cleaner way to maange async operations in javascript | |
// here: https://davidwells.io/blog/cleaner-async-await-code-without-try-catch | |
/* Native Error types https://mzl.la/2Veh3TR */ | |
const nativeExceptions = [ | |
EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError | |
].filter((except) => typeof except === 'function') |
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
// This script progressively reduces the opacity of a site depending on a set number of days | |
// handly when you do a project and they refuse to pay you | |
(function () { | |
const dueDate = new Date('2020-02-12'); // Put in date of submission to client | |
const payDeadline = 30; // number of days as deadline for payment | |
const currentDate = new Date(); | |
let utc1 = Date.UTC(dueDate.getFullYear(), dueDate.getMonth(), dueDate.getDate()); | |
let utc2 = Date.UTC(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate()); |
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
// Debounce: Debouncing enforces that a function not be called again until | |
// a certain amount of time has passed without it being called | |
// use: | |
// If we want to debounce an add function: const add = (a, b) => a + b; | |
// resulting debounce is debounce(add, 500)(2,3); | |
// this would give 5 only after 500ms of no input | |
export const debounce = (func, wait) => { | |
let timeout; | |
return (...args) => { |
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
/* Graph Search Algoroithms | |
* Here we would implement a graph in the form of an adjacency list | |
* There are a few ways this can be done : | |
* a.Adjacency Matrix: Represent each node in a 2D matrix, the edges represent relationships between nodes | |
* b.Adjacency List: Start with a collection of nodes each having their array of edges, this is easier 0on memmory and faster | |
* Adjacency matrix method is not desirable because adding, finding or working with nodes take an O(n^2) space and time complexity | |
*/ | |
// Let's work with a graph connecting airports and their routes | |
const airports = 'PHX BKK OKC JFK LAX MEX EZE HEL LOS LAP LIM'.split(' '); |
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
// 1. Find sum of even numbers from n | |
function findSumEvenNumbers(num) { | |
let n = 0; | |
if(even(num)){ | |
n = num/2 | |
} else { | |
n = (num - 1)/2 | |
} | |
return n*n + n | |
} |
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
// A decorator is a function that can be used to modify/change different properties or methods in a class | |
// They are used inside/on classes only | |
// Understanding the orde that they are run is important in using decorators | |
// Decorators can be used on properties, methods or accessors(getters and setters); | |
// First argument is the prototype | |
// Second argument is the key ( the method, accessor or property) | |
// Third argument is the property descriptor | |
// We want to use a decorator factory as such that takes in externat arguments | |
const Logging = (message: string) => { |
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
interface IElevator { | |
on: (events: string, callback: Function) => void; | |
getCurrentFloor: () => number; | |
getCurrentDirection: () => DIRECTIONS; | |
moveUp: () => void; | |
moveDown: () => void; | |
} | |
interface IElevatorDoor { | |
open: () => void; |
OlderNewer