Skip to content

Instantly share code, notes, and snippets.

@gigamonkey
gigamonkey / oranges.py
Created June 2, 2011 21:25
My solution to @bramcohen's coding puzzle from a week or two ago
#!/usr/bin/env python3
# Two solutions to http://www.bittorrent.com/company/about/developer_challenge
from itertools import combinations
bowls = 40
oranges = 9
def brute_force():
@gigamonkey
gigamonkey / test.js
Created June 8, 2011 01:34
Now working jQuery/Javascript
// #notes is DIV with overflow: scroll and currentParagraph is an element within that DIV.
// I'm trying to arrange things so that currentParagraph appears at the top of the DIV.
// The trick seems to be to set position: absolute on #notes so it will be the
// offsetParent of its elements and then to scroll like this:
$('#notes').scrollTop($('#notes').scrollTop() + $(currentParagraph).position().top);
@gigamonkey
gigamonkey / fpc.lisp
Created April 9, 2012 18:58
FPC experiments
(defun random-population (size p)
(loop repeat size collect (< (random 1d0) p)))
(defun population (size p)
(let ((pop (make-array size :initial-element nil)))
(loop for i below (round (* size p))
do (setf (aref pop i) t))
pop))
(defun p (population)
(defun one-apart-p (w1 w2)
(and (eql (length w1) (length w2))
(= 1 (loop
for c1 across w1
for c2 across w2
count (not (eql c1 c2))))))
@gigamonkey
gigamonkey / drunken-walk.py
Created February 20, 2013 04:32
Drunken walk solution
#!/usr/bin/env python3
# coding: utf-8
from random import choice
def make_board(size):
return { (x, y):choice('NSEW') for x in range(size) for y in range(size) }
def random_square(board):
return choice(list(board.keys()))
@gigamonkey
gigamonkey / FizzBuzz.scala
Created May 14, 2013 20:21
I don't always write FizzBuzzes, but when I do, I do it without modulus.
object FizzBuzz extends App {
val nones = Stream.continually(None)
val fizzes: Stream[Option[String]] = nones.take(2) ++ Some("Fizz") #:: fizzes
val buzzes: Stream[Option[String]] = nones.take(4) ++ Some("Buzz") #:: buzzes
for (((fizz, buzz), n) <- fizzes zip buzzes zip (1 to 100)) {
println(fizz.map(_ + buzz.getOrElse("")).orElse(buzz).getOrElse(n))
}
@gigamonkey
gigamonkey / gist:5717295
Created June 5, 2013 21:01
This seems suboptimal to me.
scala> def foo() = "foo"
foo: ()String
scala> foo
res6: String = foo
scala> foo()
res7: String = foo
scala> val o = Option("foo")
@gigamonkey
gigamonkey / Change.scala
Created July 26, 2013 18:20
Change counting problem from SICP in Scala
object Change {
val zeros = Stream.continually(0)
def ways(denominations: Seq[Int]) = {
denominations.foldLeft(zeros) { (previous, d) =>
lazy val base: Stream[Int] = zeros.take(d - 1) ++ 1 #:: previous.zip(base).map { x => x._1 + x._2 }
base.drop(d)
}
}
@gigamonkey
gigamonkey / jake-change.py
Created July 30, 2013 05:55
It seems to me the time complexity of this is non-linear in the amount as you can see in the way the number of iterations required grows. Unless maybe I've messed something up in my translation from Clojure to Python.
#!/usr/bin/env python3
def count_ways_to_make_change(coins, total):
iterations = 0
ways = 0
digit = 0
counts = [0] * len(coins)
@gigamonkey
gigamonkey / change.py
Last active December 20, 2015 09:49
A somewhat low-level implementation of the constant space, linear time change counting algorithm. (By low-level, I mean something that could be straightforwardly translated to C if I actually knew C.)
#!/usr/bin/env python3
def ways(amount, coins):
size = sum(coins) + len(coins)
starts = [ i + sum(coins[:i]) for i in range(len(coins)) ]
data = [ i in starts for i in range(size) ]
for offset in range(1, amount + 1):
prev = 0