Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 45 additions & 29 deletions Linear_Search/Linear_Search.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
# Function for Linear Search
def linearSearch(list, desired):
for i in range(0, len(list)):
# return positon if found
if(list[i] == desired):
return i

return -1

num = int(input())
list = []
for i in range(0, num):
list.append(int(input()))
desired = int(input())
if(linearSearch(list,desired) != -1):
print("Found")
else:
print("Number not found")


'''
Input :
num = 5
arr = [1,4,5,6,3]
desired = 3

Output :
Number not found
'''

#Putting it all in try except

try:
num = int(input("Enter the number of integers you want in the list "))
list = []
#taking the number of elements wanted in the list



for i in range(0, num):
print("Enter value of element ",i+1)
list.append(int(input()))

#taking the elements



Ele=int(input("Enter the element you whose position you want to find "))

#Searching the element

if Ele not in list:
print("The element ",Ele," is not in the list ")
else:
desired = list.index(Ele)
print("The position of ",Ele, "is ",i+1)

except ValueError:
print("Enter integer only ")


"""
Output

Enter the number of integers you want in the list 3
Enter value of element 1
1
Enter value of element 2
2
Enter value of element 3
3
Enter the element you whose position you want to find 4
The element 4 is not in the list
"""