Skip to content

Instantly share code, notes, and snippets.

View digitalex's full-sized avatar

Alexander Mossin digitalex

View GitHub Profile
@digitalex
digitalex / ip_locator.rb
Created March 12, 2013 15:50
Based on a list of IP ranges and country (format: "<from> <to> <country>", look up where an IP is located.
class IPLocator
attr_accessor :ranges
def initialize(filename)
self.ranges = []
File.open(filename).each do |line|
if not line.start_with? '#'
start, stop, country = line.split
self.ranges << IPRange.new(start.to_i, stop.to_i, country)
@digitalex
digitalex / removehtml
Last active January 13, 2023 08:50
Delete all HTML comments in VIM
%s/<!--\_.\{-}-->//g
@digitalex
digitalex / levenshtein_k.java
Created November 29, 2012 12:33
Levenshtein w/diagonal stripe optimization
import java.util.*;
import java.io.*;
public class Dictionary {
private List<String> words;
public Dictionary(List<String> list) {
this.words = list;
}
@digitalex
digitalex / MovingAverage.groovy
Created November 20, 2012 09:28
Moving average over a fixed window size, constant-time operations.
/**
* Calculates average over the last X data points.
*
* All operations are constant time (O(1)).
*
* Note the possibility of overflow, if the sum
* of the last X data points exceeds Float.MAX_VALUE.
*/
class MovingAverage {
private final int maxSize
@digitalex
digitalex / draw.rb
Created November 16, 2012 12:30
2 player tournament draw
players = ['sergei','birger','vincent','geir','harald','alex']
players.shuffle.each_slice(2) do |a,b|
puts "#{a} - #{b}"
end
@digitalex
digitalex / group.sh
Created October 23, 2012 13:04
GROUP BY in awk
awk -F'\t' '{ if(a[$1]) a[$1]=a[$1]","$2; else a[$1]=$2;} END {for (i in a) print i, a[i];}' OFS='\t' clickthroughs.tsv | sort > clickthroughs2.tsv
@digitalex
digitalex / cumulativeMovingAverage.java
Created September 24, 2012 12:50
Cumulative Moving Average
class CumulativeMovingAverage {
int n = 0;
double average = 0.0;
public double add(double x) {
return average += (x - average) / ++n;
}
}
class TopList<E extends Comparable> {
@Delegate
private List items
private int maxSize
private Comparator<E> comparator = new ReverseComparator()
TopList(int maxSize) {
this.maxSize = maxSize
this.items = new ArrayList(maxSize)
@digitalex
digitalex / CamelTosnake_case.groovy
Created August 3, 2012 13:17
CamelTosnake_case
public static String camelToSnakeCase(String camelCased) {
def snakeCased = new StringBuilder()
for (char c : camelCased) {
if (c.isUpperCase()) {
snakeCased.append('_')
}
snakeCased.append(c.toLowerCase())
}
@digitalex
digitalex / showDeleted.sh
Created May 23, 2012 10:05
Show deleted code
git log -p -SmyCode -- . | grep -A20 "^\-.*myCode.*$" | cut -c 2-