Upload
esehara-shigeo
View
1.020
Download
4
Embed Size (px)
DESCRIPTION
PyCon APEC 2013 Lightning Talk
Citation preview
Fizz and Buzz of
Computer Programsin Python.
esehara shigeo
WHOARE
YOU?
Shigeo Esehara Just a Programmer using Python
andLove Trivial Programming
!! WARNING !!I guess you get used to my funny English Speaking.
(or Programming ?)
OK. Ready!!
GO!!
Before,Programmers had
“The Common Sense”...
SICP
But….
Jeff Atword(ex-StackOverFlow)
wrote the essay:Why Can't Programmers..
Program?
Then….
Now,Programmer has
“The Common Sense”...
FIZZ
BUZZ
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”
Other Lang Example
Haskell :: Maybe Monads FizzBuzztype IsFizzBuzz = Maybe String is_n:: Integer -> Integer -> Integeris_n x y = x `mod` y fizz :: Integer -> Integerfizz x = is_n x 3 buzz :: Integer -> Integerbuzz x = is_n x 5 maybe_div :: Integer -> String -> IsFizzBuzz -> IsFizzBuzzmaybe_div 0 str m = case m of Nothing -> Just str Just x -> Just $ str ++ xmaybe_div _ str m = m maybe_fizz :: Integer -> IsFizzBuzz -> IsFizzBuzzmaybe_fizz x m = maybe_div is_fizz "Fizz" m where is_fizz = fizz x
maybe_buzz :: Integer -> IsFizzBuzz -> IsFizzBuzzmaybe_buzz x m = maybe_div is_buzz "Buzz" m where is_buzz = buzz x maybe_fizzbuzz :: Integer -> IsFizzBuzz -> Stringmaybe_fizzbuzz x m = case m of Nothing -> show x Just x -> x maybe_process :: Integer -> Stringmaybe_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
OK.
Real Pythonista
solvesthe Problem at 3sec.
Basic
# begin codefor 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
“Are You kidding me ?”
No!!
Real Pythonistadon’t use “if”statementin FizzBuzz.
Hint
FizzBuzz Structureconsist
“Loop” and “Branch”.
In other words ...
“Loop”is
“Iteration”.
Iterator in Python ?
List.
3 of multiple explession:
[1, 2, 3, 4, 5, 6, 7, 8, 9 …]
Fizzlize!!
[‘’, ‘’, ‘Fizz’, ‘’, ‘’, ’Fizz’,‘’, ‘’, ’Fizz’
…]
More!!
[‘’, ‘’, ‘Fizz’] * 3List can be multipication in Python.
“Use type conversion, Luke.”
bool(‘’) = Falseint(False) = 0
not use “if” statements
fizz = ['', '', “Fizz”] * 50buzz = ['', '', '', '', “Buzz”] * 50
for x in range(1, 101): fizzbuzz = fizz[x] + buzz[x] number = str(x) * (not bool(fizzbuzz)) print fizzbuzz + number
“It seems bad because
it generates listlike *malloc*
in C language.”
oh...
Real Pythonistause “itertools”
in FizzBuzz.
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
But...
TRUE Pythonistadon’t use *“List”
in FizzBuzz.* Strictly speaking,
“String” has “List”(= array (= char sets)) Structure, but now, String is exception.
Do you thinkit possible ?
Yes,
We Can !!
OK.
Hint
Loopcan be created
using “Recurtion”.
Recursion
def loop(start, limit): print start (start == limit) or loop(start + 1, limit)loop(1, 100)
FizzBuzz flag
Fizz, Buzz = [Bool, Bool]
FizzBuzz flag
Fizz, Buzz = [0 or 1, 0 or 1]
FizzBuzz flag
Number = [0, 0]Fizz = [1, 0]Buzz = [0, 1]
FizzBuzz = [1, 1]
“It seems likebinary system.”
Yes !! Binary System.
Number = 00 = 0Fizz = 01 = 1Buzz = 10 = 2
FizzBuzz = 11 = 3
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";}
“Use the String, Luke.”
" FizzBuzzFizzBuzz"
“Use the String, Luke.”
" FizzBuzzFizzBuzz"[ 0 ][ 1 ][2 ][3 ]
* “FizzBuzz” is just only 8 string.
Imaginethere’sno list..
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)
Congratulations!!
Thanks foryour attention.
twitter: @eseharagithub.com/esehara