This file contains 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
(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)) | |
This file contains 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 '(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))))) |
This file contains 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 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 |
This file contains 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 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 |
This file contains 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 sum_arithmetic_series(n): | |
return n*(n+1)/2.0 | |
def missing_one(upto,missing_one): | |
return sum_arithmetic_series(upto) - sum(missing_one) |
This file contains 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 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] |
This file contains 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
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 |
This file contains 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
#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) |
This file contains 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
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([]) |
This file contains 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 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 |
OlderNewer