Index of Target Number in List

The "Index of Target Number in List" code problem asks the reader to write a function that takes in a list of integers and a target number, and returns the index of the target number if it is present in the list, or -1 if it is not present.

Problem

Given a list of integers and a target number, write a function that returns the index of the target number if it is present in the list, or -1 if it is not present.
Example input: [1, 2, 3, 4, 5], 3
Example output: 2

Solution

This solution is in Python. Our A.I. can create solutions in multiple languages.
by kayakane
def find_target(list, target):
    for i in range(len(list)):
        if list[i] == target:
            return i
    return -1

print(find_target([1, 2, 3, 4, 5], 3))
print(find_target([1, 2, 3, 4, 5], 6))



The solution is optimal because it uses a linear search algorithm.


A.I. Evaluation of the Solution

The candidate's solution is correct and uses a linear search algorithm, which is the most efficient way to search for an element in a list. The candidate's solution is complete and solves the problem.

Evaluated at: 2022-11-18 02:15:51