Upgrade to Pro — share decks privately, control downloads, hide ads and more …

FizzBuzz code golf by ruby

D763cce13d2b05aa1611f868919f7f92?s=47 taira120
February 22, 2018

FizzBuzz code golf by ruby

FizzBuzz code golf for hiroshima.rb #062

D763cce13d2b05aa1611f868919f7f92?s=128

taira120

February 22, 2018
Tweet

Transcript

  1. FizzBuzz code golf

  2. first code

  3. (1..100).each do |i| puts "#{i} " if i % 3

    == 0 if i % 5 == 0 puts 'fizzbuzz' next else puts 'fizz' next end end if i % 5 == 0 puts 'buzz' next end end count 207
  4. if…else…end -> ? :

  5. (1..100).each do |i| print "#{i} " if i % 3

    == 0 puts i % 5 == 0 ? 'fizzbuzz' : 'fizz' next end puts i % 5 == 0 ? 'buzz' : nil end count 148
  6. nest conditional operator

  7. (1..100).each do |i| print "#{i} " puts i % 3

    == 0 ? i % 5 == 0 ? 'fizzbuzz' : 'fizz' : i % 5 == 0 ? 'buzz' : nil end count 122
  8. do…end -> {…}

  9. (1..100).each { |i| print "#{i} " puts i % 3

    == 0 ? i % 5 == 0 ? 'fizzbuzz' : 'fizz' : i % 5 == 0 ? 'buzz' : nil } count 119
  10. join lines

  11. (1..100).each { |i| print "#{i} ";puts i % 3 ==

    0 ? i % 5 == 0 ? 'fizzbuzz' : 'fizz' : i % 5 == 0 ? 'buzz' : nil } count 115
  12. nil -> ''

  13. (1..100).each { |i| print "#{i} “; puts i % 3

    == 0 ? i % 5 == 0 ? 'fizzbuzz' : 'fizz' : i % 5 == 0 ? 'buzz' : '' } count 114
  14. into string

  15. (1..100).each { |i| puts "#{i} #{'fizz' if i % 3

    == 0}#{'buzz' if i % 5 == 0}" } count 81
  16. (1..100).each -> Integer#upto

  17. 1.upto(100) { |i| puts "#{i} #{'fizz' if i % 3

    == 0}#{'buzz' if i % 5 == 0}" } count 79
  18. i == 0 -> i < 1

  19. 1.upto(100) { |i| puts "#{i} #{'fizz' if i % 3

    < 1}#{'buzz' if i % 5 < 1}" } count 77
  20. delete whitespace

  21. 1.upto(100){|i|puts"#{i} #{'fizz'if i%3<1}#{'buzz'if i%5<1}"} count 62