開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの無料機能制限版、light版)
- Script言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のIII部(ステートメント)のまとめ演習4(プログラムを書き直す)を解いてみる。
4.
コード(TextWrangler)
a.
#!/usr/bin/env python #encoding: utf-8 L = [1,2,4,8,16,32,64] X = 5 i = 0 while i < len(L): if 2 ** X == L[i]: print 'at index', i break else: i = i + 1 else: print X, 'not found'
b.
#!/usr/bin/env python #encoding: utf-8 L = [1,2,4,8,16,32,64] X = 5 i = 0 for a in L: if 2 ** X == a: print 'at index', L.index(a) break else: print X, 'not found'
c.
#!/usr/bin/env python #encoding: utf-8 L = [1,2,4,8,16,32,64] X = 5 i = 0 if 2 ** X in L: print 'at index', L.index(2**X) else: print X, 'not found'
d.
#!/usr/bin/env python #encoding: utf-8 L = [] for x in range(7): L.append(2**x) X = 5 i = 0 if 2 ** X in L: print 'at index', L.index(2**X) else: print X, 'not found'
e.(map関数とlambda)
#!/usr/bin/env python #encoding: utf-8 L = [] L = map(lambda x: 2**x, range(7)) X = 5 i = 0 if 2 ** X in L: print 'at index', L.index(2**X) else: print X, 'not found'
入出力結果(Terminal)
$ python python_program.py at index 5 $
0 コメント:
コメントを投稿