Рубрики
Без рубрики

Линейный поиск

# linear_search.py # Эта программа использует линейный поиск массива размера n для # value x # … Tagged с помощью Python, алгоритмов, программирования.

# 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!

Оригинал: “https://dev.to/sagordondev/linear-search-5445”