This file contains 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
#!/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(): |
This file contains 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
// #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); |
This file contains 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
(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) |
This file contains 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
(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)))))) |
This file contains 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
#!/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())) |
This file contains 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
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)) | |
} |
This file contains 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
scala> def foo() = "foo" | |
foo: ()String | |
scala> foo | |
res6: String = foo | |
scala> foo() | |
res7: String = foo | |
scala> val o = Option("foo") |
This file contains 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
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) | |
} | |
} |
This file contains 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
#!/usr/bin/env python3 | |
def count_ways_to_make_change(coins, total): | |
iterations = 0 | |
ways = 0 | |
digit = 0 | |
counts = [0] * len(coins) |
This file contains 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
#!/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 |
OlderNewer