Your SlideShare is downloading. ×
Fizz and buzz of computer programs in python.
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Fizz and buzz of computer programs in python.

978
views

Published on

PyCon APEC 2013 Lightning Talk

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
978
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Fizz and Buzz of Computer Programs in Python. esehara shigeo
  • 2. WHO ARE YOU?
  • 3. Shigeo Esehara Just a Programmer using Python and Love Trivial Programming
  • 4. !! WARNING !! I guess you get used to my funny English Speaking. (or Programming ?)
  • 5. OK. Ready!!
  • 6. GO!!
  • 7. Before, Programmers had “The Common Sense”...
  • 8. SICP
  • 9. But….
  • 10. Jeff Atword (ex-StackOverFlow) wrote the essay: Why Can't Programmers.. Program?
  • 11. Then….
  • 12. Now, Programmer has “The Common Sense”...
  • 13. FIZZ BUZZ
  • 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. Other Lang Example
  • 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. OK.
  • 18. Real Pythonista solves the Problem at 3sec.
  • 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. “Are You kidding me ?”
  • 21. No!!
  • 22. Real Pythonista don’t use “if”statement in FizzBuzz.
  • 23. Hint
  • 24. FizzBuzz Structure consist “Loop” and “Branch”.
  • 25. In other words ... “Loop” is “Iteration”.
  • 26. Iterator in Python ? List.
  • 27. 3 of multiple explession: [1, 2, 3, 4, 5, 6, 7, 8, 9 …]
  • 28. Fizzlize!! [‘’, ‘’, ‘Fizz’, ‘’, ‘’, ’Fizz’, ‘’, ‘’, ’Fizz’ …]
  • 29. More!! [‘’, ‘’, ‘Fizz’] * 3 List can be multipication in Python.
  • 30. “Use type conversion, Luke.” bool(‘’) = False int(False) = 0
  • 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. “It seems bad because it generates list like *malloc* in C language.”
  • 33. oh...
  • 34. Real Pythonista use “itertools” in FizzBuzz.
  • 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. But...
  • 37. TRUE Pythonista don’t use *“List” in FizzBuzz. * Strictly speaking, “String” has “List”(= array (= char sets)) Structure, but now, String is exception.
  • 38. Do you think it possible ?
  • 39. Yes, We Can!!
  • 40. OK.
  • 41. Hint
  • 42. Loop can be created using “Recurtion”.
  • 43. Recursion def loop(start, limit): print start (start == limit) or loop(start + 1, limit) loop(1, 100)
  • 44. FizzBuzz flag Fizz, Buzz = [Bool, Bool]
  • 45. FizzBuzz flag Fizz, Buzz = [ 0 or 1, 0 or 1]
  • 46. FizzBuzz flag Number = [0, 0] Fizz = [1, 0] Buzz = [0, 1] FizzBuzz = [1, 1]
  • 47. “It seems like binary system.”
  • 48. Yes !! Binary System. Number = 00 = 0 Fizz = 01 = 1 Buzz = 10 = 2 FizzBuzz = 11 = 3
  • 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. “Use the String, Luke.” " FizzBuzzFizzBuzz"
  • 51. “Use the String, Luke.” " FizzBuzzFizzBuzz" [ 0 ][ 1 ][2 ][3 ] * “FizzBuzz” is just only 8 string.
  • 52. Imagine there’s no list..
  • 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. Congratulations!!
  • 55. Thanks for your attention. twitter: @esehara github.com/esehara