Skip to content

Instantly share code, notes, and snippets.

View SegFaultAX's full-sized avatar

Michael-Keith Bernard SegFaultAX

View GitHub Profile
@SegFaultAX
SegFaultAX / gist:5626533
Created May 22, 2013 10:07
Quick and dirty EAV (for cryosphere, #python)
#!/usr/bin/env python
import sqlite3
SQL_EAV_TABLE = """
CREATE TABLE IF NOT EXISTS eav (
entity TEXT NOT NULL,
attr TEXT NOT NULL,
value BLOB,
var exists = function(x) { return x != null; };
var truthy = function(x) {
return (x !== false) && exists(x);
};
var _filter = function(fun) {
return function(coll) {
var results = [];
if (coll == null) return results;
@SegFaultAX
SegFaultAX / gist:5754209
Last active December 18, 2015 08:29
Full alphabet cryptograms
(defn crypt-range [low high]
(let [chars (map char (range low high))]
(zipmap chars (shuffle chars))))
(defn make-crypto []
(let [ranges [[97 123] [65 91] [48 58]]]
(apply merge (map #(apply crypt-range %) ranges))))
(defn encrypt [crypto s]
"Encrypt string with crypto map"
@SegFaultAX
SegFaultAX / gist:5803374
Last active December 18, 2015 15:19
Scopely Tree Challenge
#!/usr/bin/env python
from operator import itemgetter
## ELAPSED DEVELOPMENT TIME: ~45m ##
## Helper Functions ##
def juxt(*fns):
def _juxter(arg):
return [fn(arg) for fn in fns]
@SegFaultAX
SegFaultAX / gist:6043686
Last active December 20, 2015 00:39
Hyper secure encryption in 1 line of Ruby AND Clojure
# Ruby
# v1
->(x, s) { s.tr (0..25).map { |n| [(n+65).chr, (n+97).chr] }.flatten.join, (0..25).map { |n| [((n+x)%26+65).chr, ((n+x)%26+97).chr] }.flatten.join }

# v2
->(x, s) { (->(a) { s.tr a.join, (a*2).drop((x%26)*2).take(52).join })[(0..25).map{|n| [(n+65).chr, (n+97).chr]}.flatten] }
@SegFaultAX
SegFaultAX / gist:6044290
Created July 20, 2013 08:14
Trampoline Example
#!/usr/bin/python
import time
def fib(n):
if n < 2: return n
else: return fib(n - 1) + fib(n - 2)
def fast_fib(n, a, b):
if n == 0: return a
@SegFaultAX
SegFaultAX / longpath.py
Last active December 23, 2015 01:59
Long classnames in Java/Spring are long...
import os.path
import lxml.html
SPRING_URL = "http://docs.spring.io/spring/docs/3.2.x/javadoc-api/overview-tree.html"
SWING_URL = "http://www.cs.rit.edu/usr/local/pub/swm/jdoc6/overview-tree.html"
concat = lambda l: [e for v in l for e in v]
def longest(url):
files = [os.path.basename(f[2]) for f in lxml.html.parse(url).getroot().iterlinks()]
print '\n'.join(sorted(concat([f.split(".")[:-1] for f in files]), key=len, reverse=True)[:10])
package forcomp
import common._
object Phonenumber {
val dictionary = loadDictionary
val keyMap = Map(
'2' -> "ABC", '3' -> "DEF", '4' -> "GHI", '5' -> "JKL",
'6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", '9' -> "WXYZ"
)
@SegFaultAX
SegFaultAX / gist:7630307
Created November 24, 2013 18:10
letfn/letfn*
user=> (source letfn)
(defmacro letfn
"fnspec ==> (fname [params*] exprs) or (fname ([params*] exprs)+)
Takes a vector of function specs and a body, and generates a set of
bindings of functions to their names. All of the names are available
in all of the definitions of the functions, as well as the body."
{:added "1.0", :forms '[(letfn [fnspecs*] exprs*)],
:special-form true, :url nil}
[fnspecs & body]
@SegFaultAX
SegFaultAX / gist:8434500
Created January 15, 2014 11:16
Frequency sorting #python jkbbwr
#!/usr/bin/env python
import re
def powerset(l):
def _reducer(acc, e):
return acc + [x + [e] for x in acc]
return sorted(reduce(_reducer, l, [[]]), key=len)
def words(s):