Skip to content

Instantly share code, notes, and snippets.

View durrellchamorro's full-sized avatar

Durrell Chamorro durrellchamorro

  • Fleetio
  • 19:14 (UTC -05:00)
View GitHub Profile
@durrellchamorro
durrellchamorro / reverse.rb
Created November 6, 2015 17:12
Three Approaches To Reversing A String
require 'benchmark'
class String
def swap!(a,b)
self[a], self[b] = self[b], self[a]
self
end
end
string1 = "Pneumonoultramicroscopicsilicovolcanoconiosis"
@durrellchamorro
durrellchamorro / queen_attack.rb
Last active November 8, 2015 08:18
Queen Attack Solution
class Queens
attr_reader :white, :black
def initialize(white: [0, 3], black: [7, 3])
@white, @black = white, black
fail ArgumentError, "Queens cannot occupy the same position." if white == black
end
def to_s
mark_board.map { |row| row.join(" ") }.join("\n")
class Series
attr_reader :digits
def initialize(string_of_digits)
@digits = string_of_digits.chars.map(&:to_i)
end
def slices(slice_size)
fail ArgumentError, "Slice size cannot exceed caller's digit size." if slice_size > digits.size
result = []
@durrellchamorro
durrellchamorro / sieve.rb
Last active November 21, 2015 03:01
Sieve of Eratosthenes
class Sieve
def initialize(limit)
@number_list = (2..limit).to_a
end
def primes
prime_numbers = []
until @number_list.empty?
prime_numbers << @number_list.first
@number_list -= multiples_of @number_list.first
@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
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 / 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
@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 / 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
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]