This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
# A bag of random elements to pick from. | |
# Usage: when you want to guarantee that a random | |
# event will occur, but want a random ordering. | |
class RandomBag: | |
# Build a bag. Fill it with counts using tuples | |
# [('attack', 3), ('flee', 1), ...] | |
def __init__(self, element_count_tuples=[]): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The denominations | |
denoms = [1, 5, 10, 25] | |
def make_change(change, denoms): | |
return _make_change(change, sorted(denoms, reverse=True)) | |
def _make_change(cents, denoms): | |
ways = [] | |
for denom in denoms: | |
if cents == denom: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from flask import Flask, request, redirect, url_for | |
from werkzeug.utils import secure_filename | |
from flask import Flask, jsonify | |
from flask.ext.cors import cross_origin | |
app = Flask(__name__) | |
@app.route('/order', methods=['GET']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
# This method randomly picks a single choice from a collection of choices. | |
# freq_tuples: List of tuples of the form: (choice, count) | |
# Returns a choice, or None if empty list. | |
def weighted_random_selection(freq_tuples): | |
if len(freq_tuples) == 1: | |
return freq_tuples[0][0] | |
sorted_freq_tuples = sorted(freq_tuples, key = lambda tup : tup[1], reverse=True) | |
total = sum([count for gram, count in freq_tuples]) * random.random() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#python 2.7 | |
from __future__ import division | |
padding = '|' | |
cipher_key = 'abcdefghijklmnopqrstuvwxyz ' | |
cipher_key_length = len(cipher_key) | |
# A shift cipher. Can also use a key, turning it into a vernam cipher. | |
# text = the text | |
# key = the key. Can be a number or a string. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import json | |
import urllib2 | |
import csv | |
def geocode(a): | |
h = a.replace(' ', '+') | |
req = 'http://maps.googleapis.com/maps/api/geocode/json?address=' + h + '&sensor=false' | |
data = json.load(urllib2.urlopen(req)) | |
ploc = data['results'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import static org.junit.Assert.assertFalse; | |
import static org.junit.Assert.assertTrue; | |
import org.junit.Test; | |
/** | |
* A not-very-efficient hashmap with an array as a base. | |
* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# [SNIPPET_NAME: Screen Rotate] | |
# [SNIPPET_DESCRIPTION: Shows a widget to rotate screen] | |
# [SNIPPET_AUTHOR: Eric Fruchter <[email protected]>] | |
# [SNIPPET_LICENSE: GPL] | |
# | |
import gtk |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def avalanche(sequence, print_all=False): | |
states = ['1', '2'] | |
emissions = {} | |
emissions[('1', 'C')] = .1 | |
emissions[('1', 'C#')] = .1 | |
emissions[('1', 'D')] = .4 | |
emissions[('1', 'D#')] = .4 | |
emissions[('2', 'C')] = .4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"bash rotate_display flip" | |
m:0x0 + c:161 | |
NoSymbol |