Skip to content

Commit

Permalink
Added type hints to the Queue
Browse files Browse the repository at this point in the history
  • Loading branch information
nbro committed Jan 24, 2017
1 parent e2e75f6 commit 3bd7f42
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion OBJECTIVES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
- Approximation algorithms
- Heuristics

or even to more general scientific areas
or even to more general scientific areas

- Artificial intelligence
- Numerical computing
Expand Down
4 changes: 2 additions & 2 deletions ands/algorithms/greedy/fractional_knapsack.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def ask_objects():
objects = []
print("Welcome to the Fractional Knapsack problem!\n\n" +
"You will tell me the objects that you have,\n" +
"their cost and their weight.\n\n" +
"their path_cost and their weight.\n\n" +
"You should also tell me after that\n"
"how much weight you can carry with you.\n\n" +
"I will tell you then which items or\n" +
Expand All @@ -38,7 +38,7 @@ def ask_objects():
print("-" * 40, end="\n\n")

for obj in objects:
# adding as forth property of each object its cost/weight ratio
# adding as forth property of each object its path_cost/weight ratio
obj.append(obj[1] / obj[2])

objects.sort(key=operator.itemgetter(3), reverse=True)
Expand Down
8 changes: 4 additions & 4 deletions ands/ds/Queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,22 @@ def __init__(self, ls=None):
ls = []
self.q = deque(ls)

def enqueue(self, elem) -> None:
def enqueue(self, elem: object) -> None:
"""Adds `elem` to the end of this queue.
Assumes `elem` is not None."""
assert elem is not None
self.q.append(elem)

def dequeue(self):
def dequeue(self) -> object:
"""Returns the first element of this queue."""
return None if self.is_empty() else self.q.popleft()

def is_empty(self):
def is_empty(self) -> bool:
"""Returns `True` if this queue is empty, `False` otherwise."""
return self.size() == 0

def size(self):
def size(self) -> int:
"""Returns the size of this queue."""
return len(self.q)

Expand Down
9 changes: 3 additions & 6 deletions tests/ds/test_Queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,12 @@ def test_dequeue_one(self):
self.assertTrue(q.is_empty())

def test_dequeue_many(self):

r = randint(2, 100)
ls = [randint(-100, 100) for _ in range(r)]

ls = [2, 3, 5, 7, 11, 13]
q = Queue(ls)

for _ in range(r):
for i in range(len(ls)):
elem = q.dequeue()
self.assertIsNotNone(elem)
self.assertEqual(elem, ls[i])

self.assertTrue(q.is_empty())

Expand Down

0 comments on commit 3bd7f42

Please sign in to comment.