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:3606387
Created September 3, 2012 02:41
Transitive Closure of a Binary Relation
(defn transitive-closure [rel]
(let [roots (into {} (for [[k v] (group-by first rel)] [k (mapv second v)]))
children (fn children [rels e]
(let [cs (get rels e [])]
(cons e (mapcat #(children rels %) cs))))]
(set (mapcat #(map vector (repeat %) (rest (children roots %))) (keys roots)))))
(transitive-closure #{[8 4] [9 3] [4 2] [27 9]})
(transitive-closure #{["cat" "man"] ["man" "snake"] ["spider" "cat"]})
@SegFaultAX
SegFaultAX / gist:3957415
Created October 26, 2012 07:26
Greplin Challenge #1
import time
def longest_palindrome(s):
"""find the longest palindrome in string"""
palindrome = lambda s: s == s[::-1]
for n in range(len(s), 2, -1):
for i in range(0, len(s) - n + 1):
sub = s[i:i+n]
if palindrome(sub):
@SegFaultAX
SegFaultAX / gist:4073727
Created November 14, 2012 18:07
Functional URL Manipulation in Python
from operator import attrgetter, itemgetter
from urlparse import urlparse, parse_qs
def compose(*fns):
def _comp(f, g):
def inner(*args, **kwargs):
return f(g(*args, **kwargs))
return inner
return reduce(_comp, fns)
@SegFaultAX
SegFaultAX / blackjack.py
Last active December 10, 2015 04:58
Simple blackjack implementation
#!/usr/bin/env python
import sys
from random import shuffle
from pprint import pformat
def group_by(keyfn, l):
groups = {}
for e in l:
key = keyfn(e)
@SegFaultAX
SegFaultAX / colcue.clj
Created January 19, 2013 21:41
Colossal Cue Solutions
(defn vax-rand [seed]
(mod (inc (* 69069 seed)) (Math/pow 2 32)))
(defn vax-seq [seed]
(iterate vax-rand seed))
(defn roulette [n seed m]
(take n (map #(int (mod % m)) (vax-seq seed))))
;; Level 1 solution
(roulette 10 6 36)
@SegFaultAX
SegFaultAX / cards.clj
Created January 22, 2013 05:40
Blackjack Clojure (Incomplete)
(ns blackjack.cards
[:use [clojure.string :only [join capitalize]]])
(def suits [:spades :hearts :clubs :diamonds])
(def ranks (range 1 14))
(def rank-names {1 "ace" 11 "jack" 12 "queen" 13 "king"})
(def new-deck (for [s suits r ranks] [s r]))
(defn shuffle-deck
@SegFaultAX
SegFaultAX / gist:4735303
Created February 7, 2013 23:45
Simple Cohort [MySQL]
SELECT
cohorts.first_action,
cohorts.last_action,
COUNT(cohorts.id) AS user_count
FROM (SELECT
MIN(DATE(created_at - INTERVAL (DAYOFWEEK(created_at) - 1) DAY)) AS first_action,
MAX(DATE(created_at - INTERVAL (DAYOFWEEK(created_at) - 1) DAY)) AS last_action,
user_id AS id
FROM daily_activity_events
GROUP BY user_id) AS cohorts
@SegFaultAX
SegFaultAX / dump-gnome2-keyring.py
Created February 19, 2013 08:34
Dump gnome2 keyring to CSV
#!/usr/bin/env python
# The MIT License (MIT) Copyright (c) 2013 Michael-Keith Bernard
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
@SegFaultAX
SegFaultAX / easy_table.py
Created February 26, 2013 20:24
Formats a list of dicts into a textual table.
#!/usr/bin/env python
# Ported from clojure.pprint/print-table
from StringIO import StringIO
def format_row(headers, row, fmts):
out = []
for col, fmt in zip([row[k] for k in headers], fmts):
out.append(fmt % col)