Skip to content

Commit 360cc1a

Browse files
Merge pull request #17 from seancyw/issue_7
Added linear search for element in array
2 parents ece9591 + 56bce77 commit 360cc1a

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Code/ArrayLinearSearch.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#Linear search function in python
2+
#This function will look for an item in a list
3+
4+
def linearSearch(item, listItem):
5+
found = False
6+
position = 0
7+
while position < len(listItem) and not found:
8+
if listItem[position] == item:
9+
found = True
10+
position = position + 1
11+
return found
12+
13+
if __name__ == "__main__":
14+
shopping = ["apples", "bananas", "chocolate", "pasta"]
15+
item = raw_input("What item do you want to find? ")
16+
isFound = linearSearch(item, shopping)
17+
if isFound:
18+
print("Your item is in the list!")
19+
else:
20+
print("Your item is not in the list!")
21+

0 commit comments

Comments
 (0)