Advertisement
ramongr

Aping-bpong

Mar 26th, 2024
973
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.01 KB | None | 0 0
  1. # First I started with a descriptive method
  2. # Every condition is layed out explicitly to make the code readable
  3. # It's a good first candidate because it makes testing simple
  4. (1..100).each do |number|
  5.   if number % 3 == 0 && number % 5 == 0
  6.     puts 'APingBPong'
  7.   elsif number % 3 == 0 && number % 5 != 0
  8.     puts 'APing'
  9.   elsif number % 3 != 0 && number % 5 == 0
  10.     puts 'BPong'
  11.   else
  12.     puts number
  13.   end
  14. end
  15.  
  16. # Even though there's no increase in complexity this version is slightly faster
  17. # It checks if we can just print the number and goes on to the next
  18. # If it can't just print the number, then it concatenates whicever strings match the individual checks
  19. # Either for being a multiple of 3 or a multiple of 5
  20. # We can leverage any unit tests written for the problem above
  21. (1..100).each do |number|
  22.   puts number and next if number % 3 != 0 && number % 5 != 0
  23.  
  24.   printable = ""
  25.   if number % 3 == 0
  26.     printable += 'APing'
  27.   else number % 5 == 0
  28.     printable += 'BPong'
  29.   end
  30.   puts printable
  31. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement