Skip to content

Instantly share code, notes, and snippets.

@nathanic
nathanic / tsalesp.py
Created October 2, 2012 14:10
Simulated Annealing in Python in the early aughties
#!/usr/bin/env python
import random
from math import *
# This is an attempt at solving the travelling salesperson problem
# with simulated annealing. I tried this once in matlab and it sucked.
# a map is a list of tuples, one tuple for each city.
# each tuple represents the (x,y) location of a city on the map.
@nathanic
nathanic / quine.hs
Created September 4, 2012 20:34
triple quine in three languages!
nathan@hugin:~/p/haskell/tidbits$ cat quine.hs
q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']
main=q "q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']" "def q(a,b,c):print b+chr(10)+'q('+repr(b)+','+repr(c)+','+repr(a)+')'" "def e(x) return 34.chr+x+34.chr end;def q(a,b,c) print b+10.chr+'main=q '+e(b)+' '+e(c)+' '+e(a)+' '+10.chr end"
nathan@hugin:~/p/haskell/tidbits$ runghc quine.hs
def q(a,b,c):print b+chr(10)+'q('+repr(b)+','+repr(c)+','+repr(a)+')'
q("def q(a,b,c):print b+chr(10)+'q('+repr(b)+','+repr(c)+','+repr(a)+')'","def e(x) return 34.chr+x+34.chr end;def q(a,b,c) print b+10.chr+'main=q '+e(b)+' '+e(c)+' '+e(a)+' '+10.chr end","q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']")
@nathanic
nathanic / reverseStdin.hs
Created August 31, 2012 01:05
Explanatory version of the line-reversing Haskell one-liner
-- a more verbose and explanatory version of this one-liner
-- main = interact $ unlines . map reverse . lines
-- we're going to use some library functions:
-- lines :: String -> [String]
-- unlines :: [String] -> String
-- reverse :: [a] -> [a]
-- reverse works on arbitrary lists, and Strings are [Char].
-- let ... in allows you to bind names to values,
@nathanic
nathanic / ants.py
Created August 2, 2012 16:51
ants sorting woodchips in space
#!/usr/bin/env python
""" This is a simulation of ants moving woodchips around
using the pyscape classes. Hopefully it demonstrates the
construction of central clusters using only decentralized
algorithms followed by autonomous agents.
We begin with a scattering of woodchips across the grid.
Ants randomly walk around, following a simple rule:
if you see a woodchip:
@nathanic
nathanic / knight.clj
Created May 10, 2012 14:26
knight's random walk
;; http://www.johndcook.com/blog/2012/05/08/a-knights-random-walk/
;;
;; Start a knight at a corner square of an otherwise-empty chessboard. Move the
;; knight at random by choosing uniformly from the legal knight-moves at each
;; step. What is the mean number of moves until the knight returns to the
;; starting square?
(def MAXPOS 7)
; (pos, move) -> pos
@nathanic
nathanic / gist:1214725
Created September 13, 2011 19:01
jasmine BDD example
describe("Player", function() {
var player;
// hook before each test
beforeEach(function(){
player = new Player();
spyOn(loading,'show'); // intercept and record calls to loading.show
player.play();
});
// actual specifications/tests