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 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") |
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 'benchmark' | |
class String | |
def swap!(a,b) | |
self[a], self[b] = self[b], self[a] | |
self | |
end | |
end | |
string1 = "Pneumonoultramicroscopicsilicovolcanoconiosis" |
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 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 = [] |
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 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 |
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 |
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
# 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 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
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 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] |
OlderNewer