Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # First I started with a descriptive method
- # Every condition is layed out explicitly to make the code readable
- # It's a good first candidate because it makes testing simple
- (1..100).each do |number|
- if number % 3 == 0 && number % 5 == 0
- puts 'APingBPong'
- elsif number % 3 == 0 && number % 5 != 0
- puts 'APing'
- elsif number % 3 != 0 && number % 5 == 0
- puts 'BPong'
- else
- puts number
- end
- end
- # Even though there's no increase in complexity this version is slightly faster
- # It checks if we can just print the number and goes on to the next
- # If it can't just print the number, then it concatenates whicever strings match the individual checks
- # Either for being a multiple of 3 or a multiple of 5
- # We can leverage any unit tests written for the problem above
- (1..100).each do |number|
- puts number and next if number % 3 != 0 && number % 5 != 0
- printable = ""
- if number % 3 == 0
- printable += 'APing'
- else number % 5 == 0
- printable += 'BPong'
- end
- puts printable
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement