FizzBuzz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fizzbuzz(n): | |
if n % 15 == 0: | |
return "FizzBuzz" | |
elif n % 3 == 0: | |
return "Fizz" | |
elif n % 5 == 0: | |
return "Buzz" | |
else: | |
return n |
なんのへんてつもないコード。
yield使って、generatorにしてみる。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fizzbuzz_iter(): | |
n = 1 | |
while True: | |
yield fizzbuzz(n) | |
n += 1 |
このケースならitertoolsを使ったほうがカッコイイ。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
def fizzbuzz_iter(): | |
return map(fizzbuzz, itertools.count(1)) |
countはnからstep(デフォルトは1)づつ増えていくgeneratorを作る関数です。
mapはいつの間にかイテレータに対応してくれてました。
素数
判定を書く
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
import operator | |
def isprime(n): | |
return all(map(functools.partial(operator.mod, n), range(2, n - 1))) |
functoolsとoperatorで関数型っぽく書けて素敵ですね。
functools.partialは部分適用、operator.modは剰余です。
素数列を作る
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def primes(): | |
return filter(isprime, itertools.count(2)) |
なかなかいい感じ。
フィボナッチ数列
n番目のフィボナッチ数を返す関数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fibonacci(n): | |
if n == 0: | |
return 1 | |
elif n == 1: | |
return 1 | |
else: | |
return fibonacci(n - 2) + fibonacci(n - 1) |
dict使って分岐する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fibonacci(n): | |
return {0: lambda: 1, 1: lambda: 1}.get(n, lambda: fibonacci(n - 2) + fibonacci(n - 1))() |
switch的なものはdictで代用できるのでもーまんたいですね。
getの第二引数はキーが存在しない場合返ります。
イテレータ版
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fibonacci_iter(): | |
return map(fibonacci, itertools.count(1)) |
Pythonはなかなか関数型言語ちっくに書けて素敵ですね。
Rから始まる4文字のプログラミング言語より関数型っぽいです。
0 件のコメント:
コメントを投稿