Skip to content

Instantly share code, notes, and snippets.

@mac-r
mac-r / r_sample.r
Created July 28, 2013 20:15
R Sample
# INCLUDES <- "web/graphics"
# FILETYPE <- "html"
# Upper case first letter of string
# This comes from the examples of some R function.
#
# @keyword internal
firstUpper <- function(s) {
paste(toupper(substring(s, 1,1)), substring(s, 2), sep="")
}
@mac-r
mac-r / quicksort.rb
Created June 25, 2013 06:42
quick sort
def quicksort(array, from=0, to=nil)
# Sort the whole array, by default
to = array.count - 1 if to == nil
# Done sorting
return if from >= to
# Take a pivot value, at the far left
pivot = array[from]
require 'net/http'
require 'uri'
###################################################
# EDIT YOUR REQUEST HERE #
###################################################
# for phrases wirte like that: word = "my+phrase"
word = "New+Year"
month = 1
@mac-r
mac-r / gist:4239669
Created December 8, 2012 10:03
HRoadmap for HighlandDB, opensource database for Ruby apps
Hi, everyone!
During the last month I've been working hard to roll out HighlandDB, lightweight
NoSQL database for Ruby applications. It gets installed in the application
folder and provides user with a clear API.
Here are the links:
- http://mac-r.github.com/highland/ (official website)
- https://github.com/mac-r/highland (Github repo)
@mac-r
mac-r / benchmarking.rb
Created August 14, 2012 22:04
Binary Heaps in the Context of Prioritization #3
n = 220
k = 1
Benchmark.bm do |x|
n.times do
x.report("Heaps_#{k*1000}x#{k*100}:") { heap_extractor(k*1000, k*100) }
x.report("Sort_#{k*1000}x#{k*100}:") { sorted_array_shifter(k*1000, k*100) }
k += 1
end
end
@mac-r
mac-r / heap_module.rb
Created August 14, 2012 22:02
Binary Heap Module
module HeapModule
def self.sort(keys)
sort!(keys.clone)
end
def self.sort!(keys)
build_max_heap(keys)
(keys.size-1).downto(1) do |i|
keys[0], keys[i] = keys[i], keys[0]
max_heapify(keys, i, 0)
@mac-r
mac-r / binary_heap.rb
Created August 14, 2012 22:01
Binary Heaps in the Context of Prioritization #2
def heap_extractor(length, n)
random_array = (1..length).to_a.inject(Array.new) {|arr, el| el = rand(1..el); arr << el;}
array = HeapModule.build_max_heap(random_array)
n.times do
HeapModule.extract_maximum(array)
end
end
@mac-r
mac-r / sorted_array.rb
Created August 14, 2012 21:59
Binary Heaps in the Context of Prioritization #1
def sorted_array_shifter(length, n)
random_array = (1..length).to_a.inject(Array.new) do |arr, el|
el = rand(1..el); arr << el;
end
array = random_array.sort {|x,y| y<=>x }
n.times do
array.shift
end
end
test
@mac-r
mac-r / gist:2655592
Created May 10, 2012 20:13
Ornstein-Unlenbeck in Maple
restart;
with(Statistics):
STDNORM:=proc( ) local ij,U1,U2,S,B,X1;global i,X2;
if type(i,boolean)then
if (i) then
i:=not(i);X2;
else
for ij from 1 to infinity do
U1:=evalf(2.0*rand()/10^12)-1.0;
U2:=evalf(2.0*rand()/10^12)-1.0;