2013年12月14日土曜日

開発環境

初めてのコンピュータサイエンス(Jennifer CampbellPaul GriesJason MontojoGreg Wilson(著)長尾 高弘(翻訳))の12章(各種ツール)、12.7(練習問題)、12-10.を解いてみる。

12.7(練習問題)、12-10.

コード(BBEdit)

sample.py

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

import nose

def isSorted(s):
    if type(s) != list or len(list(filter(lambda x:type(x) != int, s))):
        raise ValueError
    return s == sorted(s)

def test_valueerror():
    ''' リストではない'''
    try:
        isSorted("python")
        assert False
    except ValueError: pass

def test_is_all_int():
    '''全て整数のリスト'''
    try:
        isSorted([1, 'a'])
        False
    except ValueError: pass

def test_empty():
    '''空のリスト'''
    assert isSorted([])

def test_one():
    '''要素が1つのリスト'''
    assert isSorted([10])

def test_two_sorted():
    '''要素が2つのソート済リスト'''
    assert isSorted([1, 2])

def test_two_reversed():
    '''要素が2つの降順リスト'''
    assert not isSorted([2, 1])

def test_all_equal():
    '''全て等しい要素のリスト'''
    assert isSorted([10, 10])

def test_first():
    '''先頭がソートされてないリスト'''
    assert not isSorted([10, 1, 2, 3, 4, 5])

def test_middle():
    ''' 真ん中の要素がソートされてないリスト'''
    assert not isSorted([1, 2, 10, 4, 5])

def test_last():
    '''末尾がソートされてないリスト'''
    assert not isSorted([1, 2, 3, 4, 0])

if __name__ == '__main__':
    nose.runmodule()

入出力結果(Terminal)

$ ./sample.py
..........
----------------------------------------------------------------------
Ran 10 tests in 0.002s

OK
$

0 コメント:

コメントを投稿