Skip to content

Instantly share code, notes, and snippets.

View durrellchamorro's full-sized avatar

Durrell Chamorro durrellchamorro

  • Fleetio
  • 06:44 (UTC -05:00)
View GitHub Profile
class WordProblem
def initialize(string)
@problem = string
end
def answer
operations = extract_operations @problem
fail ArgumentError, 'Valid operations: plus, minus, multiplied or divided' if operations.empty?
numbers = extract_numbers @problem
index = 0
@durrellchamorro
durrellchamorro / sum.rb
Last active April 9, 2016 05:23
Sum of Multiples
class SumOfMultiples
def initialize(*numbers)
@numbers = numbers
end
def self.to(limit)
new(3, 5).to(limit)
end
def to(limit)
class Prime
def self.nth(nth_prime)
raise ArgumentError if nth_prime == 0
return 2 if nth_prime == 1
primes = [2,3]
5.step(by:2) do |potential_prime|
break if primes.size == nth_prime
primes << potential_prime if prime? potential_prime
end
@durrellchamorro
durrellchamorro / saddle_points.rb
Last active March 25, 2016 17:58
Saddle Points
class Matrix
def initialize(raw_data_string)
@data = raw_data_string
end
def rows
format @data
end
def columns
class Trinary
def initialize(string)
@trinary_number = string
end
def to_decimal
return 0 if contains_any_non_digit?
numbers_paired_with_powers.map do |pair|
pair[0].to_i * 3**pair[1]
@durrellchamorro
durrellchamorro / palindrome_products.rb
Last active March 5, 2016 00:16
Palindrome Products
require 'pry'
class Palindromes
attr_accessor :min_factor, :max_factor, :palindrome_factors, :palindromes
def initialize(min_factor: 1, max_factor: 1)
@min_factor, @max_factor = min_factor, max_factor
end
def generate
@durrellchamorro
durrellchamorro / clock.rb
Last active February 27, 2016 22:29
Clock Exercise Solution
class Clock
attr_accessor :hours, :minutes
def initialize(hours, minutes)
@hours, @minutes = hours, minutes
end
def self.at(hours=0, minutes=0)
new(hours, minutes)
end
@durrellchamorro
durrellchamorro / Rakefile
Last active January 21, 2016 18:07
The Rakefile I wrote to use with Exercism
# Include this rakefile in the same directory as your Exercism exercise.
# Instead of submitting with $ exercism submit my_file.rb
# Enter $ rake s my_file.rb
# Now rubocop will automatically check your file before it submits to Exercism.
# If there are any problems, rubocop will tell you what they are like normal, and your exercise won't get submitted.
# If rubocop doesn't find any problems, then your exercise will be submitted.
require 'fileutils'
task :rubocop do
class PigLatin
def self.translate(string)
string.split(' ').map do |word|
if /yt|xr/ =~ word[0..1]
word << 'ay'
elsif /ye/ =~ word[0..1]
word.chars.rotate.join << 'ay'
elsif /thr|sch/ =~ word[0..2]
word.chars.rotate(3).join << 'ay'
elsif /ch|qu|th/ =~ word[0..1]
@durrellchamorro
durrellchamorro / luhn.rb
Last active December 5, 2015 01:38
Luhn Algorithm
class Luhn
attr_reader :numbers
def initialize(numbers)
@numbers = numbers.to_s.chars.map(&:to_i)
end
def self.create(numbers)
create_valid_number(new(numbers))
end