ckazu
November 09, 2011
5

# 時間をかけて解く FizzBuzz

https://www.slideshare.net/ckazu/fizzbuzz-12147905
2011-11-09, 社内LT大会@aiming-inc
2012-03-28, Shinjuku.rb #03 LT

## ckazu

November 09, 2011

## Transcript

2. ### FizzBuzz ༡ͼํ ϓϨΠϠʔ͸ԁঢ়ʹ࠲Δɻ ࠷ॳͷϓϨΠϠʔ͸ʮ1ʯͱ਺ࣈΛൃݴ͢Δɻ ࣍ͷϓϨΠϠʔ͸௚લͷϓϨΠϠʔͷ࣍ͷ਺ࣈΛൃݴ͍ͯ͘͠ɻ ͨͩ͠ɺ 3ͰׂΓ੾ΕΔ৔߹͸ ʮFizzʯ 5ͰׂΓ੾ΕΔ৔߹͸ ʮBuzzʯ

྆ऀͰׂΓ੾ΕΔ৔߹͸ ʮFizz Buzzʯ Λ਺ͷ୅ΘΓʹൃݴ͠ͳ͚Ε͹ͳΒͳ͍ɻ ൃݴΛؒҧ͑ͨऀ΍ɺͨΊΒͬͨऀ͸୤མͱͳΔɻ http://ja.wikipedia.org/wiki/Fizz_Buzz
3. ### FizzBuzz 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz,

Buzz, 11, Fizz,13,14, FizzBuzz, 16, 17, Fizz, 19, Buzz...

5. ### FizzBuzz ίʔυྫ if จ def if_function n if n %

15 == 0 'FizzBuzz' elsif n % 3 == 0 'Fizz' elsif n % 5 == 0 'Buzz' else n end end
6. ### FizzBuzz ίʔυྫ case จ def case_function n case n %

15 when 0 'FizzBuzz' when 3, 6, 9, 12 'Fizz' when 5, 10 'Buzz' else n end end
7. ### FizzBuzz ίʔυྫ 3߲ԋࢉࢠ def ternary_function n n % 15 ==

0 ? ‘FizzBuzz’ : n % 3 == 0 ? ’Fizz’ : n % 5 == 0 ? ‘Buzz’ : n end
8. ### FizzBuzz ίʔυྫ map def map_function (1..100).map{|n|(s=(n%3<1 ? 'Fizz' : '')

+ (n%5<1 ?'Buzz':''))=~/^\$/ ? n:s} end

: ret end

12. ### γϯϓϧͳ FizzBuzz !? def silly_function n case n when 1;

1 when 2; 2 when 3; 'Fizz' when 4; 4 when 5; 'Buzz' when 6; 'Fizz' when 7; 7 when 8; 8 when 9; 'Fizz' when 10; 'Buzz' # ... end end
13. ### γϯϓϧͳ FizzBuzz !? def silly_function puts ‘1 2 Fizz 4

Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FuzzBuzz 16 17 Fizz ...’ end

27. ### GPʢҨ఻తϓϩάϥϛϯάʣ ύϥϝʔλɾ৚݅ ֤ੈ୅ͷݸମ਺: 100 ಥવมҟ֬཰: 0.1 બ୒ઓུ: ΤϦʔτઓུ ࠷େੈ୅਺: 100,000

ੈ୅ ࢖༻Ͱ͖ΔΦϖϨʔλ: +, -, *, %, if, > ࢖༻Ͱ͖ΔΦϖϥϯυ: 1..10 ͷ੔਺, ‘Fizz’, ‘Buzz’ ڭࢣ৴߸ʢదԠ౓ͷࢉग़ʣ: 1<n<50 ·Ͱͷ݁Ռ
28. ### GPʢҨ఻తϓϩάϥϛϯάʣ ݁Ռ if ( 5 - 4 ) > n

% ( 9 + ( 7 - 1 ) ) 'Fizz' + 'Buzz' else if (9 % 8) > n % 5 'Fizz' * 1 else if (7 - 6) > n % (10 % 7) 'Buzz' else n end end end ऩଋ࣌ͷੈ୅਺: 19,473 ੈ୅໨ ʢ3ճͷ͏ͪɼ2ճ͸ऩଋͤͣʣ

31. ### GPʢҨ఻తϓϩάϥϛϯάʣ ͔͔ͬͨ࣌ؒ ίʔσΟϯά GAɿ 1 ࣌ؒऑ GP΁ͷվྑ: 1.5 ࣌ؒ ࣮ߦ࣌ؒ

Ұճʹ͖ͭɼ5 ࣌ؒ ͱ͔ 2 ࣌ؒ ͱ͔