Skip to content

Commit c151f72

Browse files
authored
Merge pull request #158 from sel3ne/master
fixed LinearSearch name and impleneted tests
2 parents b346fce + a1f4740 commit c151f72

File tree

3 files changed

+69
-31
lines changed

3 files changed

+69
-31
lines changed

classical_algorithms/python/Linear Search .py

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
def linear(arr,n,x):
2+
if arr == []:
3+
return -1
4+
5+
6+
for i in range(n):
7+
if(arr[i] == x):
8+
return i
9+
return -1
10+
11+
# not executing unnless called
12+
if __name__=='__main__':
13+
n = int(input("Enter the total number of elements\n"))
14+
arr =list(map(int,input().split()))
15+
x = int(input("Enter which element you want to find\n"))
16+
result = linear(arr,n,x)
17+
if (result == -1):
18+
print("Element not found")
19+
else:
20+
print("Element found at" ,result+1,"pos")
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import unittest
2+
from classical_algorithms.python.LinearSearch import linear
3+
4+
# linear(arr,n,x):
5+
6+
class TestLinaerSearch(unittest.TestCase):
7+
def test_linear_search(self):
8+
9+
print('None Array')
10+
self.assertRaises(TypeError, linear(None,0,12))
11+
12+
print('None Search')
13+
self.assertRaises(TypeError, linear([12,22],2,None))
14+
15+
print('Empty Array')
16+
self.assertEqual(linear([],2,12), -1)
17+
18+
print('Wrong size')
19+
self.assertRaises(TypeError, linear([1,2,3], 5, 2))
20+
21+
print('One Element')
22+
self.assertEqual(linear([12], 1, 12), 0)
23+
24+
print('Multiple Elements')
25+
self.assertEqual(linear([-100, -12, -1, 0, 1,2,3,4, 100], 9, 3), 6)
26+
27+
28+
29+
30+
31+
if __name__=='__main__':
32+
unittest.main()

0 commit comments

Comments
 (0)