Skip to content

Instantly share code, notes, and snippets.


Jonas Elfström jonelf

View GitHub Profile
jonelf / gist:639589
Last active Sep 29, 2020
Binary tree with some boundary rules
View gist:639589
require 'rubygems'
require 'graphviz'
@min_level = 1
@max_level = 12
@max_depth = 10
start_level = 6
@g =, :type => "strict digraph" )
jonelf / gist:908911
Created Apr 7, 2011
Mihai asked an interesting question on my blog on how to analyze Base64 encoded and encrypted data and this code is the result of me starting to think about that.
View gist:908911
# 2011-04-08
require 'base64'
def caesar(text,n)
alphas=('A'..'Z').to_a*2'A-Z', alphas[n..n+26].join)
def histogram(message)
jonelf / gist:912723
Created Apr 10, 2011
A follow-up to now it's showing the histogram for each slice of 3 characters.
View gist:912723
require 'base64'
def histogram(arr)
arr.inject( { |h, k| h[k] += 1; h}
def graph_histogram(h, skip_less_than){|v| v[1]>=skip_less_than}.sort_by{|kv| kv[1]*-1}.
map{|a| "#{a[0]} |#{('+'*a[1])}#{a[1].to_s}\n"}.
jonelf / gist:914326
Created Apr 11, 2011
Sudoku solver refactored from Luddite Geek and based on the Python solution by Peter Norvig.
View gist:914326
#!usr/bin/env ruby
# 2011-04-11
# Sudoku solver refactored from Luddite Geek
# and that in turn is a translation from the Python solution by Peter Norvig
## Throughout this program we have:
## r is a row, e.g. 'A'
## c is a column, e.g. '3'
jonelf / gist:927782
Created Apr 19, 2011
Sudoku solver in CoffeScript based on the Python solution by Peter Norvig
View gist:927782
# Sudoku solver in CoffeeScript based on the Python solution by Peter Norvig
# 2011-04-19
# Throughout this program we have:
# r is a row, e.g. 'A'
# c is a column, e.g. '3'
# s is a square, e.g. 'A3'
# d is a digit, e.g. '9'
# u is a unit, e.g. ['A1','B1','C1','D1','E1','F1','G1','H1','I1']
jonelf / gist:1029099
Created Jun 16, 2011
Kaprekar's constant
View gist:1029099
[i, o, n, e] = [0, '', prompt('Enter a number between 1 and 9998 whose digits are not all identical'), '']
while n!=6174
n = e + n
q = (n='0'+n if !n[3]) for x in [0..2]
n ?= q
o+=n+"\n" if i
a = n.split(e).sort().join(e)
n = a.split(e).reverse().join(e)
o += n + ' - ' + a + ' = '
jonelf / gist:1054098
Created Jun 29, 2011
Self-descriptive numbers
View gist:1054098
# Searches for self-descriptive numbers
module Enumerable
def all_with_index?
each_with_index{|e,i| return false unless yield(e,i)}
View gist:1335248
(import (javax.swing JFrame JPanel)
(java.awt Color Dimension Graphics)
(java.awt.image BufferedImage WritableRaster))
(defn construct-frame [width height panel]
"Creates and displays a JFrame of the given dimensions with
the panel added to it"
(let [frame (JFrame.)]
(.setPreferredSize panel (Dimension. width height))
(doto frame
jonelf / gist:2009196
Created Mar 9, 2012
The other one is also a girl.
View gist:2009196
# Ruby
# Suppose Mr. and Mrs. Smith tell you they have two children, one of
# whom is a girl. What is the probability that the other is a girl?
1.upto(10) do |n|
siblings = []
# Create a bunch of randomized pairs of children
1.upto(10000) do
child1 = rand(2) == 0 ? :boy : :girl
child2 = rand(2) == 0 ? :boy : :girl
jonelf / gist:2011028
Created Mar 10, 2012
PLaying with conditional probabilities
View gist:2011028
# You've been told that the Larsson family has four children
# and that three of them are girls. What's that chance that
# the fourth one is also a girl?
# This is a brute force way to settle that.
sibling_count = 4
1.upto(10) do |n|
families = []
# Create a bunch of randomized families
1.upto(sibling_count * 5000) do