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
>> Version | |
=> "0.8.2" | |
>> MongoMapper::Version | |
=> "0.8.2" |
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 'chronic_duration' | |
ps0=Post.all(:conditions => ["? <= created_at and created_at <= ? ", Time.parse("06/29/2010"), Time.parse("07/05/2010")], :include => :replies) | |
ps1=Post.all(:conditions => ["? <= created_at and created_at <= ? ", Time.parse("07/06/2010"), Time.parse("07/12/2010")], :include => :replies) | |
ps2=Post.all(:conditions => ["? <= created_at and created_at <= ? ", Time.parse("07/13/2010"), Time.parse("07/19/2010")], :include => :replies) | |
ps3=Post.all(:conditions => ["? <= created_at and created_at <= ? ", Time.parse("07/20/2010"), Time.parse("07/26/2010")], :include => :replies) |
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
# randomly shuffle cards in-place | |
# Fisher-Yates Shuffle | |
# Use the back of the array for storing the | |
# randomly picked card and move your way to the front | |
class Array | |
def shuffle | |
length = self.size | |
array = self.dup | |
(length - 1).step(0, -1).each do |i| |
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
# Partition-based selection algorithm to find the | |
# median of an array in O(N) time | |
# Note that this is very similar to quicksort but since | |
# we know which partition our element lies in, we are reducing | |
# the problem space by 1/2 at every step on average and thus, | |
# N + 1/2 x N + 1/4 x N + 1/8 x N + ... | |
# This sequence leads to N x (sum of series from 1...1/inf) = | |
# N x (1 + 1) = 2N = O(N) running time | |
class Array | |
def median |
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 sqrt(num, precision, beg=0, final=num) | |
final = 1 if num < 1 # handle cases where the squareroot is > num i.e. sqrt(0.1) ~ 0.3 | |
begin | |
guess = (beg + final) / 2.0 | |
diff = num - (guess ** 2) | |
if diff.abs <= precision | |
return guess | |
elsif diff > 0 |
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 occurrences(sorted_array) | |
puts "occurrences: #{sorted_array.inspect}" | |
count = 0 | |
sorted_array.each_with_index do |num, index| | |
if index + 1 >= sorted_array.size || num != sorted_array[index+1] | |
puts "#{num}: #{count+1}" | |
count = 0 | |
else | |
count += 1 |
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 fib(n) | |
cache = {} | |
n.times do |i| | |
if i == 0 | |
cache[0] = 0 | |
elsif i == 1 | |
cache[1] = 1 | |
else | |
cache[i] = cache[i-1] + cache[i-2] |
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
THOUSANDS = ['', 'thousand', 'million', 'billion'] | |
NUMS = { | |
"0" => '', | |
"1" => 'one', | |
"2" => 'two', | |
"3" => 'three', | |
"4" => 'four', | |
"5" => 'five', | |
"6" => 'six', | |
"7" => 'seven', |
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
# my attempt | |
def powerset(set) | |
all_sets = [[]] | |
set.each do |elem| | |
subsets = [] | |
all_sets.each do |subset| | |
subsets << subset + [elem] | |
end | |
all_sets += subsets | |
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
class BitMaskNode | |
attr_accessor :children, :name | |
attr_reader :mask | |
def initialize(name, bitmask) | |
@name = name | |
@mask = bitmask | |
@children = [] | |
end |
OlderNewer