Skip to content

Instantly share code, notes, and snippets.

@kantologist
Last active November 5, 2017 22:04
Show Gist options
  • Save kantologist/76625854ce5632c522cfb9d31d926794 to your computer and use it in GitHub Desktop.
Save kantologist/76625854ce5632c522cfb9d31d926794 to your computer and use it in GitHub Desktop.
an algorithm for finding a sub sequence in an sequence.
import unittest
class TestSubInSeq(unittest.TestCase):
def test_true(self):
self.assertTrue(sub_in_seq([1,3,4], [2,3,4,1,3,4]))
def test_false(self):
self.assertFalse(sub_in_seq([1,3,4], [1,2,4,1,3,1,4,3]))
def sub_in_seq(sub, seq):
for a in [i for i,x in enumerate(seq) if x == sub[0]]:
try:
sub_from_seq = seq[a:a+len(sub)+1]
if sub_from_seq == sub:
return True
except IndexError:
return false
return False
if __name__ == '__main__':
print "testing..."
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment