## 2017年2月13日月曜日

### Python - 数学 - 体 - Python のループに関する問題

コード(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()
```

```\$ ./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
\$
```