28 Feb 2023

Posted on:

27 Feb 2023

0

# Resolved:a different approach worked for me

I did a different approach, and I think it worked. However, I doubt whether it is linear or not. However, I think, based on the iteration, it can be considered linear.

found=[]
def linear_search(l,m):
for i in range(len(l)):
if l[i] !=m:
found.append(m)
else:
print(f'your requested item is the {i+1}th item of the list')
return m,i

Instructor
Posted on:

28 Feb 2023

0

Hey Arsalan,

Thank you for reaching out and sharing your approach.

The solution you've provided indeed solve the problem in a linear fashion--it goes through the list and checks whether the number we're on is the one we're searching for.

Here are a couple of question to think about that could make your code more efficient. I'll make use of the following list: `[6,5,8,2,3,45,87,24,70]`

1. If the number I'm searching for is 5 and I've found it's on position `i=1`, do I need to go through the rest of the list or I can terminate my search?

2. Do I need the `found` list? Do I need to append `m` to a list?

3. Do I need to return the variables `m` and `i`?

Kind regards,

365 Hristina