This file contains hidden or 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
| 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 |
This file contains hidden or 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
| #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 = [] | |
This file contains hidden or 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 '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 | |
| } |
This file contains hidden or 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
| # 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) |
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| (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 |
This file contains hidden or 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
| # 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| |
This file contains hidden or 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 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 |
This file contains hidden or 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
| #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 |
This file contains hidden or 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
| #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' |