Perform linear search in list. Returns -1 if element is not found. Parameters ---------- left : int left index bound. right : int right index bound. array : List[int] List of elements to be searched on target : int Element that is searched
(left: int, right: int, array: list[int], target: int)
| 18 | |
| 19 | |
| 20 | def lin_search(left: int, right: int, array: list[int], target: int) -> int: |
| 21 | """Perform linear search in list. Returns -1 if element is not found. |
| 22 | |
| 23 | Parameters |
| 24 | ---------- |
| 25 | left : int |
| 26 | left index bound. |
| 27 | right : int |
| 28 | right index bound. |
| 29 | array : List[int] |
| 30 | List of elements to be searched on |
| 31 | target : int |
| 32 | Element that is searched |
| 33 | |
| 34 | Returns |
| 35 | ------- |
| 36 | int |
| 37 | index of element that is looked for. |
| 38 | |
| 39 | Examples |
| 40 | -------- |
| 41 | >>> lin_search(0, 4, [4, 5, 6, 7], 7) |
| 42 | 3 |
| 43 | >>> lin_search(0, 3, [4, 5, 6, 7], 7) |
| 44 | -1 |
| 45 | >>> lin_search(0, 2, [-18, 2], -18) |
| 46 | 0 |
| 47 | >>> lin_search(0, 1, [5], 5) |
| 48 | 0 |
| 49 | >>> lin_search(0, 3, ['a', 'c', 'd'], 'c') |
| 50 | 1 |
| 51 | >>> lin_search(0, 3, [.1, .4 , -.1], .1) |
| 52 | 0 |
| 53 | >>> lin_search(0, 3, [.1, .4 , -.1], -.1) |
| 54 | 2 |
| 55 | """ |
| 56 | for i in range(left, right): |
| 57 | if array[i] == target: |
| 58 | return i |
| 59 | return -1 |
| 60 | |
| 61 | |
| 62 | def ite_ternary_search(array: list[int], target: int) -> int: |
no outgoing calls
no test coverage detected