Skip to content

Instantly share code, notes, and snippets.

View louismullie's full-sized avatar

L louismullie

View GitHub Profile
@louismullie
louismullie / benchmark-mini-parser-vs-regex.rb
Created February 14, 2012 17:55
Benchmark: Mini-parser or Regex?
require 'benchmark'
Benchmark.bm do |x|
scores = %w{00100300800 32004300X 00(11)34000 0000(15)000X 0000(15)000(13) 10(18)47(11)8(10)3}
x.report do
1_000.times do
games = []
@louismullie
louismullie / benchmark-n-best-sentences.rb
Created February 27, 2012 03:24
Benchmark: Finding N keys with highest value in hash, keeping order
require 'benchmark'
def extract1(sentences, n)
sentences.map(&:reverse).each_with_index
.sort_by(&:first).reverse
.take(n)
.sort_by(&:last)
.map { |(_,s),_| s }
end
@louismullie
louismullie / php-mail-form.php
Created March 12, 2012 13:47
PHP Mail Form
<!-- CONTACT.HTML -->
<form method='post' action='mailform.php'>
Email: <input name='email' type='text' /><br />
Subject: <input name='subject' type='text' /><br />
Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type='submit' />
</form>
@louismullie
louismullie / gist:2089431
Created March 19, 2012 01:33
Space-filling with an arbitrary number of circles with predefined radii
/*
*
* Space-filling with circles
*
* Authors: Kris Van Bael.
* Small modifications by Louis Mullie.
*
*
* Usage:
*
@louismullie
louismullie / domain-name-catching.rb
Created April 15, 2012 01:48
Expired Domain Name Catching Script
domain = "www.test.com"
email = "[email protected]"
password = "test"
require 'whois'
require 'gmail'
client = Whois::Client.new
while true
@louismullie
louismullie / gist:2394767
Created April 15, 2012 20:52
Sinatra Servlet That Forks Background Processes and Allows Polling Them Through DRb
require 'sinatra'
require 'fileutils'
require 'cgi'
require 'drb'
TmpDir = './tmp/'
ServiceFile = 'services.txt'
def local_get(url)
@louismullie
louismullie / hash-to-struct.rb
Created June 17, 2012 17:30
Convert nested hashes to nested structs
def hash_to_struct(hash)
return hash if hash.keys.
select { |k| !k.is_a?(Symbol) }.size > 0
struct = Struct.new(
*hash.keys).new(*hash.values)
hash.each do |key, value|
if value.is_a?(Hash)
struct[key] =
self.hash_to_struct(value)
end
@louismullie
louismullie / ruby-project-stats.rb
Created June 17, 2012 20:13
Count the number of files, lines of code and comments in a Ruby project.
o = 0 # Number of files
n = 0 # Number of lines of code
m = 0 # Number of lines of comments
files = Dir.glob('/path/to/dir/**/*.rb')
files.each do |f|
next if FileTest.directory?(f)
o += 1
i = 0
lines = []
@louismullie
louismullie / pi-monte-carlo.py
Created September 23, 2012 07:31
Monte Carlo Estimation of PI in Python
import random as r
import math as m
# Number of darts that land inside.
inside = 0
# Total number of darts to throw.
total = 1000
# Iterate for the number of darts.
for i in range(0, total):
require 'thread'
$done = {}
$size = 2
$mutex = Mutex.new
$resource = ConditionVariable.new
def register(thread)
id = thread.object_id
$mutex.synchronize do