Skip to content

Instantly share code, notes, and snippets.

View leopd's full-sized avatar

Leo Dirac leopd

View GitHub Profile
@leopd
leopd / default.rb
Created October 25, 2012 17:12
Chef recipe to run gunicorn application and gearman workers. Posted for discussion about notification problems. This is probably not a good example to follow. :)
#
# Cookbook Name:: imgmuck
# Recipe:: default
#
# Copyright 2012, Scaled Recognition, all rights reserved
#
include_recipe "git"
include_recipe "python"
include_recipe "application"
@leopd
leopd / FifteenPuzzle.java
Created July 13, 2013 22:42
Solving the fifteen puzzle in Java using A* and Dijkstra's algorithm.
package algostudy;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
@leopd
leopd / balanced_matrix.py
Created July 18, 2013 17:12
An example of a dynamic programming solution to the problem of counting the number of nxn matrices that have exactly n/2 zeroes and n/2 ones in each row and each column. Produces OEIS A058527.
import copy
import math
import logging
def balance_cnt(n):
"""Considering all nxn matrices consisting entirely of 0s and 1s,
return the number of matrices which have exactly n/2 ones in each column and each row.
Solution using dynamic programming as outlined here:
https://en.wikipedia.org/wiki/Dynamic_programming#A_type_of_balanced_0.E2.80.931_matrix
Produces answers in this sequence (for n/2) https://oeis.org/A058527
@leopd
leopd / dfs.py
Last active April 12, 2017 19:58
Depth-first traversal with and without recursion.
class Node:
def __init__(self,value=None,kids=[]):
self.value = value
self.kids = kids
def rdfs(n, previsit, postvisit):
'''Recursive depth-first traversal'''
previsit(n)
for k in n.kids:
function add_rand_field(collection) {
collection.find().forEach(function(data) {
collection.update({_id:data._id}, {
$set:{rand:Math.random()}
});
});
collection.ensureIndex({rand:1},{background:true})
}
@leopd
leopd / .gitconfig
Created August 28, 2013 15:37 — forked from srleo/.gitconfig
[alias]
lgb = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset%n' --abbrev-commit --date=relative --branches
@leopd
leopd / groupby.js
Created August 28, 2013 15:37 — forked from srleo/groupby.js
count_by = function(collection,field) {
return collection.aggregate({
$group: {
_id: "$" + field,
count: { $sum: 1 }
}
})
}
 
count_by(db.vid10,'char');
@leopd
leopd / .vimrc
Created September 4, 2013 19:10
My .vimrc file
" turn off compatibility with the old vi
set nocompatible
set ts=4
set et
" turn syntax highlighting on by default
syntax on
" set auto-indenting on for programming
@leopd
leopd / gist:6b1e5875305f6dde6556
Created October 10, 2014 15:56
Configuring a new linux login
set -o vi # Switch bash to vi mode
@leopd
leopd / file_reader_dataset.py
Last active June 8, 2019 15:47
PyTorch Dataset class to access line-delimited text files too big to hold in memory.
from functools import lru_cache
import subprocess
from torch.utils.data import Dataset
class FileReaderDataset(Dataset):
"""Exposes a line-delimited text file as a PyTorch Dataset.
Maintains an LRU cache of lines it has read, while supporting random access into
files too large to hold in memory. Memory requirement still scales by O(N), but just
for pointers into the file, about 8 bytes per line. After the file has been scanned,
random access will be very fast - as fast as the disk plus the OS's cache of it.