2016-07-29 19:48:47 +00:00
|
|
|
import sys
|
|
|
|
|
2016-07-22 17:04:54 +00:00
|
|
|
|
2016-07-29 19:14:30 +00:00
|
|
|
def sequential_search(alist, target):
|
2016-07-29 19:31:20 +00:00
|
|
|
for index, item in enumerate(alist):
|
|
|
|
if item == target:
|
|
|
|
print("Found target {} at index {}".format(target, index))
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
print("Not found")
|
2016-07-29 19:14:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2016-07-29 19:48:47 +00:00
|
|
|
# Python 2's `raw_input` has been renamed to `input` in Python 3
|
|
|
|
if sys.version_info.major < 3:
|
|
|
|
input_function = raw_input
|
|
|
|
else:
|
|
|
|
input_function = input
|
|
|
|
|
2016-07-29 19:31:20 +00:00
|
|
|
try:
|
|
|
|
print("Enter numbers separated by spaces")
|
2016-07-29 19:48:47 +00:00
|
|
|
s = input_function()
|
2016-07-29 19:31:20 +00:00
|
|
|
inputs = list(map(int, s.split(' ')))
|
2016-07-29 19:48:47 +00:00
|
|
|
target = int(input_function('\nEnter a number to be found in list: '))
|
2016-07-29 19:31:20 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
else:
|
|
|
|
sequential_search(inputs, target)
|
2016-07-29 19:14:30 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2016-07-29 19:31:20 +00:00
|
|
|
print('==== Linear Search ====\n')
|
|
|
|
main()
|