Problem 1: Nothing but the Truth [Elementary]
true
Problem 2: Simple Math [Elementary]
4
(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"]}) |
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): |
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) |
#!/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) |
(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) |
(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 |
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 |
#!/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: |
#!/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) |