Skip to content

Instantly share code, notes, and snippets.

View jonelf's full-sized avatar

Jonas Elfström jonelf

View GitHub Profile
@jonelf
jonelf / gist:2127599
Created March 19, 2012 22:13
Labouchère system simulation in Ruby
# A Ruby version of the Labouchère system simulation in
# CoffeeScript by Anders Löfgren that can be found at
# https://github.com/gnidde/gniddegit/blob/master/roulette.coffee
# 2012-03-19 jonelf@gmail.com
TABLE_ZEROS = 1
MAX_BET = 24
RED_NUMBERS = [1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36]
class Player
@jonelf
jonelf / gist:2164553
Created March 22, 2012 21:04
Labouchère system simulation in Clojure
; A Clojure version of the Labouchère system simulation in
; CoffeeScript by Anders Löfgren that can be found at
; https://github.com/gnidde/gniddegit/blob/master/roulette.coffee
; Ruby version at https://gist.github.com/2127599
;
; I'm a complete beginner at Clojure so there's little to
; no chance that this code follows conventions and such.
; 2012-03-20 jonelf@gmail.com
(def table-zeros 1)
@jonelf
jonelf / gist:2357864
Created April 11, 2012 08:25
Project Euler one-liner
require'prime';s=500;n=s*(s+1)/2;i=s+1;while(n.prime_division.transpose[1].map{|n|n+1}.reduce(&:*)<s);n+=i;i+=1;end;n
@jonelf
jonelf / gist:2779846
Created May 24, 2012 06:41
A line of 100 airline passengers is waiting to board a plane.
=begin
A line of 100 airline passengers is waiting to board a plane. They
each hold a ticket to one of the 100 seats on that flight. (For
convenience, let's say that the nth passenger in line has a ticket for
the seat number n.)
Unfortunately, the first person in line is crazy, and will ignore the
seat number on their ticket, picking a random seat to occupy. All of
the other passengers are quite normal, and will go to their proper
seat unless it is already occupied. If it is occupied, they will then
find a free seat to sit in, at random.
require 'benchmark'
@properties = []
1.upto(10000) do
@properties << {
Strasse:"Oberdorfstrasse",
StrassenNr:6,
Plz:6277,
Ort:"Lieli",
@jonelf
jonelf / gist:2823871
Created May 29, 2012 10:11
Performance test of searching in array of dynamic objects
function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var houses = [];
for(var i = 0; i < 10000; i++) {
var house = {
Street:"Orchard Street",
StreetNumber:getRandomInt(1,20),
@jonelf
jonelf / gist:2823878
Created May 29, 2012 10:12
Performance test of searching in List of Dictionary
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Test
{
class Program
{
static void Main(string[] args)
{
@jonelf
jonelf / gist:3358287
Created August 15, 2012 09:50
Silly interpolation of two strings.
arr = ["imer".split(//), "mare".split(//)].transpose
0.upto(arr.length**2-1) {|n| print "De"; 0.upto(arr.length-1) {|m| print arr[m][n>>m & 1] }; puts "t"}
@jonelf
jonelf / gist:3423148
Last active August 2, 2017 20:17
De Bruijn sequence
# De Bruijn sequence
# Original implementation by Frank Ruskey (1994)
# translated to C by Joe Sawada
# translated to Ruby by Jonas Elfström (2009)
@n = 4
@k = 10
@a = [0]
@sequence = []
@jonelf
jonelf / gist:3613495
Created September 3, 2012 21:00
Produces colorized output of the Common Log Format
# Produces colorized output of the Common Log Format
# Usage: $ tail -f access.log | ruby colorpipe.rb
require 'rubygems'
require 'fastercsv'
require 'colorize'
require 'resolv'
colors = String.colors.reject {|c| c == :black || c == :light_cyan}
hosts = {}