2014年9月6日土曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 9(Repeating Code Using Loops)、9.10(Exercises) 5.を解いてみる。

9.10(Exercises) 5.

コード(BBEdit)

sample5.py

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

def mysteryFunction(values):
    """ (list) -> list
    parameter is list of list.
    Return the list of list, each sublist is reversed.
    >>> mysteryFunction([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
    [[5, 4, 3, 2, 1], [10, 9, 8, 7, 6]]
    >>> mysteryFunction([[1, 2]])
    [[2, 1]]
    >>> mysteryFunction([[1, 2], [1]])
    [[2, 1], [1]]
    >>> mysteryFunction([[1], [1, 2]])
    [[1], [2, 1]]
    """
    result = []
    for sublist in values:
        result.append([sublist[0]])
        for i in sublist[1:]:
            result[-1].insert(0, i)
    return result

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

入出力結果(Terminal, IPython)

$ ./sample5.py -v
Trying:
    mysteryFunction([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
Expecting:
    [[5, 4, 3, 2, 1], [10, 9, 8, 7, 6]]
ok
Trying:
    mysteryFunction([[1, 2]])
Expecting:
    [[2, 1]]
ok
Trying:
    mysteryFunction([[1, 2], [1]])
Expecting:
    [[2, 1], [1]]
ok
Trying:
    mysteryFunction([[1], [1, 2]])
Expecting:
    [[1], [2, 1]]
ok
1 items had no tests:
    __main__
1 items passed all tests:
   4 tests in __main__.mysteryFunction
4 tests in 2 items.
4 passed and 0 failed.
Test passed.
$

0 コメント:

コメントを投稿