2017年2月13日月曜日

開発環境

行列プログラマー(Philip N. Klein (著)、 松田 晃一 (翻訳)、 弓林 司 (翻訳)、 脇本 佑紀 (翻訳)、 中田 洋 (翻訳)、 齋藤 大吾 (翻訳)、オライリージャパン)の1章(体)、1.7(問題)、Python のループに関する問題、問題1.7.4、1.7.5、1.7.6、1.7.7、1.7.8、1.7.9を取り組んでみる。

問題1.7.4、1.7.5、1.7.6、1.7.7、1.7.8、1.7.9

コード(Emacs)

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

# 1.7.4


def mySum(l):
    current = 0
    for x in l:
        current += x
    return current

# 1.7.5


def myProduct(l):
    current = 1
    for x in l:
        current *= x
    return current


# 1.7.6


def myMin(l):
    current = None
    for x in l:
        if current is None:
            current = x
        elif x < current:
            current = x
    return current


# 1.7.7


def myConcat(l):
    current = ''
    for x in l:
        current += x
    return current


# 1.7.8


def myUnion(l):
    current = set()
    for x in l:
        current |= x
    return current

# 1.7.9

import unittest


class Test(unittest.TestCase):

    def setUp(self):
        self.l0 = []
        self.l1 = [1]
        self.l10 = list(range(1, 11))

    def tearDown(self):
        pass

    def test_mySum0(self):
        self.assertEqual(mySum(self.l0), 0)

    def test_mySum1(self):
        self.assertEqual(mySum(self.l1), 1)

    def test_mySum10(self):
        self.assertEqual(mySum(self.l10), 55)

    def test_myProduct0(self):
        self.assertEqual(myProduct(self.l0), 1)

    def test_myProduct1(self):
        self.assertEqual(myProduct(self.l1), 1)

    def test_myProduct10(self):
        self.assertEqual(myProduct(self.l10), 3628800)

    def test_myMin0(self):
        self.assertEqual(myMin(self.l0), None)

    def test_myMin1(self):
        self.assertEqual(myMin(self.l1), 1)

    def test_myMin10(self):
        self.assertEqual(myMin(self.l10), 1)

    def test_myConcat0(self):
        self.assertEqual(myConcat([]), '')

    def test_myConcat1(self):
        self.assertEqual(myConcat(['python']), 'python')

    def test_myConcat2(self):
        self.assertEqual(myConcat(['python', 'scheme']), 'pythonscheme')

    def test_myUnion0(self):
        self.assertEqual(myUnion([]), set())

    def test_myUnion1(self):
        self.assertEqual(myUnion([{1}]), {1})

    def test_myUnion2(self):
        self.assertEqual(myUnion([set(range(5)), set(range(2, 10))]),
                         set(range(10)))

if __name__ == '__main__':
    unittest.main()

入出力結果(Terminal, IPython)

$ ./sample7_4.py -v
test_myConcat0 (__main__.Test) ... ok
test_myConcat1 (__main__.Test) ... ok
test_myConcat2 (__main__.Test) ... ok
test_myMin0 (__main__.Test) ... ok
test_myMin1 (__main__.Test) ... ok
test_myMin10 (__main__.Test) ... ok
test_myProduct0 (__main__.Test) ... ok
test_myProduct1 (__main__.Test) ... ok
test_myProduct10 (__main__.Test) ... ok
test_mySum0 (__main__.Test) ... ok
test_mySum1 (__main__.Test) ... ok
test_mySum10 (__main__.Test) ... ok
test_myUnion0 (__main__.Test) ... ok
test_myUnion1 (__main__.Test) ... ok
test_myUnion2 (__main__.Test) ... ok

----------------------------------------------------------------------
Ran 15 tests in 0.001s

OK
$

0 コメント:

コメントを投稿

Comments on Google+: