I hereby claim:
- I am kimyoutora on github.
- I am kang (https://keybase.io/kang) on keybase.
- I have a public key whose fingerprint is F854 4A74 12A5 0377 D70F A48B 0586 5B9E EEE1 96EB
To claim this, I am signing this object:
I hereby claim:
To claim this, I am signing this object:
| class BitMaskNode | |
| attr_accessor :children, :name | |
| attr_reader :mask | |
| def initialize(name, bitmask) | |
| @name = name | |
| @mask = bitmask | |
| @children = [] | |
| end |
| # 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 |
| THOUSANDS = ['', 'thousand', 'million', 'billion'] | |
| NUMS = { | |
| "0" => '', | |
| "1" => 'one', | |
| "2" => 'two', | |
| "3" => 'three', | |
| "4" => 'four', | |
| "5" => 'five', | |
| "6" => 'six', | |
| "7" => 'seven', |
| 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] |
| 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 |
| 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 |
| # 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 |
| # 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| |
| 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) |