Skip to content

Instantly share code, notes, and snippets.

View motss's full-sized avatar
🥑
Life is like a prototype, just like JavaScript. So, keep betting on JavaScript!

The web walker motss

🥑
Life is like a prototype, just like JavaScript. So, keep betting on JavaScript!
View GitHub Profile
@motss
motss / pascals-triangle-with-n-level.js
Created August 30, 2019 10:01
Pascal's triangle with n level
// Time complexity: O(n ^ 2) where is the number of levels and
// another n is for the number of elements in each level.
// Space complexity: O(2n + 1) to hold all elements, which is linear space.
function pascalTriangle(level) {
if (level === 0) return [];
if (level === 1) return [1];
if (level === 2) return [1, 1];
const d = Array.from(Array(level), () => []);
@motss
motss / min-steps-to-one.js
Last active August 30, 2019 05:50
Moar dynamic programming
// Given an integer n, find the minimum number of steps
// to reach integer 1.
// At each step, you can:
// * n - 1
// * n / 2, if it is divisble by 2
// * n / 3, if it is divisble by 3
// n = 0: 0 // base case 0.
// n = 1: 0 // base case 1.
// n = 2: 1 // base case 2.
@motss
motss / insertion-sort.js
Last active August 29, 2019 06:21
Sorting
// i
// j
// 4 2 1 3 6 0
// i
// j
// 4 2 1 3 6 0
// 2 4 1 3 6 0 # 2 < 4
// i
@motss
motss / quick-sort.js
Last active July 28, 2019 07:18
Sorting algorithms
{
// Reference: https://bit.ly/2yjWDeY
// Time complexity: O(n log n) on average or O(n^2) for worst-case
// Space complexity: O(n)
function quickSort(list) {
if (list.length < 2) return list;
const left = [];
const right = [];
@motss
motss / null-equals-undefined.js
Last active July 12, 2019 09:35
According to ECMAScript's specs, it explains why null == undefined
// http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3
// http://es5.github.io/#x11.9.3
// Abstract/ loose equality comparison will do type coercion
// While strict equality comparison will compare the types
null == undefined; // true
@motss
motss / binary-search-iterative.js
Last active July 23, 2019 03:36
Binary Search (recursive or iterative implementations)
function bsr(arr, m) {
let l = 0;
let r = arr.length - 1;
while (l <= r) {
const mid = (l + (r - l) / 2) | 0;
const val = arr[mid];
if (val === m) return mid;
if (m > val) l = mid + 1;
@motss
motss / flight-search-form.md
Created July 10, 2019 05:11
Attempt to recreate flight search form on Google Flights

Attempt to recreate flight search form on Google Flights

Demo URL

@motss
motss / bfs.ts
Last active August 24, 2019 11:14
Construct Binary Search Tree
function bfs<T>(tree: BSTNode<T>, fn: (n: T) => void) {
const queue = [tree];
while (queue.length) {
const cur = queue.shift();
if (cur.left) queue.push(cur.left);
if (cur.right) queue.push(cur.right);
fn(cur.value);
@motss
motss / number-in-range.js
Last active July 23, 2019 03:40
Determine if a given number is in range without using comparison branches and operators
/**
* This was intended for checking that an ASCII character is in range.
* But this turns out to be a function that can be used for checking if a number is in range too.
*
* Assuming, `MIN` and `MAX` are hardcoded to the code point of the character 'a' and
* the character 'z', respectively, the following equation will always return positive integers
* in the difference of the `MIN` and the `MAX`:
*
* (MAX - x) and (x - MIN) must always be positive for any of the results.
*
@motss
motss / uuid-v4-browser.js
Created July 3, 2019 03:33
UUID v4 for browsers
/** See https://bit.ly/2gvCcqe to learn more about UUID v4 for browsers */
function uuidv4() {
if (window.crypto && window.crypto.getRandomValues) {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16));
}
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = Math.random() * 16 | 0;
const v = c == 'x' ? r : (r & 0x3 | 0x8);