Created
May 9, 2013 16:49
-
-
Save blairanderson/5548740 to your computer and use it in GitHub Desktop.
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(args) | |
@max = args[:max_factor] | |
@min = args[:min_factor] || 1 | |
end | |
def generate | |
factors = (@min..@max).to_a | |
uniq_products = factors.map do |f| | |
[factors.map{|m| m*f}] | |
end.flatten.uniq | |
return uniq_products.select{|f| palindrome?(f)} | |
end | |
def largest | |
new_thing = { | |
palindrome: generate.sort.max, | |
maximum: @max, | |
minimum: @min | |
} | |
Palindrome.new(new_thing) | |
end | |
def smallest | |
new_thing = { | |
palindrome: generate.sort.min, | |
maximum: @max, | |
minimum: @min | |
} | |
Palindrome.new(new_thing) | |
end | |
def palindrome?(query) | |
letters = query.to_s | |
letters == letters.reverse | |
end | |
end | |
class Palindrome | |
attr_reader :number, :max, :min | |
def initialize(args) | |
@number = args[:palindrome] | |
@max = args[:max] | |
@min = args[:min] | |
@factors = (1..@number).map do |n| | |
[n, @number/n] if ((@number/n) * n) == @number | |
end.compact | |
end | |
def value | |
@number | |
end | |
def factors | |
# @factors.select do |first,second| | |
# clean?(first) && clean?(second) | |
# end | |
result = [] | |
@factors.each do |factor| | |
result << factor if clean?(factor) | |
end | |
end | |
def clean?(n) | |
n.each do |l| | |
return false if l > @max | |
return false if l < @min | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment