Fizz and buzz of computer programs in python.

1,659 views

Published on

PyCon APEC 2013 Lightning Talk

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,659
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Fizz and buzz of computer programs in python.

  1. 1. Fizz and Buzz of Computer Programs in Python. esehara shigeo
  2. 2. WHO ARE YOU?
  3. 3. Shigeo Esehara Just a Programmer using Python and Love Trivial Programming
  4. 4. !! WARNING !! I guess you get used to my funny English Speaking. (or Programming ?)
  5. 5. OK. Ready!!
  6. 6. GO!!
  7. 7. Before, Programmers had “The Common Sense”...
  8. 8. SICP
  9. 9. But….
  10. 10. Jeff Atword (ex-StackOverFlow) wrote the essay: Why Can't Programmers.. Program?
  11. 11. Then….
  12. 12. Now, Programmer has “The Common Sense”...
  13. 13. FIZZ BUZZ
  14. 14. FizzBuzz Rule ● Write a program that prints the numbers from 1 to 100. ● For multiples of three print “Fizz” instead of the number ● For multiples of five print “Buzz”. ● For numbers which are multiples of both three and five print “FizzBuzz”
  15. 15. Other Lang Example
  16. 16. Haskell :: Maybe Monads FizzBuzz type IsFizzBuzz = Maybe String is_n:: Integer -> Integer -> Integer is_n x y = x `mod` y fizz :: Integer -> Integer fizz x = is_n x 3 buzz :: Integer -> Integer buzz x = is_n x 5 maybe_div :: Integer -> String -> IsFizzBuzz -> IsFizzBuzz maybe_div 0 str m = case m of Nothing -> Just str Just x -> Just $ str ++ x maybe_div _ str m = m maybe_fizz :: Integer -> IsFizzBuzz -> IsFizzBuzz maybe_fizz x m = maybe_div is_fizz "Fizz" m where is_fizz = fizz x maybe_buzz :: Integer -> IsFizzBuzz -> IsFizzBuzz maybe_buzz x m = maybe_div is_buzz "Buzz" m where is_buzz = buzz x maybe_fizzbuzz :: Integer -> IsFizzBuzz -> String maybe_fizzbuzz x m = case m of Nothing -> show x Just x -> x maybe_process :: Integer -> String maybe_process x = maybe_fizzbuzz x =<< is_fizzbuzz x where is_fizzbuzz x = maybe_fizz x >>= maybe_buzz x go :: (Integer -> String) -> IO () go f = process f 1 process :: (Integer -> String) -> Integer -> IO () process f 101 = putStr "" process f x = (putStrLn $ f x) >> process f next where next = x + 1
  17. 17. OK.
  18. 18. Real Pythonista solves the Problem at 3sec.
  19. 19. Basic # begin code for i in range(1, 100): if (i % 15 == 0): print "FizzBuzz" if (i % 3 == 0): print "Buzz" if (i % 5 == 0): print "Fizz" print i # end
  20. 20. “Are You kidding me ?”
  21. 21. No!!
  22. 22. Real Pythonista don’t use “if”statement in FizzBuzz.
  23. 23. Hint
  24. 24. FizzBuzz Structure consist “Loop” and “Branch”.
  25. 25. In other words ... “Loop” is “Iteration”.
  26. 26. Iterator in Python ? List.
  27. 27. 3 of multiple explession: [1, 2, 3, 4, 5, 6, 7, 8, 9 …]
  28. 28. Fizzlize!! [‘’, ‘’, ‘Fizz’, ‘’, ‘’, ’Fizz’, ‘’, ‘’, ’Fizz’ …]
  29. 29. More!! [‘’, ‘’, ‘Fizz’] * 3 List can be multipication in Python.
  30. 30. “Use type conversion, Luke.” bool(‘’) = False int(False) = 0
  31. 31. not use “if” statements fizz = ['', '', “Fizz”] * 50 buzz = ['', '', '', '', “Buzz”] * 50 for x in range(1, 101): fizzbuzz = fizz[x] + buzz[x] number = str(x) * (not bool(fizzbuzz)) print fizzbuzz + number
  32. 32. “It seems bad because it generates list like *malloc* in C language.”
  33. 33. oh...
  34. 34. Real Pythonista use “itertools” in FizzBuzz.
  35. 35. Pythonista like ‘itertools’. from itertools import cycle fizz = cycle(['', '', 'Fizz']) buzz = cycle(['', '', '', '', '', 'Buzz']) for i in range(1, 101): fizzbuzz = fizz.next() + buzz.next() fizzbuzz += (str(i) * (not bool(fizzbuzz))) print fizzbuzz
  36. 36. But...
  37. 37. TRUE Pythonista don’t use *“List” in FizzBuzz. * Strictly speaking, “String” has “List”(= array (= char sets)) Structure, but now, String is exception.
  38. 38. Do you think it possible ?
  39. 39. Yes, We Can!!
  40. 40. OK.
  41. 41. Hint
  42. 42. Loop can be created using “Recurtion”.
  43. 43. Recursion def loop(start, limit): print start (start == limit) or loop(start + 1, limit) loop(1, 100)
  44. 44. FizzBuzz flag Fizz, Buzz = [Bool, Bool]
  45. 45. FizzBuzz flag Fizz, Buzz = [ 0 or 1, 0 or 1]
  46. 46. FizzBuzz flag Number = [0, 0] Fizz = [1, 0] Buzz = [0, 1] FizzBuzz = [1, 1]
  47. 47. “It seems like binary system.”
  48. 48. Yes !! Binary System. Number = 00 = 0 Fizz = 01 = 1 Buzz = 10 = 2 FizzBuzz = 11 = 3
  49. 49. Example :: PHP <?php function _00($i) { echo $i; } function _10($i) { echo "Fizz"; } function _01($i) { echo "Buzz"; } function _11($i) { echo _10($i); echo _01($i); } for ($i = 1; $i < 101; $i++) { $fizz = $i % 3 === 0; $fizz = (int) $fizz; $fizz = (string) $fizz; $buzz = $i % 5 === 0; $buzz = (int) $buzz; $buzz = (string) $buzz; $fizzbuzz = "_" . $fizz . $buzz; $fizzbuzz($i); echo "n"; }
  50. 50. “Use the String, Luke.” " FizzBuzzFizzBuzz"
  51. 51. “Use the String, Luke.” " FizzBuzzFizzBuzz" [ 0 ][ 1 ][2 ][3 ] * “FizzBuzz” is just only 8 string.
  52. 52. Imagine there’s no list..
  53. 53. No List !! def loop(start, limit): fizzbuzz = (not start % 3) | ((not start % 5) * 2) print (" FizzBuzz FizzBuzz"[ fizzbuzz * 4: (fizzbuzz + 1) * 4 + ((fizzbuzz == 3) * 4)].strip() ) or start (start == limit) or loop(start + 1, limit) loop(1, 100)
  54. 54. Congratulations!!
  55. 55. Thanks for your attention. twitter: @esehara github.com/esehara

×