練習問題
#!/usr/bin/python
# -*- coding: utf-8 -*-
# main.py
from collections import namedtuple
X = namedtuple('X', 'letter, pos')
class Solver:
def __init__(self, s):
self.copy = s
self.arr = list(s)
self.cost = 0
def __change(self, letter, cost, *pos):
for p in pos:
self.arr[p] = letter
self.cost += cost * len(pos)
__to_N = lambda self, *pos: self.__change('N', 10, *pos)
__to_O = lambda self, *pos: self.__change('O', 15, *pos)
def to_palindromically(self):
sz = len(self.arr)
for i in xrange(sz/2):
j = -1 - i
if self.arr[i] == self.arr[j]:
if self.arr[i] == '*':
self.__to_N(i, j)
else:
cnt = (self.arr[i], self.arr[j]).count('*')
if cnt == 0:
return (self.copy, -1)
elif cnt == 1:
srt = sorted((X(self.arr[i], i), X(self.arr[j], j)))
if srt[-1].letter == 'N':
self.__to_N(srt[0].pos)
elif srt[-1].letter == 'O':
self.__to_O(srt[0].pos)
if sz & 1 and self.arr[sz/2] == '*':
self.__to_N(sz/2)
return (''.join(self.arr), self.cost)
#!/usr/bin/python
# -*- coding: utf-8 -*-
# test.py
from main import Solver
from collections import namedtuple
import unittest
T = namedtuple('T', 'input_data, expected')
test_cases = [
T('NOO*', ('NOON', 10)),
T('NO*N', ('NOON', 15)),
T('NO', ('NO', -1)),
T('O*O', ('ONO', 10)),
T('*********', ('NNNNNNNNN', 90)),
T('**********', ('NNNNNNNNNN', 100)),
T('*O*******', ('NONNNNNON', 85)),
T('*O********', ('NONNNNNNON', 95)),
T('*N*******', ('NNNNNNNNN', 80)),
T('*N********', ('NNNNNNNNNN', 90)),
T('*N*****O*', ('*N*****O*', -1)),
T('*N******O*', ('*N******O*', -1)),
T('****N****', ('NNNNNNNNN', 80)),
T('****N*****', ('NNNNNNNNNN', 90)),
T('****O****', ('NNNNONNNN', 80)),
T('****NO****', ('****NO****', -1)),
]
class TestSequence(unittest.TestCase):
pass
def test_generator(input_data, expected):
def test(self):
obj = Solver(input_data)
self.assertEqual(obj.to_palindromically(), expected)
return test
for case in test_cases:
name = 'test{0}'.format(case.input_data)
test = test_generator(case.input_data, case.expected)
setattr(TestSequence, name, test)
suite = unittest.TestLoader().loadTestsFromTestCase(TestSequence)
unittest.TextTestRunner().run(suite)