Skip to content

Instantly share code, notes, and snippets.

@dnene
dnene / bar.py
Created February 9, 2012 04:32
Redirecting print - a) by reassigning to print b) creating a different variable
from functools import partial
mode = "debug"
def get_logger(stream = None) :
if stream is None :
return __builtins__.print
else :
return partial(__builtins__.print,file=stream)
def main() :
@dnene
dnene / pom_modify.groovy
Created January 25, 2012 15:56
gradle snippet to modify pom contents
install.repositories.mavenInstaller.pom.whenConfigured { pom ->
excludedJars = project.bnd.exclusions.tokenize(",").collect{ dep ->
splitDep = dep.split(':')
[splitDep[0],splitDep[1]]
}
deps = project.configurations['compile'].dependencies.collect {dep ->[dep.group,dep.name]}
pom.dependencies.removeAll {dep -> !excludedJars.contains([dep.groupId,dep.artifactId])}
}
@dnene
dnene / python_computer_scientists.py
Created January 17, 2012 14:40
python is for computer scientists ?
import csv
import StringIO
data = """Bob,Dobbs,bob@dobbs.com,25.00
Rocket J.,Squirrel,rocky@frostbite.com,0.00
Bullwinkle,Moose,bull@frostbite.com,0.25
Vim,Wibner,vim32@goomail.com,25.00"""
data_stream = StringIO.StringIO(data)
@dnene
dnene / GradleDemo.scala
Created January 17, 2012 03:39
Using gradle for build and release management with Scala and Java - Part 1. Accompanying code
package in.vayana.blog.gradle_demo
object GradleDemo {
def main(args: Array[String]) = {
println("hello world!")
}
}
@dnene
dnene / excel_column_headers.scala
Created January 4, 2012 17:57
Converting column numbers into excel column headers. Convert integers into strings as follows 0=> "A", 25=>"Z",26=>"AA",701=>"ZZ", 702=>"AAA"
import scala.collection.mutable.ListBuffer
object Coordinates {
def main(args: Array[String]): Unit = {
println(numToStr(0))
println(numToStr(25))
println(numToStr(26))
println(numToStr(51))
println(numToStr(52))
println(numToStr(700))
println(numToStr(701))
@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)
@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 / 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 / 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 / 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)