View gist:639589
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'graphviz' | |
@min_level = 1 | |
@max_level = 12 | |
@max_depth = 10 | |
start_level = 6 | |
@g = GraphViz.new(:G, :type => "strict digraph" ) |
View gist:908911
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
View gist:912723
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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"}. |
View gist:914326
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!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' |
View gist:927782
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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'] |
View gist:1029099
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 |
View gist:1054098
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
View gist:1335248
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
View gist:2009196
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
View gist:2011028
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
OlderNewer