Skip to content

Instantly share code, notes, and snippets.

Created July 8, 2014 14:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/6910a85b4978daee137f to your computer and use it in GitHub Desktop.
Save anonymous/6910a85b4978daee137f to your computer and use it in GitHub Desktop.
def contains(subseq, inseq):
return any(inseq[pos:pos + len(subseq)] == subseq for pos in range(0, len(inseq) - len(subseq) + 1))
class TestContainsForString(unittest.TestCase):
def test_smoketest(self):
self.assertTrue(contains('abc', 'abc cde fgi'))
def testcontains_middle_seq(self):
self.assertTrue(contains('cde', 'abc cde fgi'))
def testcontains_end_seq(self):
self.assertTrue(contains('fgi', 'abc cde fgi'))
def testcontains_oversized_seq(self):
self.assertFalse(contains('abc cde fgi!', 'abc cde fgi'))
def testcontains_iteslf(self):
self.assertTrue(contains('abc cde fgi', 'abc cde fgi'))
class TestContainsForList(unittest.TestCase):
def test_smoketest(self):
self.assertTrue(contains('ab c'.split(' '), 'ab c cde fgi'.split(' ')))
def testcontains_middle_seq(self):
self.assertTrue(contains('cd e'.split(' '), 'abc cd e fgi'.split(' ')))
def testcontains_end_seq(self):
self.assertTrue(contains('f gi'.split(' '), 'abc cde f gi'.split(' ')))
def testcontains_oversized_seq(self):
self.assertFalse(contains('abc cde fgi fgi'.split(' '), 'abc cde fgi'.split(' ')))
def testcontains_iteslf(self):
self.assertTrue(contains(*['abc cde fgi'] * 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment