Knuth–Morris–Pratt string search algorithm
This code does not work. In some cases it fails to find the pattern:

&gt;&gt;&gt; KMP_search('ab', 'aab')
[]


In other cases it raises an exception:

&gt;&gt;&gt; KMP_search('ab', 'ba')
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "cr190837.py", line 39, in KMP_search
    if pattern[j] == lst[i + j]:
IndexError: string index out of range


How could you have found these problems? Well, by testing the code thoroughly. One way to do this is to use random test case generation. The idea behind randomized testing is that it can test a much larger range of inputs than you could ever enter by hand, and it can test parts of the space of inputs that would not be reached by systematic test case generation.

So one idea for a random test case would be to generate a random string of length k:

haystack = ''.join(random.choices('abc', k=k))


(Here I'm using random.choices which was new in Python 3.6. If you're on an earlier version of Python, you could use a loop like random.choice('abc') for _ in range(k).)

Then pick a random substring:

start, stop = sorted(random.sample(range(k + 1), 2))
needle = haystack[start:stop]


Run KMP_search(needle, haystack) and check that the results are correct, for example by comparing with Python's built-in str.find.

This can all be packaged together into a unit test using the unittest module:

import random
import unittest

class TestKMPSearch(unittest.TestCase):
    "Unit tests for KMP_search."

    def test_positive(self):
        for k in range(1, 100):
            # Generate a random test case with k characters.
            haystack = ''.join(random.choices('abc', k=k))
            start, stop = sorted(random.sample(range(k + 1), 2))
            needle = haystack[start:stop]

            # Use Python's built-in str.find to compute the expected result.
            expected = []
            index = -1
            while True:
                index = haystack.find(needle, index + 1)
                if index == -1:
                    break
                expected.append(index)

            # Compare with the actual result.
            found = KMP_search(needle, haystack)
            self.assertEqual(found, expected,
                             "KMP_search({!r}, {!r})".format(needle, haystack))


(I called the method test_positive because the test cases are positive in the sense that the needle is found in the haystack. For really thorough testing you ought to generate some negative test cases too, ones in which the needle is not found. But this is enough to find some errors.)

Now if you run the unit tests you'll (most likely) get a failure like this:

$ python -munittest cr190837.py
F
======================================================================
FAIL: test_positive (cr190837.TestKMPSearch)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "cr190837.py", line 79, in test_positive
    "KMP_search({!r}, {!r})".format(needle, haystack))
AssertionError: Lists differ: [] != [3]

Second list contains 1 additional elements.
First extra element 0:
3

- []
+ [3]
?  +
 : KMP_search('bac', 'ccbbac')

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (failures=1)