This file contains hidden or 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 operator | |
# naive recursive solution | |
def fact1(n): | |
if n <= 1: | |
return 1 | |
return n * fact1(n - 1) | |
fact2 = lambda n: reduce(operator.mul, xrange(1, n + 1)) |
This file contains hidden or 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
(let [chr (zipmap (range) "ABCDEFGHIJKLMNOPQRSTUVWXYZ") | |
encode (fn [n acc] | |
(if (< n 26) | |
(cons n acc) | |
(recur (dec (quot n 26)) (cons (rem n 26) acc))))] | |
(defn col-str | |
"Convert numbers to Excel headers." | |
[n] | |
(apply str (map chr (encode n '()))))) |
This file contains hidden or 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
(fn [n] | |
(nth | |
(iterate (fn [row] | |
(let [prow (partition 2 1 row)] | |
(concat [1] (map #(apply + %) prow) [1]))) | |
[1]) | |
(dec n))) |
This file contains hidden or 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 my-flatten | |
"Flatten a nested collection using Tail Recursion." | |
[coll] | |
(letfn [(step [[fst & more :as coll] res] | |
(if (seq coll) | |
(if (coll? fst) | |
(recur (concat fst more) res) | |
(recur more (cons fst res))) | |
(reverse res)))] | |
(step coll nil))) |
This file contains hidden or 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
(ns tetris.core | |
(:import (java.awt Color Dimension BorderLayout) | |
(javax.swing JPanel JFrame JOptionPane JButton JLabel) | |
(java.awt.event KeyListener)) | |
(:use clojure.contrib.import-static deflayout.core | |
clojure.contrib.swing-utils) | |
(:gen-class)) | |
(import-static java.awt.event.KeyEvent VK_LEFT VK_RIGHT VK_DOWN VK_UP VK_SPACE) |
This file contains hidden or 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 gevent import monkey; monkey.patch_all() | |
import gevent | |
import gevent.greenlet | |
from functools import partial | |
from random import random | |
import urllib | |
import urllib2 | |
def on_exception(fun, greenlet): |
This file contains hidden or 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
(ns pascal) | |
(defn next-row | |
"Given one row, generate the next one." | |
[row] | |
(vec (map + (concat [0] row) (concat row [0])))) | |
(defn pascal | |
"Generate an infinite lazy sequence of Pascal rows." | |
[] |
This file contains hidden or 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
(ns test) | |
(defn try-times | |
"Try executing a thunk `retries` times." | |
[retries thunk] | |
(if-let [res (try | |
[(thunk)] | |
(catch Exception e ; can be any exception | |
(when (zero? retries) | |
(throw e))))] |
This file contains hidden or 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
;;; accounts.clj -- Accounts -*- Clojure -*- | |
;;; Time-stamp: "2010-05-04 11:17:53 ghoseb" | |
;;; Author: Baishampayan Ghose <[email protected]> | |
(ns accounts) | |
(def *min-bal* 500) | |
(defstruct account | |
:name |
This file contains hidden or 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
;; | |
;; NS CHEATSHEET | |
;; | |
;; * :require makes functions available with a namespace prefix | |
;; and optionally can refer functions to the current ns. | |
;; | |
;; * :import refers Java classes to the current namespace. | |
;; | |
;; * :refer-clojure affects availability of built-in (clojure.core) | |
;; functions. |