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

FizzBuzz code golf by ruby

gurrium
February 22, 2018

FizzBuzz code golf by ruby

FizzBuzz code golf for hiroshima.rb #062

gurrium

February 22, 2018
Tweet

More Decks by gurrium

Other Decks in Programming

Transcript

  1. FizzBuzz code golf

    View full-size slide

  2. (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

    View full-size slide

  3. if…else…end -> ? :

    View full-size slide

  4. (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

    View full-size slide

  5. nest conditional operator

    View full-size slide

  6. (1..100).each do |i|
    print "#{i} "
    puts i % 3 == 0 ? i % 5 == 0 ? 'fizzbuzz' : 'fizz' : i % 5 == 0 ? 'buzz' : nil
    end
    count 122

    View full-size slide

  7. do…end -> {…}

    View full-size slide

  8. (1..100).each { |i|
    print "#{i} "
    puts i % 3 == 0 ? i % 5 == 0 ? 'fizzbuzz' : 'fizz' : i % 5 == 0 ? 'buzz' : nil
    }
    count 119

    View full-size slide

  9. (1..100).each { |i| print "#{i} ";puts i % 3 == 0 ? i %
    5 == 0 ? 'fizzbuzz' : 'fizz' : i % 5 == 0 ? 'buzz' : nil }
    count 115

    View full-size slide

  10. (1..100).each { |i| print "#{i} “;
    puts i % 3 == 0 ? i % 5 == 0 ? 'fizzbuzz' : 'fizz' : i % 5 == 0 ? 'buzz' : '' }
    count 114

    View full-size slide

  11. (1..100).each { |i| puts "#{i} #{'fizz' if i % 3 == 0}#{'buzz' if i % 5 == 0}" }
    count 81

    View full-size slide

  12. (1..100).each -> Integer#upto

    View full-size slide

  13. 1.upto(100) { |i| puts "#{i} #{'fizz' if i % 3 == 0}#{'buzz' if i % 5 == 0}" }
    count 79

    View full-size slide

  14. i == 0 -> i < 1

    View full-size slide

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

    View full-size slide

  16. delete whitespace

    View full-size slide

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

    View full-size slide