Skip to content

Instantly share code, notes, and snippets.

View dmitric's full-sized avatar

Dmitri Cherniak dmitric

View GitHub Profile
(defn mark-up [tag-name value]
(str "<" tag-name ">" value "</" tag-name ">"))
(defn xmlify [names fields]
(reduce str (map #(mark-up %1 %2) names fields)))
;;will work, but ugly... makes each html element
(?<- (stdout) [?p ?xml-name ?xml-age ?xml-gender] (age ?p ?a) (gender ?p ?g)(mark-up "name" ?p :> ?xml-name) (mark-up "age" ?a :> ?xml-age)(mark-up "gender" ?g :> ?xml-gender))
@dmitric
dmitric / gist:836560
Created February 21, 2011 01:51
hadoop to xml pipes example
(import '(java.io BufferedReader InputStreamReader FileReader File BufferedWriter FileWriter))
(defn cmd [p] (.. Runtime getRuntime (exec (str p))))
(defn cmdout [o]
(let [r (BufferedReader.
(InputStreamReader.
(.getInputStream o)))]
(dorun (map println (line-seq r)))))
def naive_is_prime(n):
if n <= 1:
return False
if n == 2:
return True
if n % 2 == 0:
return False
for i in xrange(3,n):
if n % i == 0:
return False
def nth_prime(n):
i = 2
prime_count = 0
while True:
if is_prime(i):
if prime_count == n:
return i
prime_count += 1
if i == 2:
i += 1
def sum_arithmetic_series(n):
return n*(n+1)/2.0
def missing_one(upto,missing_one):
return sum_arithmetic_series(upto) - sum(missing_one)
def find_missing(n,missing_some):
check = [k+1 for k in xrange(n)]
for i in missing_some:
if i in check:
check.remove(i)
return check
def find_missing_using_sets(n,missing_some):
x = frozenset(frozenset([i for i in xrange(1,n+1)])-frozenset(missing_some))
return [i for i in x]
from operator import mul
#find what numbers we are missing from an unordered list of size 1 to k, where numbers are bounded by n-k
def missing_n_prime(upto,missing_some):
product_1_to_n_primes = reduce(mul,[nth_prime(n) for n in xrange(upto)])
product_missing = reduce(mul, [nth_prime(n-1) for n in missing_some])
prime_factors = prime_factorize(product_1_to_n_primes/product_missing)
return [what_number_prime(i) for i in prime_factors]
#get prime factors of a number
@dmitric
dmitric / add.py
Created March 19, 2011 23:04
Some useful bit arithmetic
#addition, full adder style
def add(a,b):
if a == 0:
return b
elif b == 0:
return a
summed = a^b
carry = (a&b) << 1
return add(summed,carry)
@dmitric
dmitric / cumulativesum2D.py
Created March 21, 2011 07:43
A wrapper class for calculating simple statistics over various sub-arrays of an array in amortized O(1) time
class CumulativeSum2D:
"""A wrapper around 2D arrays to be able to quickly grab sums and averages of various sub-arrays of the array"""
def __init__(self,data):
self.cum_sum = self.__cumulify(data)
def __cumulify(self,data):
""" creates a 2D cumulative sum matrix"""
cum_sum_array = []
for j in xrange(len(data)):
cum_sum_array.append([])
@dmitric
dmitric / api_call.py
Created March 27, 2011 19:49
A nice way to handle twitter API errors
def make_api_call(self,f,**kwargs):
if f is None:
raise ValueError("No function passed in!")
response = []
wait_period = 1
while True:
try:
#your api call
response = f(**kwargs)
break