Skip to content

Instantly share code, notes, and snippets.

View timbuckley's full-sized avatar

Tim Buckley timbuckley

  • Aline
  • NYC
View GitHub Profile
@timbuckley
timbuckley / wordle-solver.ts
Last active January 15, 2022 23:16
Wordle Solver
// Create a large array of 5-letter words in a words.json array like so { "solutions": [...] }
import { solutions } from "./words.json";
/**
* Solves Wordle in as few tries as possible.
*
* @example
* const solver = new WordleSolver(optionalStartingList);
*
* // User enters 'later', gets a score of [grey, grey, hit, misplaced, misplaced]:
@timbuckley
timbuckley / does-polling-work.ts
Created November 21, 2021 22:27
Does polling work? (Yes)
const US_ADULT_POPULATION = 245e6;
const populationSize = US_ADULT_POPULATION;
const defaultSampleSize = 15000;
type Individual = number;
type Population = Individual[];
type Sample = Individual[];
main();
import React, {useState} from 'react'
// Custom hook that manages local state for a counter.
export function useCounter(initialValue) {
const [count, setCount] = useState(initialValue)
const increment = () => setCount(count => count + 1)
const decrement = () => setCount(count => count - 1)
const reset = () => setCount(initialValue)
return {
reset,
import React from 'react';
function Spenses({ items }) {
function deleteItem({ id, amount }) {
// Delete the item.
}
return (
<>
{items.map((item, index) => (
@timbuckley
timbuckley / sequelize-migration-file-generator.js
Created August 18, 2020 19:22 — forked from agodin3z/sequelize-migration-file-generator.js
Creates migration files for existing sequelize models
#!/usr/bin/env node
const fs = require('fs');
const models = require('../models');
for (const model in models) {
const tableName = models[model].tableName;
let defaultValue = '';
let onUpdate = '';
@timbuckley
timbuckley / basic-fizzbuzz-haskell.hs
Last active December 21, 2017 20:07
Comparing fizzbuzz implementations in Haskell and JavaScript
fizzes = cycle ["", "", "Fizz"]
buzzes = cycle ["", "", "", "", "Buzz"]
fbs = zipWith (++) fizzes buzzes
nums = map show [1..]
fizzbuzz n = take n (zipWith max nums fbs)
-- Usage:
-- fizzbuzz 3
-- -> ["1", "2", "Fizz"]
@timbuckley
timbuckley / schema.js
Created December 20, 2017 16:00
Example schema setup of normalizr
import { Schema, arrayOf } from 'normalizr'
let userSchema = new Schema('user', options)
let nbhdSchema = new Schema('nbhd', options)
let itemSchema = new Schema('item', options)
let commentSchema = new Schema('comment', options)
let nbhdsSchema = arrayOf(nbhdSchema)
userSchema.define({
nbhds: nbhdsSchema
@timbuckley
timbuckley / hangman-no-types-w-arrow.hs
Last active August 7, 2018 19:25
Solving Hangman in Haskell
module HangmanGuesser (main) where
import Data.List (intersect, nub, sort)
import Data.Char (toLower)
import Data.Tuple (swap)
import Data.Map as M (Map, fromListWith, toList)
main = do
corpus <- readFile "web2"
import qualified Data.Map as M (Map, empty, insertWith, toList)
import Data.Foldable
import Data.List
example_csv = "rest1,item1\nrest1,item2\nrest3,item1\nrest3,item2\nrest2,item3\nrest1,item5\nrest2,item2"
csvToDict :: String -> M.Map String [String]
csvToDict = foldToDict . map splitOnComma . lines
where
splitOnComma = words . map (\c -> if c == ',' then ' ' else c)
@timbuckley
timbuckley / how-effective-sampling-is.py
Last active June 4, 2017 23:01
Simulates doing a thousand polls at a 1.5k sample size from a population of 256 million. Most any poll was off was by 4.5%, while average variance was just 1.19%.
import random
american_adult_population = int(245e6)
# Warning: This array is HUGE!
# The array is just the numbers from 0 to 245,000,000
usa_array = range(american_adult_population)
actual_average = float(american_adult_population / 2)
sample_amount = 1500