Skip to content

Instantly share code, notes, and snippets.

@lordhumunguz
lordhumunguz / clever.rb
Last active December 30, 2015 05:39
Clever API - average section size Dependencies: clever-ruby, VCR, RSpec, Fakeweb
require 'clever-ruby'
class CleverClient
include Clever
attr_reader :sections
def initialize
Clever.configure do |config|
config.api_key = 'DEMO_KEY'
end
@lordhumunguz
lordhumunguz / rpn_calculator.rb
Last active December 30, 2015 05:19
RPN Calculator implemented using Ruby 2.0 and Rspec 2.14.7 Note: the calculator only deals with Integers. If decimals are desired, I would use BigDecimal instead of Float.
# NOTE: This calculator only deals with Integers. If decimal operations are
# desired, I would use BigDecimal instead of Float.
class RPNCalculator
attr_reader :should_check_input
def initialize
@should_check_input = true
end
def calculate(expression)
@lordhumunguz
lordhumunguz / hash_tdd.rb
Created October 16, 2013 22:11
hash implementation, tdd'ed
require 'test-unit'
class OutOfMemoryBoundary < StandardError
end
class HashKeyOutOfBound < StandardError
end
class FakeHash
def initialize(size)
@body = Array.new(size)
@lordhumunguz
lordhumunguz / traveling_salesman.rb
Created June 17, 2013 21:47
Traveling Salesman
# This is a brute force implementation of the traveling salesman problem.
# The cities are situated within a 20 by 20 grid, ranging from -10 to 10 on both
# the x and y axis. The distance between any two cities are calculated using the
# Pythagorean Theorem (c = Math.sqrt(a**2 + b**2)). The algorithm calculates the distances
# of all possible routes and picks the shortest one. This is not a scalable solution
# since the number of possibilities is the factorial of the number of cities. Some of
# the possible optimizations could include a more efficient Array#generate_permutations method,
# clustering of nearby nodes into one node, and early elimination of inprobable routes (choosing the farthest city
# right after the starting city, etc.)
@lordhumunguz
lordhumunguz / tsm.rb
Created June 17, 2013 17:36
Traveling Salesman Brute Force
# will refactor asap permutation implementation
class Array
def generate_permutations
return [self] if self.size < 2
permutation = []
self.each do |element|
(self - [element]).generate_permutations.each do |perm_accumulator|
permutation.push ([element] + perm_accumulator)
end
end
@lordhumunguz
lordhumunguz / perm.rb
Created June 17, 2013 16:38
permutation implementation for array
class Array
def generate_permutations
return [self] if self.size < 2
permutation = []
self.each do |element|
(self - [element]).generate_permutations.each do |perm_accumulator|
permutation.push ([element] + perm_accumulator)
end
end
permutation
var Animal = function(name){
this.name = name;
};
Animal.prototype.sayHi = function(){
console.log('rawrrrrr')
}
var animal = new Animal('Bob')
@lordhumunguz
lordhumunguz / hash.js
Last active December 18, 2015 07:29
Todo: constrict the size of the memory array to more closely simulate real computer memory. yell at the user if the user goes out of bound.
var memory = [];
var Hash = function(){
this.keys = [];
};
Hash.prototype.getIndex = function(key){
var sum = 0
for (var i = 0; i < key.length; i++){
sum += key.charCodeAt(i);
class Car
attr_reader :ignited, :direction
def initialize(brand, model, args={})
@brand = brand
@model = model
@color = args[:color]
@mpg = args[:mpg]
@ignited = false
@direction = nil
end
var binarySearch = function(list1, item){
var list = list1.sort();
var startIndex = 0;
var endIndex = list.length-1;
var midIndex = Math.ceil(list.length/2);
while(startIndex !== endIndex){
midIndex = Math.ceil((endIndex+1)/2);
var midVal = list[midIndex];