2016-07-29 20:06:49 +00:00
|
|
|
"""
|
|
|
|
This is pure python implementation of linear search algorithm
|
|
|
|
|
|
|
|
For doctests run following command:
|
|
|
|
python -m doctest -v linear_search.py
|
|
|
|
or
|
|
|
|
python3 -m doctest -v linear_search.py
|
|
|
|
|
|
|
|
For manual testing run:
|
|
|
|
python linear_search.py
|
|
|
|
"""
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
|
|
|
|
def linear_search(sequence, target):
|
2016-08-30 19:06:49 +00:00
|
|
|
"""Pure implementation of linear search algorithm in Python
|
2016-07-29 20:06:49 +00:00
|
|
|
|
|
|
|
:param sequence: some sorted collection with comparable items
|
|
|
|
:param target: item value to search
|
|
|
|
:return: index of found item or None if item is not found
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
>>> linear_search([0, 5, 7, 10, 15], 0)
|
|
|
|
0
|
|
|
|
|
|
|
|
>>> linear_search([0, 5, 7, 10, 15], 15)
|
|
|
|
4
|
|
|
|
|
|
|
|
>>> linear_search([0, 5, 7, 10, 15], 5)
|
|
|
|
1
|
|
|
|
|
|
|
|
>>> linear_search([0, 5, 7, 10, 15], 6)
|
|
|
|
|
|
|
|
"""
|
|
|
|
for index, item in enumerate(sequence):
|
|
|
|
if item == target:
|
|
|
|
return index
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
|
|
|
|
2017-10-07 13:47:50 +00:00
|
|
|
# For python 2.x and 3.x compatibility: 3.x has no raw_input builtin
|
2016-07-29 20:06:49 +00:00
|
|
|
# otherwise 2.x's input builtin function is too "smart"
|
|
|
|
if sys.version_info.major < 3:
|
|
|
|
input_function = raw_input
|
|
|
|
else:
|
|
|
|
input_function = input
|
|
|
|
|
|
|
|
user_input = input_function('Enter numbers separated by coma:\n')
|
|
|
|
sequence = [int(item) for item in user_input.split(',')]
|
|
|
|
|
|
|
|
target_input = input_function(
|
|
|
|
'Enter a single number to be found in the list:\n'
|
|
|
|
)
|
|
|
|
target = int(target_input)
|
|
|
|
result = linear_search(sequence, target)
|
|
|
|
if result is not None:
|
|
|
|
print('{} found at positions: {}'.format(target, result))
|
|
|
|
else:
|
|
|
|
print('Not found')
|