Skip to content

Instantly share code, notes, and snippets.

View mortenjohs's full-sized avatar

Morten Johannes Ervik mortenjohs

  • International Agency for Research on Cancer
  • Lyon, France
View GitHub Profile
@mortenjohs
mortenjohs / pdf2csv.rb
Created September 30, 2012 12:20
A simple script to grab tabular data from a PDF
require 'pdf-reader'
require 'csv'
pdf_reader = PDF::Reader.new("input.pdf")
csv = CSV.open("output.tsv","wb", {:col_sep => "\t"})
area = ""
pdf_reader.pages[42..69].each do |page|
page.text.each_line do |line|
if /^[a-z|\s]*$/i=~line
@mortenjohs
mortenjohs / gist:4129962
Created November 22, 2012 08:20
Oneliner
number.chars.map(&:to_i).each_cons(5).map{|a| a.inject(&:*)}.max
class Array
def count_by function
map = Hash.new(0)
self.each { |e| map[e.send(function)] += 1 }
map
end
end
module Enumerable
def count_by (&block)
Hash[ self.group_by { |e| yield e }.map { |key, list| [key, list.length] } ]
end
end
a = ["test", "tull", "t\U+FFC3\U+FFB8ys"]
=> ["test", "tull", "tys"]
$ rvm get latest
$ rvm pkg install readline --verify-downloads 1
$ rvm reinstall all --force --verify-downloads 1
a = ["test", "tull", "tøys"]
=> ["test", "tull", "tøys"]
@mortenjohs
mortenjohs / k-means.rb
Last active October 13, 2015 19:07
k-means hack in Ruby
begin
require 'gnuplot'
rescue LoadError
puts "No usable Gnuplot..."
end
class Point
def initialize(coords)
@coords = coords
end
@mortenjohs
mortenjohs / partitions.rb
Last active December 10, 2015 02:48
A small script to automate the creation of groups in a class of students - to maximize the variation within each group, but minimize the difference between them. (As seen on: http://m635j520.blogspot.fr/2012/12/partitioning-or-students-into-n-groups.html )
require 'csv'
require 'pp'
require './names.rb'
require './countries.rb'
number_of_runs = 10000
number_of_groups = 4
participants_file = "participants.txt"
col_sep = "\t"
encoding = "UTF-8"
require 'benchmark'
require 'benchmark/ips'
array = []
1000.times { array << rand(1000) }
N = 10000
module Enumerable
def count_by_function function
map = Hash.new(0)