fizz and buzz of computer programs in python

55
Fizz and Buzz of Computer Programs in Python. esehara shigeo

Upload: esehara-shigeo

Post on 06-May-2015

1.020 views

Category:

Technology


4 download

DESCRIPTION

PyCon APEC 2013 Lightning Talk

TRANSCRIPT

Page 1: Fizz and buzz of computer programs in python

Fizz and Buzz of

Computer Programsin Python.

esehara shigeo

Page 2: Fizz and buzz of computer programs in python

WHOARE

YOU?

Page 3: Fizz and buzz of computer programs in python

Shigeo Esehara Just a Programmer using Python

andLove Trivial Programming

Page 4: Fizz and buzz of computer programs in python

!! WARNING !!I guess you get used to my funny English Speaking.

(or Programming ?)

Page 5: Fizz and buzz of computer programs in python

OK. Ready!!

Page 6: Fizz and buzz of computer programs in python

GO!!

Page 7: Fizz and buzz of computer programs in python

Before,Programmers had

“The Common Sense”...

Page 8: Fizz and buzz of computer programs in python

SICP

Page 9: Fizz and buzz of computer programs in python

But….

Page 11: Fizz and buzz of computer programs in python

Then….

Page 12: Fizz and buzz of computer programs in python

Now,Programmer has

“The Common Sense”...

Page 13: Fizz and buzz of computer programs in python

FIZZ

BUZZ

Page 14: Fizz and buzz of computer programs in python

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”

Page 15: Fizz and buzz of computer programs in python

Other Lang Example

Page 16: Fizz and buzz of computer programs in python

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

Page 17: Fizz and buzz of computer programs in python

OK.

Page 18: Fizz and buzz of computer programs in python

Real Pythonista

solvesthe Problem at 3sec.

Page 19: Fizz and buzz of computer programs in python

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

Page 20: Fizz and buzz of computer programs in python

“Are You kidding me ?”

Page 21: Fizz and buzz of computer programs in python

No!!

Page 22: Fizz and buzz of computer programs in python

Real Pythonistadon’t use “if”statementin FizzBuzz.

Page 23: Fizz and buzz of computer programs in python

Hint

Page 24: Fizz and buzz of computer programs in python

FizzBuzz Structureconsist

“Loop” and “Branch”.

Page 25: Fizz and buzz of computer programs in python

In other words ...

“Loop”is

“Iteration”.

Page 26: Fizz and buzz of computer programs in python

Iterator in Python ?

List.

Page 27: Fizz and buzz of computer programs in python

3 of multiple explession:

[1, 2, 3, 4, 5, 6, 7, 8, 9 …]

Page 28: Fizz and buzz of computer programs in python

Fizzlize!!

[‘’, ‘’, ‘Fizz’, ‘’, ‘’, ’Fizz’,‘’, ‘’, ’Fizz’

…]

Page 29: Fizz and buzz of computer programs in python

More!!

[‘’, ‘’, ‘Fizz’] * 3List can be multipication in Python.

Page 30: Fizz and buzz of computer programs in python

“Use type conversion, Luke.”

bool(‘’) = Falseint(False) = 0

Page 31: Fizz and buzz of computer programs in python

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

Page 32: Fizz and buzz of computer programs in python

“It seems bad because

it generates listlike *malloc*

in C language.”

Page 33: Fizz and buzz of computer programs in python

oh...

Page 34: Fizz and buzz of computer programs in python

Real Pythonistause “itertools”

in FizzBuzz.

Page 35: Fizz and buzz of computer programs in python

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

Page 36: Fizz and buzz of computer programs in python

But...

Page 37: Fizz and buzz of computer programs in python

TRUE Pythonistadon’t use *“List”

in FizzBuzz.* Strictly speaking,

“String” has “List”(= array (= char sets)) Structure, but now, String is exception.

Page 38: Fizz and buzz of computer programs in python

Do you thinkit possible ?

Page 39: Fizz and buzz of computer programs in python

Yes,

We Can !!

Page 40: Fizz and buzz of computer programs in python

OK.

Page 41: Fizz and buzz of computer programs in python

Hint

Page 42: Fizz and buzz of computer programs in python

Loopcan be created

using “Recurtion”.

Page 43: Fizz and buzz of computer programs in python

Recursion

def loop(start, limit): print start (start == limit) or loop(start + 1, limit)loop(1, 100)

Page 44: Fizz and buzz of computer programs in python

FizzBuzz flag

Fizz, Buzz = [Bool, Bool]

Page 45: Fizz and buzz of computer programs in python

FizzBuzz flag

Fizz, Buzz = [0 or 1, 0 or 1]

Page 46: Fizz and buzz of computer programs in python

FizzBuzz flag

Number = [0, 0]Fizz = [1, 0]Buzz = [0, 1]

FizzBuzz = [1, 1]

Page 47: Fizz and buzz of computer programs in python

“It seems likebinary system.”

Page 48: Fizz and buzz of computer programs in python

Yes !! Binary System.

Number = 00 = 0Fizz = 01 = 1Buzz = 10 = 2

FizzBuzz = 11 = 3

Page 49: Fizz and buzz of computer programs in python

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";}

Page 50: Fizz and buzz of computer programs in python

“Use the String, Luke.”

" FizzBuzzFizzBuzz"

Page 51: Fizz and buzz of computer programs in python

“Use the String, Luke.”

" FizzBuzzFizzBuzz"[ 0 ][ 1 ][2 ][3 ]

* “FizzBuzz” is just only 8 string.

Page 52: Fizz and buzz of computer programs in python

Imaginethere’sno list..

Page 53: Fizz and buzz of computer programs in python

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)

Page 54: Fizz and buzz of computer programs in python

Congratulations!!

Page 55: Fizz and buzz of computer programs in python

Thanks foryour attention.

twitter: @eseharagithub.com/esehara