This file contains 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 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 |
This file contains 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 SumOfMultiples | |
def initialize(*numbers) | |
@numbers = numbers | |
end | |
def self.to(limit) | |
new(3, 5).to(limit) | |
end | |
def to(limit) |
This file contains 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 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 |
This file contains 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 Matrix | |
def initialize(raw_data_string) | |
@data = raw_data_string | |
end | |
def rows | |
format @data | |
end | |
def columns |
This file contains 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 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] |
This file contains 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 '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 |
This file contains 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 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 |
This file contains 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
# 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 |
This file contains 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 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] |
This file contains 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 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 |