Skip to content

Instantly share code, notes, and snippets.

@dnene
dnene / greed.py
Created January 10, 2011 06:38
Greed scoring in python
# Rules : https://github.com/edgecase/ruby_koans/blob/master/koans/GREED_RULES.txt
r = ((100,1000), (0,200), (0,300), (0,400), (50,500), (0,600))
def times(dct,val) :
dct[val-1] = dct[val-1] + 1
return dct
def score(dice) :
dct = reduce(times,dice,dict((x,0) for x in range(6)))
@dnene
dnene / player.scala
Created April 8, 2011 13:39
Early work in progress implementation of a statically typed API poser
package tictactoe
import scala.collection.immutable.Map
// Players
sealed abstract case class Player(val number: Int, val mark: Char, val next: () => Player)
case object player_0 extends Player(0,'O', () => player_1)
case object player_1 extends Player(1,'X', () => player_0)
// Cell
@dnene
dnene / prime_generator.py
Created July 5, 2011 10:52 — forked from dexterous/prime_generator.py
prime number generator in python
def prime_generator():
yield 2
prev = [2]
potential = 3
while True:
while any(map(lambda x: potential % x == 0, prev)):
potential += 1
@dnene
dnene / sieve.py
Created July 5, 2011 10:54
Sieve of Erathosnes Generator
def prime_generator():
current = 2
primes = []
while True:
while any(map(lambda x: current % x == 0, primes)):
current += 1
yield current
@dnene
dnene / exercise-1.R
Created October 30, 2011 17:33
Machine Learning Class - First week problem - solution using R (not Octave/Matlab)
# define function computeCost
computeCost = function(X, y, theta) { sum((X %*% theta - y) ^ 2) / (2 * nrow(X))}
# define function to compute next value of theta
nextTheta = function(X, y, theta, alpha) { theta - (t(X) %*% (X %*% theta - y) * alpha / nrow(y))}
# define function to perform gradientDescent
gradientDescent = function(X,y,theta,alpha,num_iters) {
jHistory = rep(NA,num_iters);
for (i in 1:num_iters) {
@dnene
dnene / weights.py
Created October 31, 2011 00:02
Broken Weight problem
# Response to challenge at http://beust.com/weblog/2011/10/30/a-new-coding-challenge/
# Further (braintwistingly) compact version of the same logic : https://gist.github.com/1326654
import itertools
n = 40
weights = tuple((w1,w2,w3,n-(w1+w2+w3)) for w1 in range(1,n) for w2 in range(w1,n - w1) for w3 in range(w2, n-w1-w2) if n-(w1+w2+w3) -w3 >= 0)
for weight in weights :
allvals = list(val for val in range(1,n + 1))
for clength in range(1,5) :
for combo in itertools.combinations(weight,clength) :
other = list(weight)
@dnene
dnene / weights2.py
Created October 31, 2011 00:40
Broken Weight problem - Challenge by @cbeust
# Earlier solution at : https://gist.github.com/1326624
# This one is the same logic, though a bit compact
# 4 weights are w1, w2, w3, w4 where w1 + w2 + w3 + w4 = 40
import itertools
n = 40
def listremove(list,val):
if val in list : list.remove(val)
return list
for weight in tuple((w1,w2,w3,n-(w1+w2+w3)) for w1 in range(1,n) for w2 in range(w1,n - w1) for w3 in range(w2, n-w1-w2) if n-(w1+w2+w3) -w3 >= 0) :
@dnene
dnene / weightsets.py
Created October 31, 2011 00:59
Generate weight combinations for four pieces totalling 40 pounds (int vals)
wts = tuple((w1,w2,w3,n-(w1+w2+w3)) for w1 in range(1,n) for w2 in range(w1,n - w1) for w3 in range(w2, n-w1-w2) if n-(w1+w2+w3) -w3 >= 0)
print len(wts)
@dnene
dnene / truthy.py
Created October 31, 2011 16:40
grouping by truth values
import itertools
print dict((key,list(iter)) for key,iter in itertools.groupby(sorted([True, False, -1, 0, 1, 2, None, object(), [], [object()], {}, {'foo': object()}],key = bool),bool))
@dnene
dnene / Lens.scala
Created November 24, 2011 07:50
The lens API in a single compilable source file
// author : Tony Morris : @dibblego
// originally published at : http://paste.pocoo.org/show/512163/
import collection.SeqLike
import collection.immutable.Stack
sealed trait State[S, A] {
val run: S => (A, S)