Skip to content

Instantly share code, notes, and snippets.

@karapetyan
Last active October 26, 2015 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karapetyan/9b8f6977bb4f9a4b5816 to your computer and use it in GitHub Desktop.
Save karapetyan/9b8f6977bb4f9a4b5816 to your computer and use it in GitHub Desktop.
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