Advertisement
ramonrails

range output sliced with conditions

Dec 19th, 2022 (edited)
950
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.91 KB | Source Code | 0 0
  1. # frozen_string_literal: true
  2.  
  3. require 'benchmark'
  4.  
  5. # synopsis
  6. #
  7. # rubocop suggested syntax is
  8. #   * costlier on execution
  9. #   * not really that super easy to read and maintain than case statement
  10. #   * adds more number of lines
  11. #   * `zero?` is syntactical-sugar but high cost on execution
  12. # case statement
  13. #   * is fastest in execution
  14. #   * seems most suitable for production
  15. # one-liner
  16. #   * is just a show-off :)
  17. #   * but it does work too!
  18.  
  19. # sample output:
  20. #
  21. # 1 2 Ping 4 Pong Ping 7 8 Ping Pong 11 Ping 13 14 PingPong 16 17 Ping 19 Pong
  22. # Ping 22 23 Ping Pong 26 Ping 28 29 PingPong 31 32 Ping 34 Pong Ping 37 38 Ping
  23. # Pong 41 Ping 43 44 PingPong 46 47 Ping 49 Pong Ping 52 53 Ping Pong 56 Ping 58
  24. # 59 PingPong 61 62 Ping 64 Pong Ping 67 68 Ping Pong 71 Ping 73 74 PingPong 76
  25. # 77 Ping 79 Pong Ping 82 83 Ping Pong 86 Ping 88 89 PingPong 91 92 Ping 94 Pong
  26. # Ping 97 98 Ping Pong
  27. #
  28. # Benchmarks
  29. #
  30. # Rehearsal ------------------------------------------------------------
  31. # one liner                  0.026752   0.000098   0.026850 (  0.026848)
  32. # one liner (rubocop)        0.031388   0.000084   0.031472 (  0.031474)
  33. # case statement             0.026730   0.000095   0.026825 (  0.026840)
  34. # case statement (rubocop)   0.030073   0.000081   0.030154 (  0.030189)
  35. # --------------------------------------------------- total: 0.115301sec
  36. #
  37. #                                user     system      total        real
  38. # one liner                  0.027161   0.000050   0.027211 (  0.027212)
  39. # one liner (rubocop)        0.030034   0.000046   0.030080 (  0.030083)
  40. # case statement             0.026946   0.000100   0.027046 (  0.027059)
  41. # case statement (rubocop)   0.031117   0.000055   0.031172 (  0.031173)
  42.  
  43. #
  44. # One-liner ruby statement. Complex to maintain. Not ideal.
  45. #
  46. # @param [Range] range -> ruby object
  47. #
  48. # @return [String] concatenated string of final output
  49. #
  50. def ping_pong_one_liner(range = 1..100)
  51.   return 'Hey! I am smart :) Give me a range' unless range.is_a?(Range)
  52.  
  53.   range.map {|e| e % 15 == 0 ? 'PingPong' : (e % 5 == 0 ? 'Pong' : (e % 3 == 0 ? 'Ping' : e )) }.join(' ')
  54. end
  55.  
  56. #
  57. # Using case statement. Easier to maintain than one-liner
  58. #
  59. # @param [Range] range -> ruby object
  60. #
  61. # @return [String] concatenated string of final output
  62. #
  63. def ping_pong_case(range = 1..100)
  64.   return 'Hey! I am smart :) Give me a range' unless range.is_a?(Range)
  65.  
  66.   range.map do |e|
  67.     case
  68.     when e % 15 == 0 then 'PingPong'
  69.     when e %  5 == 0 then 'Pong'
  70.     when e %  3 == 0 then 'Ping'
  71.     else
  72.       e
  73.     end
  74.   end.join(' ')
  75. end
  76.  
  77. #
  78. # One-liner ruby statement, corrected with rubocop
  79. #
  80. # @param [Range] range -> ruby object
  81. #
  82. # @return [String] concatenated string of final output
  83. #
  84. def ping_pong_one_liner_rubocop(range = 1..100)
  85.   return 'Hey! I am smart :) Give me a range' unless range.is_a?(Range)
  86.  
  87.   range.map do |e|
  88.     if (e % 15).zero?
  89.       'PingPong'
  90.     else
  91.       (if (e % 5).zero?
  92.          'Pong'
  93.        else
  94.          ((e % 3).zero? ? 'Ping' : e)
  95.        end)
  96.     end
  97.   end.join(' ')
  98. end
  99.  
  100. #
  101. # Using case statement, corrected with rubocop
  102. #
  103. # @param [Range] range -> ruby object
  104. #
  105. # @return [String] concatenated string of final output
  106. #
  107. def ping_pong_case_rubocop(range = 1..100)
  108.   return 'Hey! I am smart :) Give me a range' unless range.is_a?(Range)
  109.  
  110.   range.map do |e|
  111.     if (e % 15).zero?
  112.       'PingPong'
  113.     elsif (e %  5).zero?
  114.       'Pong'
  115.     elsif (e %  3).zero?
  116.       'Ping'
  117.     else
  118.       e
  119.     end
  120.   end.join(' ')
  121. end
  122.  
  123. # run
  124. #
  125. puts ping_pong_one_liner(1..100)
  126.  
  127. puts "\nBenchmarks\n\n"
  128. Benchmark.bmbm do |x|
  129.   x.report('one liner') { 1000.times { ping_pong_one_liner(1..100) } }
  130.   x.report('one liner (rubocop)') { 1000.times { ping_pong_one_liner_rubocop(1..100) } }
  131.   x.report('case statement') { 1000.times { ping_pong_case(1..100) } }
  132.   x.report('case statement (rubocop)') { 1000.times { ping_pong_case_rubocop(1..100) } }
  133. end
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement