#format python """SlurpysProblem solution by yong27, 2005-04 """ import unittest def isSlump(s): if s[0]=='D' or s[0]=='E': s = s[1:] while s: if (len(s) == 1 and s[0] == "G") or isSlump(s): return True elif s[0] != "F": return s = s[1:] def isSlimp(s): if s and s[0] == "A": if len(s) == 2 and s[1] == "H": return True elif s[-1] == "C": if s[1] == "B" and isSlimp(s[2:-1]): return True elif isSlump(s[1:-1]): return True def isSlurpy(s): for i in range(len(s)): if isSlimp(s[:i+1]) and isSlump(s[i+1:]): return True class SlurpyTest(unittest.TestCase): def testIsSlump(self): self.assert_(isSlump("DFG")) self.assert_(isSlump("EFG")) self.assert_(isSlump("DFFFFFFG")) self.assert_(isSlump("DFDFDFDFG")) self.assert_(isSlump("DFEFFFFFFG")) def testIsNotSlump(self): self.assertFalse(isSlump("DF")) self.assertFalse(isSlump("DFGA")) self.assertFalse(isSlump("DFFFFDFK")) def testIsSlimp(self): self.assert_(isSlimp("AH")) self.assert_(isSlimp("ABAHC")) self.assert_(isSlimp("ADFFFFGC")) def testIsNotSlimp(self): self.assertFalse(isSlimp("ABAH")) self.assertFalse(isSlimp("ABABAHC")) def testIsSlurpy(self): self.assert_(isSlurpy("AHDFG")) self.assert_(isSlurpy("ADFGCDFFFFFFG")) def testIsNotSlurpy(self): self.assertFalse(isSlurpy("AHDFGA")) self.assertFalse(isSlurpy("ABABCC")) self.assertFalse(isSlurpy("DFGAH")) if __name__=='__main__': unittest.main()