No older revisions available
No older revisions available
~cpp
import unittest
class Slurpys:
def __init__(self):
self.result = ()
def isSlimp(self, aStr):
if not aStr: return False
if aStr[0] != 'A':
return False
if len(aStr) == 2 and aStr[1] == 'H':
return True
else:
if aStr[-1] != 'C': return False
if aStr[0:2] == 'AB' and self.isSlimp(aStr[2:-1]):
return True
if aStr[0] == 'A' and self.isSlump(aStr[1:-1]):
return True
return False
def isSlump(self, aStr):
if aStr[0] == 'D' or aStr[0] == 'E':
pass
else: return False
if aStr[1] != 'F':
return False
index = 0
for ch in aStr[1:]:
if ch != 'F':
index = aStr.find(ch,1)
break
else: return False
if aStr[index] == 'G':
return True
elif self.isSlump(aStr[index:]):
return True
return False
def isSlurpy(self, aStr):
if aStr[-1] != 'G':
return False
if aStr[0:2] == 'AH' and self.isSlump(aStr[2:]):
return True
else:
slump = self.findSlumpStartIndex(aStr)
if slump != -1:
return self.isSlimpAndSlump(aStr, slump)
return False
def findSlumpStartIndex(self, aStr):
if aStr.find('CD') != -1:
return aStr.find('CD') + 1
elif aStr.find('CE') != -1:
return aStr.find('CE') + 1
else: return -1
def isSlimpAndSlump(self, aStr, slump):
if self.isSlimp(aStr[0:slump]) and self.isSlump(aStr[slump:]):
return True
return False
class SlurpysTestCase(unittest.TestCase):
def testSlimp(self):
slimp = Slurpys()
self.assertEquals(True, slimp.isSlimp('AH'))
self.assertEquals(True, slimp.isSlimp('ABAHC'))
self.assertEquals(True, slimp.isSlimp('ABABAHCC'))
self.assertEquals(True, slimp.isSlimp('ADFGC'))
self.assertEquals(True, slimp.isSlimp('ADFFFFGC'))
self.assertEquals(True, slimp.isSlimp('ABAEFGCC'))
self.assertEquals(True, slimp.isSlimp('ADFDFGC'))
self.assertEquals(False, slimp.isSlimp('ABC'))
self.assertEquals(False, slimp.isSlimp('ABAH'))
self.assertEquals(False, slimp.isSlimp('DFGC'))
self.assertEquals(False, slimp.isSlimp('ABABAHC'))
self.assertEquals(False, slimp.isSlimp('SLIMP'))
self.assertEquals(False, slimp.isSlimp('ADGC'))
def testSlump(self):
slump = Slurpys()
self.assertEquals(True, slump.isSlump('DFG'))
self.assertEquals(True, slump.isSlump('EFG'))
self.assertEquals(True, slump.isSlump('DFFFFFG'))
self.assertEquals(True, slump.isSlump('DFDFDFDFG'))
self.assertEquals(True, slump.isSlump('DFEFFFFFG'))
self.assertEquals(False, slump.isSlump('DFEFF'))
self.assertEquals(False, slump.isSlump('EFAHG'))
self.assertEquals(False, slump.isSlump('DEFG'))
self.assertEquals(False, slump.isSlump('DG'))
self.assertEquals(False, slump.isSlump('EFFFFDG'))
def testSlurpy(self):
slurpy = Slurpys()
self.assertEquals(True, slurpy.isSlurpy('AHDFG'))
self.assertEquals(True, slurpy.isSlurpy('ADFGCDFFFFFG'))
self.assertEquals(True, slurpy.isSlurpy('ABAEFGCCDFEFFFFFG'))
self.assertEquals(False, slurpy.isSlurpy('AHDFGA'))
self.assertEquals(False, slurpy.isSlurpy('DFGA5H'))
self.assertEquals(False, slurpy.isSlurpy('ABABCC'))
def main():
slurpy = Slurpys()
while True:
size = input()
if 1 <= size <= 10:
break
else: print 'input size again'
for num in range(size):
while True:
string = raw_input()
if 1 <= len(string) <= 60:
break
else: print 'input string again'
if slurpy.isSlurpy(string):
slurpy.result += 'YES',
else: slurpy.result += 'NO',
print 'SLURPYS OUTPUT'
for num in range(size):
print slurpy.result[num]
print 'END OF OUTPUT'
if __name__ == '__main__':
#unittest.main(argv=('', '-v'))
main()
쓰레드 ¶
- 테스트와 함께하니 재밌다. 문자열의 순서를 따지느라 if-else를 남발했는데 리펙토링하기 참 힘들다 -- 재선