Skip to content

Instantly share code, notes, and snippets.

@Jberczel
Jberczel / friendly_dates_test.rb
Last active August 29, 2015 14:16
test cases for reddit daily programmer challenge #205
require_relative 'friendly_date_ranges'
require 'minitest/autorun'
class FriendlyDatesTest < Minitest::Test
def test_teen_day_formatting
input = '2015-07-11 2015-08-12'
results = FriendlyDates.new(input)
assert_equal '11th', results.date1.day
assert_equal '12th', results.date2.day
end
@Jberczel
Jberczel / bi_daily_scheduler.rb
Created December 13, 2014 15:25
heroku scheduler workaround
class BiDailyScheduler < HerokuScheduler
def should_run?
(Time.now.hour % 12) == hour
end
end
@Jberczel
Jberczel / Post.rb
Last active August 29, 2015 14:11
Extract scrape logic out of models
class Post < ActiveRecord::Base
extend PostUtils
default_scope { order(:id) }
end
@Jberczel
Jberczel / Post.rb
Last active August 29, 2015 14:10
Rails model with lots of scrape logic
class Post < ActiveRecord::Base
extend PostUtils
default_scope { order(:id) }
end
@Jberczel
Jberczel / call_center.rb
Created December 4, 2014 14:49
Cracking the Code Interview 7.2 (OOP)
# Imagine you have a call center with three levels of employees: fresher, technical lead (TL),
# product manager (PM). There can be multiple employees, but only one TL or PM
# An incoming telephone call must be allocated to a fresher who is free. If a fresher
# can’t handle the call, he or she must escalate the call to technical lead. If the TL is
# not free or not able to handle it, then the call should be escalated to PM. Design the
# classes and data structures for this problem. Implement a method getCallHandler().
class CallHandler
attr_reader :product_manager, :tech_lead, :freshers, :calls
@Jberczel
Jberczel / floor_plan.rb
Last active August 29, 2015 14:10
implementation of flood fill algorithm
class FloorPlan
attr_reader :input, :layout, :rows, :cols
EMPTY = '.'
VISITED = '-'
def initialize(file)
@input = format(file)
@layout = format(file)
@rows = @layout.size
@Jberczel
Jberczel / benchmark.rb
Last active August 29, 2015 14:09
WordProblem Benchmarking
require_relative 'other_word_problem'
require_relative 'word_problem'
require 'benchmark'
TEST_COUNT = 10000
Benchmark.bmbm do |b|
b.report("myWordy") do
TEST_COUNT.times do |i|
@Jberczel
Jberczel / linked_list.rb
Created November 12, 2014 18:18
Single Linked List - ruby implementation for Cracking the Code Interview Questions
Node = Struct.new(:value, :next)
class LinkedList
def initialize(*values)
@head = Node.new(values.shift, nil)
values.each { |v| add(v) }
end
def add(value)
current = @head