Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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.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 / 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 / quick-sort.hs
Created May 15, 2012 09:36
haskell quick sort
qsort :: Ord a => [a] -> [a]
qsort [] = []
qsort (x:xs) = qsort lesser ++ [x] ++ qsort greater
where
lesser = filter (< x) xs
greater = filter (>= x) xs
@mmagm
mmagm / pqueue.rb
Created May 15, 2012 07:59
priority queue
class PQueue
def initialize
@a = []
@heapsize = 0
end
def left(i)
2 * i + 1
end
@mmagm
mmagm / heap-sort.hs
Last active February 2, 2018 07:21
haskell heap sort
import Data.List
swap :: Int -> Int -> [a] -> [a]
swap i j xs | i == j = xs
swap i j xs | otherwise = initial ++ (xs !! b) : middle ++ (xs !! a) : end
where [a,b] = sort [i,j]
initial = take a xs
middle = take (b-a-1) (drop (a+1) xs)
end = drop (b+1) xs
class Array
def left(i)
2 * i + 1
end
def right(i)
2 * i + 2
end
def heapify(i, heapsize)