선형 검색

# linear_search.py
#   This program uses a linear search of an array of size n for
#   value x 
# by: Scott Gordon

things = ["bike", "house", "cat", "computer", "hammer"]

def linear_search(array, n, x):
    answer = "Not found!"
    for i in range(1, n):
        if array[i] == x:
            answer = i
    return answer

print(linear_search(things, 5, "cat")) # prints the index of "cat" which is 2
print(linear_search(things, 5, "hammer")) # prints the index of "hammer" which is 4
print(linear_search(things, 5, "pasta")) # prints Not found!

좋은 웹페이지 즐겨찾기