This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from iamneha/work
Add Linear search and Queue
- Loading branch information
Showing
6 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
def linear_search(List, item): | ||
""" | ||
Find the element in list using linear search | ||
>>> from pydsa import linear_search | ||
>>> List = [3, 4, 6, 8, 12, 15, 26] | ||
>>> item = 6 | ||
>>> linear_search(List, item) | ||
2 | ||
""" | ||
index = len(List) | ||
for i in range(0, index): | ||
if List[i] == item: | ||
return i | ||
return -1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class queue(object): | ||
""" | ||
>>> from pydsa import queue | ||
>>> q = queue() | ||
>>> q.enqueue(5) | ||
>>> q.enqueue(8) | ||
>>> q.enqueue(19) | ||
>>> q.dequeue() | ||
5 | ||
""" | ||
def __init__(self): | ||
self.List = [] | ||
|
||
def isEmpty(self): | ||
return self.List == [] | ||
|
||
def enqueue(self, item): | ||
""" | ||
Insert element in queue. | ||
""" | ||
self.List.append(item) | ||
|
||
def dequeue(self): | ||
""" | ||
Remove element from front of the Queue. | ||
""" | ||
return self.List.pop(0) | ||
|
||
def size(self): | ||
""" | ||
Return size of Queue. | ||
""" | ||
return len(self.List) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from pydsa.linear_search import linear_search | ||
|
||
|
||
def test_linear_search(): | ||
List = [4, 17, 9, 12, 5, 16] | ||
item = 12 | ||
assert linear_search(List, item) == 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from pydsa.queue import queue | ||
|
||
|
||
def test_queue(): | ||
q = queue() | ||
q.enqueue(5) | ||
q.enqueue(9) | ||
q.enqueue(12) | ||
q.enqueue(20) | ||
assert q.dequeue() == 5 | ||
assert q.dequeue() == 9 | ||
assert q.size() == 2 |