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 'clever-ruby' | |
class CleverClient | |
include Clever | |
attr_reader :sections | |
def initialize | |
Clever.configure do |config| | |
config.api_key = 'DEMO_KEY' | |
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
# 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) |
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 'test-unit' | |
class OutOfMemoryBoundary < StandardError | |
end | |
class HashKeyOutOfBound < StandardError | |
end | |
class FakeHash | |
def initialize(size) | |
@body = Array.new(size) |
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
# 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.) |
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
# 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 |
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 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 |
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
var Animal = function(name){ | |
this.name = name; | |
}; | |
Animal.prototype.sayHi = function(){ | |
console.log('rawrrrrr') | |
} | |
var animal = new Animal('Bob') |
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
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); |
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 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 |
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
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]; |