開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART III.(Functions and Generators)、Test Your Knowledge: Part IV Exercises 、11.(Recursive functions)を解いてみる。
その他参考書籍
11.(Recursive functions)
コード(BBEdit)
sample.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
def countdown(n):
if n == 0:
print('stop')
else:
print(n, end=' ')
countdown(n - 1)
countdown(5)
# a nonfunction approach
[print(x, end=' ') for x in range(5, 0, -1)]
print('stop')
list(map(lambda x:print(x, end=' '), range(5, 0, -1)))
print('stop')
list(print(x, end=' ') for x in range(5, 0, -1))
print('stop')
# generator
def countdown1(n):
if n == 0:
yield 'stop'
else:
yield n
for x in countdown1(n - 1):
yield x
for x in countdown1(5):
print(x, end=' ')
print()
入出力結果(Terminal)
$ ./sample.py 5 4 3 2 1 stop 5 4 3 2 1 stop 5 4 3 2 1 stop 5 4 3 2 1 stop 5 4 3 2 1 stop $
0 コメント:
コメントを投稿