A pure Python implementation of a linear search algorithm :param sequence: a collection with comparable items (sorting is not required for linear search) :param target: item value to search :return: index of found item or -1 if item is not found Examples: >>> linear_sea
(sequence: list, target: int)
| 10 | |
| 11 | |
| 12 | def linear_search(sequence: list, target: int) -> int: |
| 13 | """A pure Python implementation of a linear search algorithm |
| 14 | |
| 15 | :param sequence: a collection with comparable items (sorting is not required for |
| 16 | linear search) |
| 17 | :param target: item value to search |
| 18 | :return: index of found item or -1 if item is not found |
| 19 | |
| 20 | Examples: |
| 21 | >>> linear_search([0, 5, 7, 10, 15], 0) |
| 22 | 0 |
| 23 | >>> linear_search([0, 5, 7, 10, 15], 15) |
| 24 | 4 |
| 25 | >>> linear_search([0, 5, 7, 10, 15], 5) |
| 26 | 1 |
| 27 | >>> linear_search([0, 5, 7, 10, 15], 6) |
| 28 | -1 |
| 29 | """ |
| 30 | for index, item in enumerate(sequence): |
| 31 | if item == target: |
| 32 | return index |
| 33 | return -1 |
| 34 | |
| 35 | |
| 36 | def rec_linear_search(sequence: list, low: int, high: int, target: int) -> int: |