1 """SlurpysProblem solution by yong27, 2005-04
   2 """
   3 import unittest
   4 
   5 def isSlump(s):
   6     if s[0]=='D' or s[0]=='E':
   7         s = s[1:]
   8         while s:
   9             if (len(s) == 1 and s[0] == "G") or isSlump(s):
  10                 return True
  11             elif s[0] != "F":
  12                 return
  13             s = s[1:]
  14 
  15 def isSlimp(s):
  16     if s and s[0] == "A":
  17         if len(s) == 2 and s[1] == "H":
  18             return True
  19         elif s[-1] == "C":
  20             if s[1] == "B" and isSlimp(s[2:-1]):
  21                 return True
  22             elif isSlump(s[1:-1]):
  23                 return True
  24 
  25 def isSlurpy(s):
  26     for i in range(len(s)):
  27         if isSlimp(s[:i+1]) and isSlump(s[i+1:]):
  28             return True
  29 
  30 
  31 class SlurpyTest(unittest.TestCase):
  32     def testIsSlump(self):
  33         self.assert_(isSlump("DFG"))
  34         self.assert_(isSlump("EFG"))
  35         self.assert_(isSlump("DFFFFFFG"))
  36         self.assert_(isSlump("DFDFDFDFG"))
  37         self.assert_(isSlump("DFEFFFFFFG"))
  38 
  39     def testIsNotSlump(self):
  40         self.assertFalse(isSlump("DF"))
  41         self.assertFalse(isSlump("DFGA"))
  42         self.assertFalse(isSlump("DFFFFDFK"))
  43 
  44     def testIsSlimp(self):
  45         self.assert_(isSlimp("AH"))
  46         self.assert_(isSlimp("ABAHC"))
  47         self.assert_(isSlimp("ADFFFFGC"))
  48 
  49     def testIsNotSlimp(self):
  50         self.assertFalse(isSlimp("ABAH"))
  51         self.assertFalse(isSlimp("ABABAHC"))
  52 
  53     def testIsSlurpy(self):
  54         self.assert_(isSlurpy("AHDFG"))
  55         self.assert_(isSlurpy("ADFGCDFFFFFFG"))
  56 
  57     def testIsNotSlurpy(self):
  58         self.assertFalse(isSlurpy("AHDFGA"))
  59         self.assertFalse(isSlurpy("ABABCC"))
  60         self.assertFalse(isSlurpy("DFGAH"))
  61 
  62 if __name__=='__main__':
  63     unittest.main()
web biohackers.net