Skip to content

Instantly share code, notes, and snippets.

yorbb robbywashere

Block or report user

Report or block robbywashere

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@robbywashere
robbywashere / erato.js
Created Mar 11, 2019
The Sieve of Eratosthenes in Javascript
View erato.js
function erato(n){
let map = new Map((new Array(n-1).fill(1)).map((n,i)=>[i+2,true]));
for (let i = 2; i <= Math.sqrt(n);i++){
if (!map.get(i)) continue
for (let j = i*2; j <= n; j += i) map.set(j,false)
}
return Array.from(map).filter(([n,b])=>b).map(([n])=>n);
}
console.log(erato(100))
@robbywashere
robbywashere / rx.js
Last active Feb 17, 2019
simple wild card regex implementation in javascript (interview question)
View rx.js
/*
Implement regular expression matching with the following special characters:
* . (period) which matches any single character
* * (asterisk) which matches zero or more of the preceding element
That is, implement a function that takes in a string and a valid regular
@robbywashere
robbywashere / lru.ts
Last active Feb 13, 2019
simple LRU cache typescript implementation
View lru.ts
import { strict as assert } from "assert";
type Node<V> = { parent?: Node<V>; next?: Node<V>; value: V };
class DoublyLinkedList<V> {
private _head?: Node<V>;
private _tail?: Node<V>;
get head() {
return this._head!;
@robbywashere
robbywashere / swagger2ts.js
Created Feb 9, 2019
Convert Swagger 2 spec to typescript superagent api client
View swagger2ts.js
function typify(spec, reqParams) {
return walk(null, spec, new Set(reqParams));
function walk(name, prop, req = new Set()) {
const {
name: propName,
type,
items,
required = [],
View firstSundays.js
function monthDays(year){
const m = (new Array(12)).fill(1).map((_,x)=>x%2?30:31);
if (year % 4) m[1] = 28;
else if (year % 100) m[1] = 29;
else if (year % 400) m[1] = 28
else m[1] = 28;
return m;
}
@robbywashere
robbywashere / varidiac.ts
Created Jan 23, 2019
combine functions with varidiac rest parameters types
View varidiac.ts
function combine<T extends any[]>(...args: ((...fnArgs: T) => void)[]) {
return (...a: T) => args.forEach(fn => fn(...a));
}
function foo(n: number, o: string){
console.log(`string ${n} ${o}`);
}
function bar(n: number, o: string){
View bfs.js
class Graph {
constructor(){
this.nodes = {};
}
addNode(n){
if (!this.nodes[n]) this.nodes[n] = [];
}
View topo_sort.js
class Graph {
constructor(){
this.nodes = {};
}
addNode(n){
if (!this.nodes[n]) this.nodes[n] = [];
}
addDirEdge(a,b){
@robbywashere
robbywashere / json_parser.js
Last active Jan 15, 2019
a (very naive) json parser in javascript
View json_parser.js
function parse_json(jsonStr) {
let tokens = [];
for (let i = 0; i < jsonStr.length; i++) {
if (jsonStr[i] === '"') {
value = '';
i++ ;
while (jsonStr[i] !== '"') {
value += jsonStr[i];
@robbywashere
robbywashere / HousePainter.js
Created Nov 23, 2018
House Painter Problem / Coding Problem A Day / Minimum non adjacent matrix indices
View HousePainter.js
/*
A builder is looking to build a row of N houses that can be of K different
colors. He has a goal of minimizing cost while ensuring that no two neighboring
houses are of the same color.
Given an N by K COSTS where the nth row and kth column represents the cost to
You can’t perform that action at this time.