Created
May 20, 2011 03:32
-
-
Save albertoleal/982295 to your computer and use it in GitHub Desktop.
Dividable
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 Fixnum | |
def is_dividable_by?(*numbers) | |
result = numbers.inject(0){|k,v| self%v if k == 0} | |
result == 0 ? true : false | |
end | |
end | |
output = {} | |
(1..100).each do |number| | |
output[number] = if number.is_dividable_by?(3,5); "ping pong" | |
elsif number.is_dividable_by? 3; "ping" | |
elsif number.is_dividable_by? 5; "pong" | |
else; "" | |
end | |
end | |
p output | |
=begin | |
ruby is_dividable_by.rb | |
{1=>"", 2=>"", 3=>"ping", 4=>"", 5=>"pong", 6=>"ping", 7=>"", 8=>"", 9=>"ping", 10=>"pong", 11=>"", 12=>"ping", 13=>"", 14=>"", 15=>"ping pong", 16=>"", 17=>"", 18=>"ping", 19=>"", 20=>"pong", 21=>"ping", 22=>"", 23=>"", 24=>"ping", 25=>"pong", 26=>"", 27=>"ping", 28=>"", 29=>"", 30=>"ping pong", 31=>"", 32=>"", 33=>"ping", 34=>"", 35=>"pong", 36=>"ping", 37=>"", 38=>"", 39=>"ping", 40=>"pong", 41=>"", 42=>"ping", 43=>"", 44=>"", 45=>"ping pong", 46=>"", 47=>"", 48=>"ping", 49=>"", 50=>"pong", 51=>"ping", 52=>"", 53=>"", 54=>"ping", 55=>"pong", 56=>"", 57=>"ping", 58=>"", 59=>"", 60=>"ping pong", 61=>"", 62=>"", 63=>"ping", 64=>"", 65=>"pong", 66=>"ping", 67=>"", 68=>"", 69=>"ping", 70=>"pong", 71=>"", 72=>"ping", 73=>"", 74=>"", 75=>"ping pong", 76=>"", 77=>"", 78=>"ping", 79=>"", 80=>"pong", 81=>"ping", 82=>"", 83=>"", 84=>"ping", 85=>"pong", 86=>"", 87=>"ping", 88=>"", 89=>"", 90=>"ping pong", 91=>"", 92=>"", 93=>"ping", 94=>"", 95=>"pong", 96=>"ping", 97=>"", 98=>"", 99=>"ping", 100=>"pong"} | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment