## 2017年2月12日日曜日

### Python - 数学 - 体 - Python の内包表記に関する問題

コード(Emacs)

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

# 1.7.1

def my_filter(l, num):
return [n for n in l if n % num != 0]

# 1.7.2

def my_lists(l):
return [list(range(1, x + 1)) for x in l]

# 1.7.3

def my_function_composition(f, g):
return {x: g[f[x]] for x in f}

import unittest

class Test(unittest.TestCase):

def setUp(self):
pass

def tearDown(self):
pass

def test_my_filter(self):
self.assertEqual(my_filter([1, 2, 4, 5, 7], 2), [1, 5, 7])

def test_my_lists1(self):
self.assertEqual(my_lists([1, 2, 4]), [[1], [1, 2], [1, 2, 3, 4]])

def test_my_lists2(self):
self.assertEqual(my_lists([0]), [[]])

def test_my_function_composition(self):
f = {0: 'a', 1: 'b'}
g = dict(a='apple', b='banana')
self.assertEqual(my_function_composition(
f, g), {0: 'apple', 1: 'banana'})

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

```\$ ./sample7_1.py -v
test_my_filter (__main__.Test) ... ok
test_my_function_composition (__main__.Test) ... ok
test_my_lists1 (__main__.Test) ... ok
test_my_lists2 (__main__.Test) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
\$
```