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

時間をかけて解く FizzBuzz

ckazu
November 09, 2011

時間をかけて解く 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
Tweet

More Decks by ckazu

Other Decks in Programming

Transcript

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

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

    Buzz, 11, Fizz,13,14, FizzBuzz, 16, 17, Fizz, 19, Buzz...
  3. 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
  4. 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
  5. FizzBuzz ίʔυྫ 3߲ԋࢉࢠ def ternary_function n n % 15 ==

    0 ? ‘FizzBuzz’ : n % 3 == 0 ? ’Fizz’ : n % 5 == 0 ? ‘Buzz’ : n end
  6. γϯϓϧͳ 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
  7. γϯϓϧͳ FizzBuzz !? def silly_function puts ‘1 2 Fizz 4

    Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FuzzBuzz 16 17 Fizz ...’ end
  8. GPʢҨ఻తϓϩάϥϛϯάʣ ύϥϝʔλɾ৚݅ ֤ੈ୅ͷݸମ਺: 100 ಥવมҟ֬཰: 0.1 બ୒ઓུ: ΤϦʔτઓུ ࠷େੈ୅਺: 100,000

    ੈ୅ ࢖༻Ͱ͖ΔΦϖϨʔλ: +, -, *, %, if, > ࢖༻Ͱ͖ΔΦϖϥϯυ: 1..10 ͷ੔਺, ‘Fizz’, ‘Buzz’ ڭࢣ৴߸ʢదԠ౓ͷࢉग़ʣ: 1<n<50 ·Ͱͷ݁Ռ
  9. 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ճ͸ऩଋͤͣʣ