MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / linear_search

Function linear_search

searches/linear_search.py:12–33  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

10
11
12def 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
36def rec_linear_search(sequence: list, low: int, high: int, target: int) -> int:

Callers 1

linear_search.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected