Created
November 9, 2012 20:23
-
-
Save skatenerd/4047998 to your computer and use it in GitHub Desktop.
Coinfactorizer
This file contains hidden or 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 Coinfactorizer | |
def self.change(target, domain) | |
answer = {} | |
answer_data = domain.elements(target).reduce({target: target, answer: {}}) do |aggregator, domain_element| | |
new_target, quantity_of_element = domain.destroy_domain_element_from_total(aggregator[:target], domain_element) | |
old_answer = aggregator[:answer] | |
new_answer = assoc(old_answer, domain_element, quantity_of_element) if quantity_of_element > 0 | |
new_answer ||= old_answer | |
{target: new_target, answer: new_answer} | |
end | |
answer_data[:answer] | |
end | |
def self.assoc(hash, key, value) | |
hash.merge({key => value}) | |
end | |
end | |
class Coins | |
def self.elements(_) | |
[25, 10, 5, 1] | |
end | |
def self.stop?(total) | |
total == 0 | |
end | |
def self.destroy_domain_element_from_total(total, domain_element) | |
quantity = total/domain_element | |
total -= (domain_element * quantity) | |
[total, quantity] | |
end | |
end | |
class Primes | |
def self.divides?(top, bottom) | |
(top >= bottom) && (top % bottom == 0) | |
end | |
def self.destroy_domain_element_from_total(total, domain_element) | |
count = 0 | |
while divides?(total, domain_element) | |
total /= domain_element | |
count += 1 | |
end | |
[total, count] | |
end | |
def self.stop?(total) | |
total == 1 | |
end | |
def self.elements(total) | |
(2..total).to_a | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment