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 / vscode-ff-custom-settings.json
Created November 19, 2019 03:22
VSCode Firefox Quantum Theme custom settings.json
{
"workbench.colorCustomizations": {
"editor.selectionBackground": "#2179775c",
"editor.background": "#0a0a0a"
},
"workbench.colorTheme": "Firefox Quantum Dark"
}
@motss
motss / create-social-media-share-button-by-customizable-link.js
Created September 11, 2019 04:19
Create social media share button by customizable link
@motss
motss / alternating-characters.js
Created September 3, 2019 05:58
Alternating characters
// Time complexity: O(n) where n is the number of characters.
// Space complexity: O(1) for a few variables.
function alternatingCharacters(x) {
const len = x.length;
if (len < 2) return 0;
let lastChar = x[0];
let count = 0;
let countA = lastChar === 'a' ? 1 : 0;
@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 / 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 / format-local-date-with-short-tz.js
Last active July 30, 2019 03:36
Format local date with short timezone
/**
* ```json
* e.g. 10:21:02 GMT+0700 (インドシナ時間)
* format {time} {timezone} ({timezone_name})
* ```
*
* Note that `timezone_name` will follow the system language but
* that does not affect our purpose here as we only need `time`
* and `timezone`.
*
@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 / browser-support-for-Github.md
Last active July 23, 2019 05:01
Browser Support in Markdown for Github

Microsoft Windows x64

| Internet Explorer | Microsoft Edge | Mozilla Firefox |