Skip to content

Instantly share code, notes, and snippets.

# Twitter Tracker for Ubuntu
# ==========================
# ruby twitter_tracker.rb user_to_track
#
# Will poll for updates from user_to_track every 30 seconds. Uses libnotify
# to let you know when we have something. Requires gems twitter, libnotify.
#
# Place image "twittericon.png" in this folder to get a nice icon on
# notifications.
@matchu
matchu / gist:3733152
Created September 16, 2012 16:45
benchmark prime validators
require 'benchmark/ips'
require 'prime'
def isPrimeRegexp n
"1" * n =~ /^1?$|^(11+?)\1+$/
end
def isPrime(number)
if number == 0 or number == 1
return false
@matchu
matchu / gist:3861977
Created October 9, 2012 22:52
Linear-time anagram detector
import string
# The first 26 primes (corresponding to A-Z)
ALPHABET_PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101]
def get_letter_prime(letter):
"""Given a letter A-Z (equivalent to a-z), return a unique prime number
corresponding to that letter. Given a whitespace character, return 0. Given
anything else, raise an error."""
if letter in string.whitespace:
@matchu
matchu / gist:3861988
Created October 9, 2012 22:56
Linear-time ASCII anagram detector
# The first 128 primes (corresponding to each ASCII char)
ASCII_PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719]
def get_char_prime(letter):
"""Given an ASCII char, return a unique corresponding prime."""
return ASCII_PRIMES[ord(letter)]
def get_prime_char_sum(letters):
"""Return a unique sum corresponding to the number of occurrences of
each ASCII char in the given input."""
from autograder import *
import samples
import recognizer
class Homework3(ProblemSet):
class Problem1(ProblemSet):
class Problem1a(Problem):
""" Test for train_bayes."""
def test_train_bayes(self):
@matchu
matchu / closures.py
Created November 12, 2012 16:38
Python pop quiz: closures
multipliers = []
for i in range(3):
multipliers.append(lambda n: n * i)
# What do it do?
print multipliers[0](10)
print multipliers[1](10)
print multipliers[2](10)
@matchu
matchu / closures_sol.py
Created November 13, 2012 02:32
Python pop quiz: closures solution
def build_multiplier(i):
return (lambda n: n * i)
multipliers = []
for i in range(3):
multipliers.append(build_multiplier(i))
# It do what you think.
print multipliers[0](10)
print multipliers[1](10)
@matchu
matchu / robot.js
Created December 3, 2012 22:45
Sweetie.Hunt
///////////////////
var LEFT = -1;
var RIGHT = 1;
//FightCode can only understand your robot
//if its class is called Robot
var Robot = function(robot) {
};
@matchu
matchu / robot.js
Created December 4, 2012 00:58 — forked from fabiopimentel/robot.js
<deleted>
var Robot = function(robot) {
// Oh. So this is what the fork button does.
// I wish I had a delete button.
};
@matchu
matchu / README.md
Last active December 18, 2015 17:29
Get total video duration of a given topic from the Khan API.

Setup:

gem install httparty parallel

Usage: (get the ID from the topic page's URL; can be any level)

ruby khan_duration.rb solving-linear-equations-and-inequalities