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:639589
Last active September 29, 2020 09:32
Binary tree with some boundary rules
require 'rubygems'
require 'graphviz'
@min_level = 1
@max_level = 12
@max_depth = 10
start_level = 6
@g = GraphViz.new(:G, :type => "strict digraph" )
@jonelf
jonelf / gist:908911
Created April 7, 2011 22:21
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. http://alicebobandmallory.com/articles/2009/09/16/breaking-simple-ciphers
# 2011-04-08 jonelf@gmail.com
require 'base64'
def caesar(text,n)
alphas=('A'..'Z').to_a*2
text.tr('A-Z', alphas[n..n+26].join)
end
def histogram(message)
@jonelf
jonelf / gist:912723
Created April 10, 2011 21:08
A follow-up to https://gist.github.com/908911 now it's showing the histogram for each slice of 3 characters.
require 'base64'
def histogram(arr)
arr.inject(Hash.new(0)) { |h, k| h[k] += 1; h}
end
def graph_histogram(h, skip_less_than)
h.to_a.select{|v| v[1]>=skip_less_than}.sort_by{|kv| kv[1]*-1}.
map{|a| "#{a[0]} |#{('+'*a[1])}#{a[1].to_s}\n"}.
@jonelf
jonelf / gist:914326
Created April 11, 2011 20:57
Sudoku solver refactored from Luddite Geek and based on the Python solution by Peter Norvig.
#!usr/bin/env ruby
# 2011-04-11 jonelf@gmail.com
# Sudoku solver refactored from Luddite Geek http://theludditegeek.com/blog/?page_id=92
# and that in turn is a translation from the Python solution by Peter Norvig
# http://norvig.com/sudoku.html
## Throughout this program we have:
## r is a row, e.g. 'A'
## c is a column, e.g. '3'
@jonelf
jonelf / gist:927782
Created April 19, 2011 13:44
Sudoku solver in CoffeScript based on the Python solution by Peter Norvig
# Sudoku solver in CoffeeScript based on the Python solution by Peter Norvig
# http://norvig.com/sudoku.html
# 2011-04-19 jonelf@gmail.com
#
# 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
jonelf / gist:1029099
Created June 16, 2011 12:02
Kaprekar's constant
[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 + ' = '
n-=a
@jonelf
jonelf / gist:1054098
Created June 29, 2011 15:32
Self-descriptive numbers
# Searches for self-descriptive numbers
# http://en.wikipedia.org/wiki/Self-descriptive_number
module Enumerable
def all_with_index?
each_with_index{|e,i| return false unless yield(e,i)}
true
end
end
@jonelf
jonelf / gist:1335248
Created November 2, 2011 23:11
Leif in Clojure
(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
jonelf / gist:2009196
Created March 9, 2012 23:11
The other one is also a girl.
# 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?
# http://www.whydomath.org/Reading_Room_Material/ian_stewart/9609.html
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
jonelf / gist:2011028
Created March 10, 2012 10:09
PLaying with conditional probabilities
# 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