Skip to content

Instantly share code, notes, and snippets.

View arnab's full-sized avatar

Arnab Deka arnab

View GitHub Profile
@arnab
arnab / umpiring-schdeule-generator.rb
Created March 8, 2010 22:29
randomnize a few players
#!/usr/bin/env ruby -wKU
players = "Parag
Arnab
Vinoth
Sreeram
Ajay
Nazeer
Swapnil
Sridhar
@arnab
arnab / top_n_tags.rb
Created February 26, 2010 05:32
Given lots of tags, keep a running count of the top-n
require "pp"
class TagManager
def initialize(top_tags_to_keep)
@all_tags = Hash.new(0)
@top_tags = []
@top_tags_to_keep = top_tags_to_keep
end
def search(tag)
@arnab
arnab / russian-peasant-multiplication.rb
Created January 29, 2010 22:31
Simple implementation of Russian Peasant Multiplication with tests
# http://mathforum.org/dr.math/faq/faq.peasant.html
# http://en.wikipedia.org/wiki/Ancient_Egyptian_multiplication
module RussianPeasantMultiplication
def russian_peasant_multiply(b)
numbers_to_add = []
a, b = [self, b].sort #So we have the smaller number as the first
negative_operands = [a, b].select { |n| n < 0 }
result_should_be_negative = negative_operands.size.odd? # or negative_operands.size == 1
@arnab
arnab / sort_enums_by_given_custom_order.rb
Created December 31, 2009 00:46
Sort an enumerable (animals) given a specific order (animal_class_order). Important Lines: 22-24
animals = [
{ :name => 'Frog', :class => 'Amphibian' },
{ :name => 'Butterfly', :class => 'Arthropod' },
{ :name => 'Eagle', :class => 'Bird' },
{ :name => 'Seahorse', :class => 'Fish' },
{ :name => 'Dog', :class => 'Mammal' },
{ :name => 'Man', :class => 'Mammal' },
{ :name => 'Crocodile', :class => 'Reptile' },
]
# Note that Fish and Reptiles are missing from the following
def columnize(arr, columns)
size_in_group = arr.size / columns
if arr.size % columns != 0
size_in_group += 1
end
arr_grouped = [ ]
size_in_group.times do |m|
columns.times do |i|
arr_grouped[i] ||= []
@arnab
arnab / rails-find-joins.rb
Created November 2, 2009 04:01
If you want to load data from 2 tables (with a belongs_to/has_many relation)
metrics_with_inputs = Revision.find(
:all,
:conditions => { :id => self.revisions.map { |r| r.id } },
:select => 'revisions.metric, revisions.snap_date, revisions.updated_at,
inputs.author, inputs.as_of_date, inputs.units',
:joins => 'join inputs on inputs.revision_id = revisions.id',
:order => 'revisions.metric, revisions.snap_date, revisions.updated_at'
)