Skip to content

Instantly share code, notes, and snippets.

@mmagm
mmagm / quick-sort.rb
Created May 15, 2012 10:41
quick sort
class Array
def middle
return self[self.length / 2]
end
def qsort
return self.dup if size <= 1
l,r = partition { |x| x <= self.middle }
c,l = l.partition { |x| x == self.middle }
l.qsort + c + r.qsort
@mmagm
mmagm / binary-tree.rb
Created May 25, 2012 08:39
binary tree
class BinaryTree
class Node
attr_reader :value
attr_accessor :parent
attr_accessor :right
attr_accessor :left
def initialize(value = nil)
@value = value
end
@mmagm
mmagm / binary-tree-set.rb
Created May 26, 2012 07:46
binary tree set
class BinaryTreeSet
class Node
attr_accessor :value
attr_accessor :parent
attr_accessor :right
attr_accessor :left
def initialize(value = nil)
@value = value
end
@mmagm
mmagm / binary-tree.hs
Created May 26, 2012 12:01
haskell binary tree
data BinaryTree a = EmptyTree | Node a (BinaryTree a) (BinaryTree a)
deriving (Show)
treeInsert :: Ord a => a -> BinaryTree a -> BinaryTree a
treeInsert el EmptyTree = Node el EmptyTree EmptyTree
treeInsert el (Node a left right)
| el == a = Node el left right
| el < a = Node a (treeInsert el left) right
| el > a = Node a left (treeInsert el right)
@mmagm
mmagm / image-grab.rb
Created June 1, 2012 10:23
grab images from page
require 'rubygems'
require 'nokogiri'
require 'eventmachine'
require 'em-synchrony'
require 'em-synchrony/em-http'
if ARGV.length < 2
puts 'grab url path'
exit
end
@mmagm
mmagm / word-cons.rb
Last active October 6, 2015 21:58
word constructor
require 'set'
class Coord
attr_accessor :x, :y, :n
def initialize(x, y, n)
self.x = x
self.y = y
self.n = n
end
@mmagm
mmagm / replace-rockets.el
Created December 4, 2012 07:26
replace rockets in ruby
(defun replace-rockets ()
(interactive)
(goto-char (point-min))
(while (re-search-forward ":\\([_A-Za-z0-9]+\\)[\t\n ]+=>" nil t)
(replace-match "\\1:")))
class InfiniteCycleEnumerator
def self.enum(num)
Enumerator.new do |y|
idx = 0
loop do
y << idx % num
idx = idx + 1
end
end
end
class Node
attr_accessor :next
attr_accessor :value
def initialize(value)
@next = nil
@value = value
end
end
require 'find'
class Expression
def |(other)
Or.new(self,other)
end
def &(other)
And.new(self,other)
end