Skip to content

Instantly share code, notes, and snippets.

@haakonn
haakonn / gist:2353416
Created April 10, 2012 18:22
Utility to concatenate an array of single digits ([1,2,3]) into an int (123)
public class ArrayToIntUtil {
public static int a2i(int... a) {
int result = 0;
int f = (int) Math.pow(10, a.length);
for (int i : a) result += i * (f /= 10);
return result;
}
public static void main(String... args) {
@haakonn
haakonn / gist:3938834
Created October 23, 2012 13:45
Python function for partitioning a range of integers into X number of subranges
def partition(partition_count, id_range):
"""
Partitions an integer range into partition_count partitions of uniform size.
id_range specifies the integer range to partition (a tuple (from, to)).
"""
partition_size = ceil((id_range[1] - id_range[0]) / partition_count) + 1
for x in range(0, partition_count):
from_ = int(x * partition_size) + id_range[0]
to = min(int(from_ + partition_size - 1), id_range[1])
yield (from_, to)
@haakonn
haakonn / Logfile to CSV in Scala
Created October 7, 2013 19:00
Just a quick Scala script to convert a certain logfile into CSV. Instead of instinctively going to bash or python for these things, why not Scala? Just run with 'scala makecsv.scala', starts and finishes quickly. No build step or bytecode or support files, just a script.
println("date,msisdn,message")
scala.io.Source.fromFile("problem.log").getLines().foreach { line =>
val m = "^(.{19}).*msisdn='(.{10}).*msg='(.*)', status.*".r.findFirstMatchIn(line).get
println(s"""${m.group(1)},${m.group(2)},"${m.group(3)}"""")
}
Verifying myself: My Bitcoin username is +haakon. https://onename.io/haakon
Rank Who Followers Following Ratio
1 BTCFoundation 35407 224 158.06697
2 learnemacs 4834 414 11.676329
3 B1TCOIN 21869 9725 2.2487404
4 BitcoinPRBuzz 13507 6483 2.0834491
5 magistratic 58 28 2.0714285
6 EVRY_ASA 657 364 1.8049451
7 erikfossen 1483 868 1.7085253
8 klingsen 386 258 1.496124

Keybase proof

I hereby claim:

  • I am haakonn on github.
  • I am haakon (https://keybase.io/haakon) on keybase.
  • I have a public key whose fingerprint is 4259 AFED 9A07 B544 DDA9 B948 F0E7 C547 5B3F C330

To claim this, I am signing this object:

@haakonn
haakonn / repeat.scala
Created April 21, 2015 20:38
Implementing the repeat loop in Scala
/*
Enables loops such as this:
var i = 10
repeat {
print(s"i is $i\n")
i = i - 1
} until (i == 0)
*/
@haakonn
haakonn / bt-abonnent.user.js
Last active December 14, 2016 13:48
Les abonnent-innhold på bt.no med Greasemonkey
// ==UserScript==
// @name bt-abonnent
// @namespace haakonnilsen.com
// @include http://www.bt.no/*
// @version 2
// @grant none
// ==/UserScript==
// Only works with JavaScript disabled. You can use YesScript for this.
@haakonn
haakonn / sportsfri-bt.user.js
Created January 23, 2016 17:13
Sportsfri Bergens Tidende
// ==UserScript==
// @name Sportsfri Bergens Tidende
// @namespace http://haakonnilsen.com
// @description Bergens Tidende uten sport
// @include http://www.bt.no/*
// @grant none
// ==/UserScript==
var boring = "sport|fotball|live|sprek";
var filterRE = new RegExp(boring);
@haakonn
haakonn / sportsfri-aftenposten.user.js
Created January 23, 2016 17:14
Sportsfri Aftenposten
// ==UserScript==
// @name Sportsfri Aftenposten
// @namespace http://haakonnilsen.com
// @description Aftenposten uten sport
// @include http://www.aftenposten.no/*
// @include https://www.aftenposten.no/*
// @include http://www.ba.no/
// @include http://www.bt.no/*
// @include https://www.vg.no/
// @grant none