Last active
October 26, 2015 22:23
-
-
Save karapetyan/9b8f6977bb4f9a4b5816 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
def validate(n) | |
multiply_other(n) % 10 == 0 ? true : false | |
end | |
def multiply_other(n) | |
is_even?(n) ? multiply(n) { |index| index.odd? } : multiply(n) { |index| index.even? } | |
end | |
def is_even?(n) | |
n.to_s.split(//).size.even? | |
end | |
def double(number) | |
number.to_i * 2 > 9 ? (number.to_i * 2).to_s.split(//).map(&:to_i).inject(:+) : number.to_i * 2 | |
end | |
def multiply(value) | |
value.to_s.split(//).map.with_index(offset = 1) { |item, index| yield(index)? double(item) : item.to_i }.inject(:+) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment