Skip to content

Instantly share code, notes, and snippets.

def largest(items, index):
"""
-create a dictionary where the key=item, value=occurrence of item
-get minimum key in dictionary (smallest element)
-add key to ordered list v times (add element number of times it occurs)
-remove pair from dictionary
-return kth element from ordered list
"""
d = collections.defaultdict(int)
order = []
# coding: utf8
"""Simple pattern matching function dispatch through a decorator
Example:
@matchs(int)
def myfunc(i):
print "int received:", i
@beng
beng / markov.py
Last active December 27, 2015 06:49
markov chain to preserve groupings
import pickle
import music21
from redis import Redis
from markov import MarkovChain
r = Redis(db=2)
@beng
beng / autolog.py
Last active April 3, 2023 16:28 — forked from brendano/autolog.py
modified from the original to remove the use of global variables, implement a logging class instead of relying on sys.stdout, remove the function log decorator, remove the module log decorator, allow color changing on any log call, allow indentation level changing on any log call, and PEP-8 formatting.
# Written by Brendan O'Connor, brenocon@gmail.com, www.anyall.org
# * Originally written Aug. 2005
# * Posted to gist.github.com/16173 on Oct. 2008
# Copyright (c) 2003-2006 Open Source Applications Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
@beng
beng / maybe.py
Last active August 29, 2015 14:07 — forked from senko/maybe.py
# maybe.py - a Pythonic implementation of the Maybe monad
# Copyright (C) 2014. Senko Rasic <senko.rasic@goodcode.io>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
@beng
beng / rbm.py
Last active August 29, 2015 14:10
RBM from Edwin Chen
"""
This code is taken from Edwin Chen's RBM repo:
https://github.com/echen/restricted-boltzmann-machines
Overview: http://blog.echen.me/2011/07/18/introduction-to-restricted-boltzmann-machines/
Change Log (11/24/14):
- PEP8 (Ben Gelb)
"""
@beng
beng / apt-get-history.sh
Created December 2, 2014 21:03
A way to find all the stuff you manually installed on ubuntu via apt-get as found here: http://askubuntu.com/a/250530
(zcat $( ls -tr /var/log/apt/history.log*.gz ) ; cat /var/log/apt/history.log ) | egrep '^(Start-Date:|Commandline:)' | grep -v aptdaemon | egrep '^Commandline:'
@beng
beng / solution.py
Last active November 5, 2015 15:13 — forked from isa/gist:2571012
Question: Convert following into the latter data structure in less than 30 lines:
List:
A, B, C
A, C, E
E, F, D
D, A, J
E, D, J
List
@beng
beng / reconfigure-keyboard.sh
Created January 20, 2015 16:27
command to reconfigure your keyboard in ubuntu server
sudo dpkg-reconfigure keyboard-configuration
@beng
beng / impbcopy.m
Last active September 4, 2018 07:03 — forked from kenkeiter/impbcopy.m
/////////////////////////////////////////////////////////
// Copied from http://www.alecjacobson.com/weblog/?p=3816
/////////////////////////////////////////////////////////
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import <unistd.h>
BOOL copy_to_clipboard(NSString *path)
{
// http://stackoverflow.com/questions/2681630/how-to-read-png-image-to-nsimage
NSImage * image;