2018-10-02 03:08:42 +00:00
|
|
|
"""
|
|
|
|
This is pure python implementation of sentinel linear search algorithm
|
|
|
|
|
|
|
|
For doctests run following command:
|
|
|
|
python -m doctest -v sentinel_linear_search.py
|
|
|
|
or
|
|
|
|
python3 -m doctest -v sentinel_linear_search.py
|
|
|
|
|
|
|
|
For manual testing run:
|
|
|
|
python sentinel_linear_search.py
|
|
|
|
"""
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2018-10-02 03:08:42 +00:00
|
|
|
def sentinel_linear_search(sequence, target):
|
|
|
|
"""Pure implementation of sentinel linear search algorithm in Python
|
|
|
|
|
|
|
|
:param sequence: some sequence with comparable items
|
|
|
|
:param target: item value to search
|
|
|
|
:return: index of found item or None if item is not found
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
>>> sentinel_linear_search([0, 5, 7, 10, 15], 0)
|
|
|
|
0
|
|
|
|
|
|
|
|
>>> sentinel_linear_search([0, 5, 7, 10, 15], 15)
|
|
|
|
4
|
|
|
|
|
|
|
|
>>> sentinel_linear_search([0, 5, 7, 10, 15], 5)
|
|
|
|
1
|
|
|
|
|
|
|
|
>>> sentinel_linear_search([0, 5, 7, 10, 15], 6)
|
|
|
|
|
|
|
|
"""
|
|
|
|
sequence.append(target)
|
|
|
|
|
|
|
|
index = 0
|
|
|
|
while sequence[index] != target:
|
|
|
|
index += 1
|
|
|
|
|
|
|
|
sequence.pop()
|
|
|
|
|
|
|
|
if index == len(sequence):
|
|
|
|
return None
|
|
|
|
|
|
|
|
return index
|
|
|
|
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
user_input = input("Enter numbers separated by comma:\n").strip()
|
|
|
|
sequence = [int(item) for item in user_input.split(",")]
|
2018-10-02 03:08:42 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
target_input = input("Enter a single number to be found in the list:\n")
|
2018-10-02 03:08:42 +00:00
|
|
|
target = int(target_input)
|
|
|
|
result = sentinel_linear_search(sequence, target)
|
|
|
|
if result is not None:
|
2019-12-07 05:39:59 +00:00
|
|
|
print(f"{target} found at positions: {result}")
|
2018-10-02 03:08:42 +00:00
|
|
|
else:
|
2019-10-05 05:14:13 +00:00
|
|
|
print("Not found")
|