2014年8月27日水曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 8(Storing Collections of Data Using Lists)、8.9(Exercises) 7.を解いてみる。

8.9(Exercises) 7.

コード(BBEdit)

sample7.py

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

def same_first_last(L):
    """ (list) -> bool
    Precondition: len(L) >= 2

    Return True if and only if first item of the list is the same as the last.

    >>> same_first_last([3, 4, 2, 8, 3])
    True
    >>> same_first_last(['apple', 'banana', 'pear'])
    False
    >>> same_first_last([4.8, 4.5])
    False
    """
    return L[0] == L[-1]

if __name__ == '__main__':
    import doctest
    doctest.testmod()

入出力結果(Terminal, IPython)

$ ./sample7.py -v
Trying:
    same_first_last([3, 4, 2, 8, 3])
Expecting:
    True
ok
Trying:
    same_first_last(['apple', 'banana', 'pear'])
Expecting:
    False
ok
Trying:
    same_first_last([4.8, 4.5])
Expecting:
    False
ok
1 items had no tests:
    __main__
1 items passed all tests:
   3 tests in __main__.same_first_last
3 tests in 2 items.
3 passed and 0 failed.
Test passed.
$

0 コメント:

コメントを投稿