Last active
December 19, 2015 06:09
-
-
Save gglin/5909794 to your computer and use it in GitHub Desktop.
Day 22 Warmup
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 Palindromes | |
def initialize(hash = {}) | |
hash[:max_factor] ||= 2 | |
hash[:min_factor] ||= 1 | |
@max_factor = hash[:max_factor] | |
@min_factor = hash[:min_factor] | |
@palins = [] | |
end | |
def is_palindrome?(number) | |
number.to_s == number.to_s.reverse | |
end | |
def generate | |
(@min_factor..@max_factor).each do |i| | |
(i..@max_factor).each do |j| | |
product = i * j | |
if is_palindrome?(product) | |
palin = PalinNum.find_or_create(product) | |
palin.add_factor( [i, j] ) | |
@palins << palin if [email protected]?(palin) | |
end | |
end | |
end | |
@palins.sort!{|p1, p2| p1.value <=> p2.value} | |
# p @palins.collect{|palin| palin.value} | |
end | |
def largest | |
@palins[-1] | |
end | |
def smallest | |
@palins[0] | |
end | |
end | |
class PalinNum | |
attr_accessor :pal, :factors | |
def initialize(pal = nil) | |
# @id = self.class.count + 1 | |
self.class.all << self | |
@pal = pal | |
end | |
# Findable | |
def self.find(pal) | |
all.detect{|a| a.pal == pal} | |
end | |
def self.find_or_create(pal) | |
self.find(pal) || self.new(pal) | |
end | |
# Memorable | |
def self.all | |
@all ||= [] | |
end | |
# Local Methods | |
def value | |
@pal | |
end | |
def add_factor(pair) | |
@factors ||= [] | |
@factors << pair if [email protected]?(pair) | |
end | |
end | |
# pal = Palindromes.new | |
# puts pal.is_palindrome?(324923) | |
# puts pal.is_palindrome?(324423) | |
# puts pal.is_palindrome?(32423) | |
# puts pal.is_palindrome?(324922343) |
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 'fis/test' | |
require_relative 'palindromes' | |
include Fis::Test | |
test 'largest_palindrome_from_single_digit_factors: max 9' do | |
palindromes = Palindromes.new(max_factor: 9) | |
palindromes.generate | |
largest = palindromes.largest | |
assert_equal 9, largest.value | |
assert [[[3, 3], [1, 9]], [[1, 9], [3, 3]]].include? largest.factors | |
end | |
test 'largest_palindrome_from_double_digit_factors: 10-99' do | |
palindromes = Palindromes.new(max_factor: 99, min_factor: 10) | |
palindromes.generate | |
largest = palindromes.largest | |
assert_equal 9009, largest.value | |
assert_equal [[91, 99]], largest.factors | |
end | |
test 'smallest_palindrome_from_double_digit_factors: 10-99' do | |
palindromes = Palindromes.new(max_factor: 99, min_factor: 10) | |
palindromes.generate | |
smallest = palindromes.smallest | |
assert_equal 121, smallest.value | |
assert_equal [[11, 11]], smallest.factors | |
end | |
test 'largest_palindrome_from_triple_digit_factors: 100-999' do | |
palindromes = Palindromes.new(max_factor: 999, min_factor: 100) | |
palindromes.generate | |
largest = palindromes.largest | |
assert_equal 906609, largest.value | |
assert_equal [[913, 993]], largest.factors | |
end | |
test 'smallest_palindrome_from_triple_digit_factors: 100-999' do | |
palindromes = Palindromes.new(max_factor: 999, min_factor: 100) | |
palindromes.generate | |
smallest = palindromes.smallest | |
assert_equal 10201, smallest.value | |
assert_equal [[101, 101]], smallest.factors | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment