Skip to content

Instantly share code, notes, and snippets.

View thebopshoobop's full-sized avatar

Will Timpson thebopshoobop

View GitHub Profile
from collections import defaultdict
import json
from flask import url_for
class CachedUrlFor():
def __init__(self):
self._cache = defaultdict(dict)
const simplify = sample => {
let deDup = [];
for (let current of sample) {
deDup[current] = true;
}
return Object.keys(deDup)
.map(i => +i)
.sort((a, b) => a - b);
};
function* genRange(start, stop) {
let curr = 0;
while (curr < stop) yield curr++;
}
const gen = genRange(0, 3);
console.log("generator:");
for (let i of gen) {
FROM python:3.6
# create working directory
RUN mkdir -p /usr/src/app
RUN mkdir -p /usr/src/py
# install virtualenv globally
RUN pip install virtualenv
# create app user
import React, { Component } from "react";
import ReactTable from "react-table";
import "react-table/react-table.css";
class App extends Component {
// Constructor is not needed if you don't need to compute initial state
state = { data: [] };
indexData = (item, index) => ({ ...item, id: index + 1 });
const DEBUG = JSON.parse(process.env.DEBUG) || false;
const logger = wrapped => (...args) => {
const results = wrapped(...args);
if (DEBUG) {
console.log(`Function: ${wrapped.name}; ${args} => ${results}`);
}
return results;
};
const benchmark = arr => {
arr = arr
? arr
: [...Array(1000)].map(() => Math.floor(Math.random() * 1000));
const versions = [
{ arr: [...arr], title: "Random" },
{ arr: [...arr.sort((a, b) => a - b)], title: "Sorted" },
{ arr: [...arr.reverse()], title: "Reversed" },
{ arr: new Array(1000).fill(8), title: "Equal" }
const radixSort = arr => {
const maxNum = Math.max(...arr) * 10;
let divisor = 10;
while (divisor < maxNum) {
let buckets = [...Array(10)].map(() => []);
for (let num of arr) {
buckets[Math.floor((num % divisor) / (divisor / 10))].push(num);
}
// Start with an unsorted array
[95, 93, 36, 74, 33]
// Place them in the appropriate buckets for the least significant digit
{3: [93, 33], 4: [74], 5: [95], 6: [36]}
// And extract them back into an array
[93, 33, 74, 95, 36]
// Repeat for the next significant digit
{3: [33, 36], 4: [43], 7: [74], 9: [93, 95]}
const quickSort = arr => {
if (arr.length < 2) return arr;
const pivot = arr[Math.floor(Math.random() * arr.length)];
let left = [];
let equal = [];
let right = [];
for (let element of arr) {