Calculates the new index we should go to if we fail a comparison :param pattern: :return:
(pattern)
| 34 | |
| 35 | |
| 36 | def get_failure_array(pattern): |
| 37 | """ |
| 38 | Calculates the new index we should go to if we fail a comparison |
| 39 | :param pattern: |
| 40 | :return: |
| 41 | """ |
| 42 | failure = [0] |
| 43 | i = 0 |
| 44 | j = 1 |
| 45 | while j < len(pattern): |
| 46 | if pattern[i] == pattern[j]: |
| 47 | i += 1 |
| 48 | elif i > 0: |
| 49 | i = failure[i-1] |
| 50 | continue |
| 51 | j += 1 |
| 52 | failure.append(i) |
| 53 | return failure |
| 54 | |
| 55 | |
| 56 | if __name__ == '__main__': |
no outgoing calls
no test coverage detected