Skip to content

Instantly share code, notes, and snippets.

(function () {
const SINGLES = [
'zero',
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
//we have an array of objects A and an array of indexes B. Reorder objects
//in array A with given indexes in array B. Do not change array A's length;
var A = ['C', 'D', 'E', 'F', 'G'];
var B = [3, 0, 4, 1, 2];
function resort(arr, order) {
let idx=0;
for(var start=0;start<arr.length;start++) {
//1, 1, 2, 3, 5, 8, 13, 21
(function() {
let result = [];
window.fibonacci = function(n) {
if (n < 1) {
throw new Error("n must be greater than 0");
}
// SLC -> AUS -> LAX -> JFK -> SJC
let passes = [
{depart: 'JFK', arrive: 'SJC'},
{depart: 'LAX', arrive: 'JFK'},
{depart: 'AUS', arrive: 'LAX'},
{depart: 'SLC', arrive: 'AUS'}
];
Array.prototype.swap = function(idx1, idx2) {
let first = Math.max(idx1, idx2);
let last = Math.min(idx1, idx2);
this.splice(last, 0, this.splice(first, 1)[0]);
};
function moveToFront(arr, idx) {
arr.swap(0,idx);
//Given an array of positive integers and a target total of X, find if there exists a contiguous subarray with sum = X
var list=[1,3,5,18];
function isValid(arr, target) {
var left=0;
var right=0;
while(true) {
var sum = 0;
Array.prototype.swap = function(idx1, idx2) {
if(idx1 >= this.length || idx2 >= this.length) {
throw new Error("index out of bounds");
}
let first = Math.max(idx1, idx2);
let last = Math.min(idx1, idx2);
let value = this.splice(first, 1, this[last])[0];
function flattenObj(obj, keyChain=[], flattened={}) {
for(let key in obj) {
let value = obj[key];
let keys = keyChain.concat(key);
if(value instanceof Object) {
flattenObj(value, keys, flattened);
}else {
flattened[keys.join('.')] = value;
}
@jasonwaters
jasonwaters / reversepolish.js
Created January 23, 2017 20:13
reverse polish notation
// a script that processes arithmetic in reverse polish notation
//https://en.wikipedia.org/wiki/Reverse_Polish_notation
const operate = {
'+': (a,b) => a+b,
'-': (a,b) => a-b,
'*': (a,b) => a*b,
'/': (a,b) => a/b,
};
@jasonwaters
jasonwaters / lakes.js
Last active January 31, 2017 22:44
bodies of water
/*
A script that processes a matrix that represents bodies of water. Any values that equal 0 represent water.
The script outputs a list of bodies of water found, with their respective sizes. For Example:
let water = [
[0, 2, 1, 0],
[0, 1, 0, 1],
[1, 1, 0, 1],
[0, 1, 0, 1]
];