dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
64 lines
1007 B
Markdown
64 lines
1007 B
Markdown
---
|
|
id: 698dca057932b3cc4b19c926
|
|
title: Linear Search in Code
|
|
challengeType: 11
|
|
videoId: bKkgjdPkL3A
|
|
dashedName: linear-search-in-code
|
|
---
|
|
|
|
# --description--
|
|
|
|
In this video, you will code out the linear search algorithm using Python.
|
|
|
|
# --questions--
|
|
|
|
## --text--
|
|
|
|
Which of the following is the correct way to write a linear search function?
|
|
|
|
## --answers--
|
|
|
|
```python
|
|
def linear_search(list, target):
|
|
for i in range(len(list) - 1):
|
|
if list[i] == target:
|
|
return i
|
|
return -1
|
|
```
|
|
|
|
---
|
|
|
|
```python
|
|
def linear_search(list, target):
|
|
for i in range(len(list)):
|
|
if i == target:
|
|
return i
|
|
return -1
|
|
|
|
```
|
|
|
|
---
|
|
|
|
```python
|
|
def linear_search(list, target):
|
|
for i in range(len(list)):
|
|
if list[i] == target:
|
|
return i
|
|
else:
|
|
return -1
|
|
```
|
|
|
|
---
|
|
|
|
```python
|
|
def linear_search(list, target):
|
|
for i in range(len(list)):
|
|
if list[i] == target:
|
|
return i
|
|
return None
|
|
```
|
|
|
|
## --video-solution--
|
|
|
|
4
|