1. 10進数で回文¶
10進数、2進数、8進数のいずれで表現しても回文数となる数のうち、10進数の10以上で最小の値を求めてください。
10進数 |
2進数 |
8進数 |
---|---|---|
0 |
0 |
0 |
1 |
1 |
1 |
2 |
10 |
2 |
3 |
11 |
3 |
7 |
111 |
7 |
8 |
1000 |
10 |
9 |
1001 |
11 |
10 |
1010 |
12 |
1.1. テストファースト¶
[14]:
import unittest
class 回文Test(unittest.TestCase):
None
unittest.main(argv=[''],exit=False)
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
[14]:
<unittest.main.TestProgram at 0x10f4dc190>
1.2. アサートファースト¶
[15]:
import unittest
class 回文Test(unittest.TestCase):
def test_10進数の11ならば11になる(self):
self.assertEqual(回文(11), True)
unittest.main(argv=[''],exit=False)
F
======================================================================
FAIL: test_10進数の11ならば11になる (__main__.回文Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "<ipython-input-15-a0794e691db4>", line 5, in test_10進数の11ならば11になる
self.assertEqual(回文(11), True)
AssertionError: False != True
----------------------------------------------------------------------
Ran 1 test in 0.002s
FAILED (failures=1)
[15]:
<unittest.main.TestProgram at 0x10f500e50>
1.3. 仮実装¶
[16]:
import unittest
def 回文(n):
return True
class 回文Test(unittest.TestCase):
def test_10進数の11ならば11になる(self):
self.assertEqual(回文(11), True)
unittest.main(argv=[''],exit=False)
.
----------------------------------------------------------------------
Ran 1 test in 0.002s
OK
[16]:
<unittest.main.TestProgram at 0x10f1f5310>
1.4. 三角測量¶
[17]:
import unittest
def 回文(n):
return True
class 回文Test(unittest.TestCase):
def test_10進数の11ならば11になる(self):
self.assertEqual(回文(11), True)
self.assertEqual(回文(10), False)
unittest.main(argv=[''],exit=False)
F
======================================================================
FAIL: test_10進数の11ならば11になる (__main__.回文Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "<ipython-input-17-12cefe353413>", line 10, in test_10進数の11ならば11になる
self.assertEqual(回文(10), False)
AssertionError: True != False
----------------------------------------------------------------------
Ran 1 test in 0.002s
FAILED (failures=1)
[17]:
<unittest.main.TestProgram at 0x10f1f5e10>
1.5. 明白な実装¶
[18]:
import unittest
def 回文(n):
result = str(n)
if result == result[::-1]:
return True
return False
class 回文Test(unittest.TestCase):
def test_10進数の11ならば11になる(self):
self.assertEqual(回文(11), True)
self.assertEqual(回文(10), False)
unittest.main(argv=[''],exit=False)
.
----------------------------------------------------------------------
Ran 1 test in 0.002s
OK
[18]:
<unittest.main.TestProgram at 0x10f4e13d0>
[19]:
import unittest
import pdb
def 回文(n):
result = str(n)
if result == result[::-1]:
return True
return False
class 回文Test(unittest.TestCase):
def test_10進数の11ならば11になる(self):
self.assertEqual(回文(11), True)
self.assertEqual(回文(10), False)
def test_2進数を表示する(self):
self.assertEqual(format(9, 'b'), '1001')
def test_8進数を表示する(self):
self.assertEqual(format(9, 'o'), '11')
unittest.main(argv=[''],exit=False)
...
----------------------------------------------------------------------
Ran 3 tests in 0.004s
OK
[19]:
<unittest.main.TestProgram at 0x10f50ef10>
[20]:
import unittest
import pdb
def 回文(n):
decimal = str(n)
binary = format(n, 'b')
octet = format(n, 'o')
if decimal == decimal[::-1] and binary == binary[::-1] and octet == octet[::-1]:
return True
return False
class 回文Test(unittest.TestCase):
def test_10進数の9ならば(self):
self.assertEqual(回文(9), True)
def test_10進数の10以上で最小の値は(self):
n = 10
while True:
result = 回文(n)
if result == True:
break;
else:
n += 1
self.assertEqual(n, 585)
print(f"最小の値は{n}")
def test_2進数を表示する(self):
self.assertEqual(format(9, 'b'), '1001')
def test_8進数を表示する(self):
self.assertEqual(format(9, 'o'), '11')
unittest.main(argv=[''],exit=False)
....
----------------------------------------------------------------------
Ran 4 tests in 0.004s
OK
最小の値は585
[20]:
<unittest.main.TestProgram at 0x10f2713d0>
[21]:
import unittest
import doctest
import pdb
def 回文(n):
"""10進数,2進数,8進数いずれで表現しても回文数となる場合はTureを返す
>>> 回文(9)
True
"""
decimal = str(n)
binary = format(n, 'b')
octet = format(n, 'o')
if decimal == decimal[::-1] and binary == binary[::-1] and octet == octet[::-1]:
return True
return False
doctest.testmod(verbose=True)
class 回文Test(unittest.TestCase):
def test_10進数の9ならば(self):
self.assertEqual(回文(9), True)
def test_10進数の10以上で最小の値は(self):
n = 10
while True:
result = 回文(n)
if result == True:
break;
else:
n += 1
self.assertEqual(n, 585)
print(f"最小の値は{n}")
unittest.main(argv=[''],exit=False)
..
----------------------------------------------------------------------
Ran 2 tests in 0.003s
OK
Trying:
回文(9)
Expecting:
True
ok
6 items had no tests:
__main__
__main__.回文Test
__main__.回文Test.test_10進数の10以上で最小の値は
__main__.回文Test.test_10進数の9ならば
__main__.回文Test.test_2進数を表示する
__main__.回文Test.test_8進数を表示する
1 items passed all tests:
1 tests in __main__.回文
1 tests in 7 items.
1 passed and 0 failed.
Test passed.
最小の値は585
[21]:
<unittest.main.TestProgram at 0x10f25f590>
1.6. リファクタリング¶
[22]:
import doctest
import pdb
def 回文(n):
"""10進数,2進数,8進数いずれで表現しても回文数となる場合はTureを返す
>>> 回文(9)
True
"""
回文10進数 = str(n) == str(n)[::-1]
回文2進数 = format(n, 'b') == format(n, 'b')[::-1]
回文8進数 = format(n, 'o') == format(n, 'o')[::-1]
if 回文10進数 and 回文2進数 and 回文8進数:
return True
return False
doctest.testmod(verbose=True)
Trying:
回文(9)
Expecting:
True
ok
4 items had no tests:
__main__
__main__.回文Test
__main__.回文Test.test_10進数の10以上で最小の値は
__main__.回文Test.test_10進数の9ならば
1 items passed all tests:
1 tests in __main__.回文
1 tests in 5 items.
1 passed and 0 failed.
Test passed.
[22]:
TestResults(failed=0, attempted=1)