赞
踩
def sequential_search(arr, target):
"""
顺序查找函数Args:
arr: 待查找的数组
target: 目标元素Returns:
目标元素在数组中的位置,如果未找到则返回 -1
"""
for i in range(len(arr)):
if arr[i] == target:
return i
return -1#测试用例
# 不存在返回-1
def test_sequential_search_not_exist():
assert sequential_search([1, 3, 4, 5, 7, 8], "a") == -1
# 存在返回对应数据的索引
def test_sequential_search():
assert sequential_search([1, 3, 4, 5, 7, 8], 1) == 0
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。