Skip to content

Commit

Permalink
fixed queues
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLeoni committed Dec 4, 2019
1 parent d0546d3 commit 88028b5
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 227 deletions.
50 changes: 25 additions & 25 deletions exams/2019-11-07/exam-2019-11-07-solution.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -445,30 +445,30 @@
" #/jupman-raise\n",
"\n",
"\n",
"assert is_leap(4)\n",
"assert is_leap(104)\n",
"assert is_leap(204)\n",
"assert is_leap(400)\n",
"assert is_leap(1600)\n",
"assert is_leap(2000)\n",
"assert is_leap(2400)\n",
"assert is_leap(2000)\n",
"assert is_leap(2004)\n",
"assert is_leap(2008)\n",
"assert is_leap(2012)\n",
"\n",
"assert not is_leap(1)\n",
"assert not is_leap(5)\n",
"assert not is_leap(100)\n",
"assert not is_leap(200)\n",
"assert not is_leap(1700)\n",
"assert not is_leap(1800)\n",
"assert not is_leap(1900)\n",
"assert not is_leap(2100)\n",
"assert not is_leap(2200)\n",
"assert not is_leap(2300)\n",
"assert not is_leap(2500)\n",
"assert not is_leap(2600)"
"assert is_leap(4) == True\n",
"assert is_leap(104) == True\n",
"assert is_leap(204) == True\n",
"assert is_leap(400) == True\n",
"assert is_leap(1600) == True\n",
"assert is_leap(2000) == True\n",
"assert is_leap(2400) == True\n",
"assert is_leap(2000) == True\n",
"assert is_leap(2004) == True\n",
"assert is_leap(2008) == True\n",
"assert is_leap(2012) == True\n",
"\n",
"assert is_leap(1) == False\n",
"assert is_leap(5) == False\n",
"assert is_leap(100) == False\n",
"assert is_leap(200) == False\n",
"assert is_leap(1700) == False\n",
"assert is_leap(1800) == False\n",
"assert is_leap(1900) == False\n",
"assert is_leap(2100) == False\n",
"assert is_leap(2200) == False\n",
"assert is_leap(2300) == False\n",
"assert is_leap(2500) == False\n",
"assert is_leap(2600) == False"
]
},
{
Expand Down
28 changes: 17 additions & 11 deletions exercises/queues/italian_queue_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ class ItalianQueue:
- tail()
- tail_group()
Each element is assigned a group; during enqueing, queue is scanned from head to tail
to find if there is another element with a matching group.
- If there is, element to be enqueued is inserted after the last element in the same
group sequence (that is, to the right of the group)
Each element is assigned a group; during enqueing, queue is
scanned from head to tail to find if there is another element
with a matching group.
- If there is, element to be enqueued is inserted after the
last element in the same group sequence (that is, to the
right of the group)
- otherwise the element is inserted at the end of the queue
"""

Expand All @@ -53,8 +55,8 @@ def __init__(self):
#/jupman-raise

def __str__(self):
""" For potentially complex data structures like this one, having a __str__ method is essential to
quickly inspect the data by printing it.
""" For potentially complex data structures like this one, having
a __str__ method is essential to quickly inspect the data by printing it.
"""
current = self._head
strings = []
Expand Down Expand Up @@ -99,7 +101,8 @@ def top(self):
#/jupman-raise

def top_group(self):
""" Return the group of the element at the head of the queue, without removing it.
""" Return the group of the element at the head of the queue,
without removing it.
- If the queue is empty, raises LookupError.
- Complexity: O(1)
Expand All @@ -112,7 +115,7 @@ def top_group(self):
#/jupman-raise

def tail(self):
""" Return the element at the tail of the queue (without removing it).
""" Return the element at the tail of the queue (without removing it)
- If the queue is empty, raises LookupError.
- Complexity: O(1)
Expand All @@ -139,10 +142,13 @@ def tail_group(self):
#/jupman-raise

def enqueue(self, v, g):
""" Enqueues provided element v having group g, with the following criteria:
""" Enqueues provided element v having group g, with the following
criteria:
Queue is scanned from head to find if there is another element with a matching group:
- if there is, v is inserted after the last element in the same group sequence (so to the right of the group)
Queue is scanned from head to find if there is another element
with a matching group:
- if there is, v is inserted after the last element in the
same group sequence (so to the right of the group)
- otherwise v is inserted at the end of the queue
- Complexity: O(n)
Expand Down

0 comments on commit 88028b5

Please sign in to comment.