Skip to content

Instantly share code, notes, and snippets.

View chadbrewbaker's full-sized avatar

Chad Brewbaker chadbrewbaker

View GitHub Profile
@chadbrewbaker
chadbrewbaker / suff.rb
Created February 14, 2013 15:17
DNA suffix array enumeration
def cat (letter, strlist)
strlist.each_index{|i| strlist[i] = letter+strlist[i] }
strlist
end
def dna(len)
if (len ==1)
#return ["a", "t", "c","g"]
return ["a", "t"]
end
@chadbrewbaker
chadbrewbaker / queue_prod.rb
Last active December 21, 2015 04:59
Demonstration of how to keep the product of a queue over a semigroup in amortized O(1) time
#Demonstration of how to keep the product of a queue over a semigroup in amortized O(1) time
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
inbox = []
inprod =[]
outbox = []
outprod = []
@chadbrewbaker
chadbrewbaker / scraper_example.rb
Created September 13, 2013 18:33
Extremely simple web scraper example for Mr. Blue
require 'rubygems'
require 'watir'
$browser = Watir::Browser.new
$browser.maximize
$browser.goto 'http://www.fbi.gov/wanted/topten/topten-history'
$browser.spans(:class, "blackgraphtx").each{|elt|
puts elt.text
}
@chadbrewbaker
chadbrewbaker / median.rb
Last active September 29, 2018 17:01
Implementation of the linear time median of medians algorithm.
# Gist to go with http://www.austinrochford.com/posts/2013-10-28-median-of-medians.html
# Chad Brewbaker 10/28/2013
# https://news.ycombinator.com/item?id=6628474
def median_5(list, i = list.length/2)
return list.sort[i]
end
def median(list, i = list.length/2)
if (list.length <= 5)
@chadbrewbaker
chadbrewbaker / fib_dice.rb
Created November 15, 2013 03:25
quick fib generator and dice match for PJ
fibs = Enumerator.new do |yielder|
a = b = 1
yielder.yield 1
yielder.yield 1
(0..1.0/0).each do |number|
ret = a + b
b = a
a = ret
yielder.yield ret
end
@chadbrewbaker
chadbrewbaker / minsat.rb
Created November 17, 2013 01:41
Simple minsat
(0..6).each do |i|
[1,2,3,4,5,6].combination(i).each{ |x|
puts x.inspect
#test if this subset matches predicate
}
end
@chadbrewbaker
chadbrewbaker / hook.rb
Created December 10, 2013 03:49
Hook algorithm
# Hook and shortcut from AN EFFICIENT PARALLEL BICONNECTIVITY ALGORITHM
#Tarjan and Vishkin
# http://www.umiacs.umd.edu/users/vishkin/TEACHING/ENEE759KS12/TV85.pdf
def partitions(trans)
parent =Array.new()
0.upto(trans.length-1) do |index|
parent.push(index)
end
0.upto(trans.length-1) do |outer_index|
0.upto(trans.length-1) do |index|
@chadbrewbaker
chadbrewbaker / local_k_consistent_magma.rb
Created December 17, 2013 16:28
Ruby code for studying locally k-consistent magmas. Any permutation of k elements will multiply to the same value, but may not for k+1 elements
#Ruby code for studying locally k-consistent magmas.
# Any permutation of k elements will multiply to the same value, but may not for k+1 elements
#triangular numbers, the number of entries above the diagonal in an nxn matrix
#http://oeis.org/A000217
triangulars = Enumerator.new do |yielder|
n =0
yielder.yield n
while true
@chadbrewbaker
chadbrewbaker / ins_del.rb
Created January 13, 2014 16:30
Enumerating the number of insert delete BSTs on n elements
#Chad Brewbaker 1/13/2014
#Counting the number of insert delete binary search trees
#http://cstheory.stackexchange.com/questions/20533/height-of-randomly-built-binary-search-tree-by-insert-and-delete?noredirect=1#comment54289_20533
# Seems to be http://oeis.org/A000680
def legal_tree(arr)
1.upto(arr.length/2) do |i|
ins= arr.rindex([i, 0].to_s)
del= arr.rindex([i, 1].to_s)
if ins > del
@chadbrewbaker
chadbrewbaker / fast_factorial.rb
Last active January 3, 2016 09:49
fast_factorial
#Worksheet for fast factorial algorithms, see http://cstheory.stackexchange.com/questions/20594/factoriality-testing-algorithms-and-their-efficiency
#Chad Brewbaker
#January 15, 2014
#http://www.cecm.sfu.ca/personal/pborwein/PAPERS/P29.pdf
#http://www.luschny.de/math/factorial/FastFactorialFunctions.htm
#http://cs.stackexchange.com/questions/14456/factorial-algorithm-more-efficient-than-naive-multiplication
require 'benchmark'
require 'prime'