Skip to content

Instantly share code, notes, and snippets.

# Alison asks—
#
# once i have a dictionary mapping one thing to a list of things
# how do i generate tuples of the one thing to each of the things in the list
# of the things it is mapped to
# e.g. { a : [ (1, 2), (2, 4) ], b : [ (3, 5), (7, 9) ] }
# to [(a, 1, 2), (a, 2, 4), (b, 3, 5), (b, 7, 9)]
# My reply—
@zackmdavis
zackmdavis / bear_hunt.py
Last active May 7, 2017 01:33
bear hunt
from collections import OrderedDict
OBSTACLES = OrderedDict([('grass', ("long, wavy", "swishy-swashy")),
('a river', ("deep, cold", "splash-splosh")),
('mud', ("thick, oozy", "squelch-squerch")),
('a forest', ("big, dark", "stumble-trip"))])
def hunting_anthem():
print("We're going on a bear hunt, &c.")
# if you can easily fit it in less than one and a half tweets, I don't think it
# deserves to be called a "project"
from string import ascii_uppercase as u, ascii_lowercase as l; c = lambda m, n: ''.join((chr((ord(c)-97+n)%26+97) if c in l else chr((ord(c)-65+n)%26+65)) if c in l+u else c for c in m)
in [1]: import ast; from ast import *
in [2]: m = ast.parse('def at(): print("@")')
in [3]: m.body[0].name = "@"
in [4]: exec(compile(m, "<string>", 'exec'))
in [5]: globals()['@']
Out[5]: <function __main__.@>
in [1]: import ast; from ast import *
in [2]: m = ast.parse('def at(): print("@")')
in [3]: m.body[0].name = "@"
in [4]: exec(compile(m, "<string>", 'exec'))
in [5]: globals()['@']
Out[5]: <function __main__.@>
@zackmdavis
zackmdavis / slo.go
Created May 13, 2016 21:46
writing Static Large Objects to Swift from Go
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
@zackmdavis
zackmdavis / svd.rs
Created May 6, 2016 04:35
naive singular value decomposition draft
pub fn singular_value_decomp(&self) -> (Matrix<T>, Matrix<T>, Matrix<T>) {
let ata = self.transpose() * self;
let (ata_eigenvals, ata_eigvecs) = ata.eigendecomp();
// (index, √λ) pairs (indices being order of return from `.eigendecomp`)
let mut enumerated_singular_vals = ata_eigenvals.iter()
.map(|s| s.sqrt())
.enumerate()
.collect::<Vec<_>>();
#/usr/bin/env python3
from enum import Enum
from random import choice, randint
from datetime import datetime
from operator import add, sub, mul # binary operations as functions!
# Enums were introduced in 3.4!
Operation = Enum('Operation', ('add', 'subtract', 'multiply'))
break
}
panic(err)
}
log.Printf("request is %#+v", req)
switch req := req.(type) {
case *fuse.StatfsRequest:
stats, err := mountHandle.Statfs()
import csv
import logging
import bleach
import textblob
import gensim
from sklearn.decomposition import PCA
from numpy import dot
from numpy.linalg import norm