Objective

Create a function to search for a given value in a list.

Difficulty

Very easy

Description

Suppose we have a given list and we want to check if it has given item in it.

This can easily be done by sequentially iterating over the list and comparing each subsequent item with the item to look for.

An algorithm that operates in this manner in order to search for a value in a list is generally known as a sequential search, or linear search algorithm.

The term 'linear' comes from the fact that the running time of this algorithm can be expressed as a linear function on the length of the list.

In this exercise, you have to create a function linear_search() that implements the linear search algorithm in order to search for an item in a given list.

Its general form should be as follows:

def linear_search(arr, target):
   pass

Two arguments should be provided to the function — the first is the list where to search for the target item, while the second is the item itself.

The function should return True is the target exists in the list, or else False.

Note that the function should search exactly for the target in the list.

Shown below are a couple of examples of the usage of the function:

linear_search([1, 2, 3], 2)
True
linear_search([1, 2, 3], '2')
False
linear_search(['2', '4', '6'], '2')
True
linear_search(['2, 6', '1, 4'], '2')
False
linear_search([False, False, False, True], False)
True