Skip to content

Commit

Permalink
linked lists improvemenets
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLeoni committed Nov 21, 2019
1 parent b161a47 commit 9c9adad
Show file tree
Hide file tree
Showing 9 changed files with 336 additions and 90 deletions.
296 changes: 257 additions & 39 deletions exercises/linked-lists/linked-lists.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions exercises/linked-lists/linked_list_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def search(self,item):
def remove(self, item):
""" Removes first occurrence of item from the list
If item is not found, raises an Exception.
If item is not found, raises an LookupError.
"""
#jupman-raise
current = self._head
Expand All @@ -111,7 +111,7 @@ def remove(self, item):
prev = current
current = current.get_next()

raise Exception("Tried to remove a non existing item! Item was: " + str(item))
raise LookupError("Tried to remove a non existing item! Item was: " + str(item))
#/jupman-raise

def append(self, e):
Expand Down
8 changes: 4 additions & 4 deletions exercises/linked-lists/linked_list_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ class RemoveTest(LinkedListTest):

def test_01_remove_empty_list(self):
ul = LinkedList()
with self.assertRaises(Exception):
with self.assertRaises(LookupError):
ul.remove('a')


def test_02_remove_one_element(self):
ul = LinkedList()
ul.add('a')
with self.assertRaises(Exception):
with self.assertRaises(LookupError):
ul.remove('b')
ul.remove('a')
self.assertEqual(to_py(ul), [])
Expand All @@ -98,7 +98,7 @@ def test_03_remove_two_element(self):
ul = LinkedList()
ul.add('b')
ul.add('a')
with self.assertRaises(Exception):
with self.assertRaises(LookupError):
ul.remove('c')
ul.remove('b')
self.assertEqual(to_py(ul), ['a'])
Expand All @@ -110,7 +110,7 @@ def test_04_remove_first_occurrence(self):
ul = LinkedList()
ul.add('b')
ul.add('b')
with self.assertRaises(Exception):
with self.assertRaises(LookupError):
ul.remove('c')
ul.remove('b')
self.assertEqual(to_py(ul), ['b'])
Expand Down
10 changes: 5 additions & 5 deletions exercises/oop/oop.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"<__main__.ComplexNumber object at 0x7f162c26ee48>\n"
"<__main__.ComplexNumber object at 0x7fa4ac2c0c88>\n"
]
}
],
Expand Down Expand Up @@ -794,7 +794,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"<__main__.ComplexNumber object at 0x7f162c26ee48>\n"
"<__main__.ComplexNumber object at 0x7fa4ac2c0c88>\n"
]
}
],
Expand Down Expand Up @@ -1083,7 +1083,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"<__main__.ComplexNumber object at 0x7f162c275cf8>\n"
"<__main__.ComplexNumber object at 0x7fa4ac3307f0>\n"
]
}
],
Expand Down Expand Up @@ -1158,7 +1158,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"<__main__.ComplexNumber object at 0x7f162c2df5c0>\n"
"<__main__.ComplexNumber object at 0x7fa4ac2d3d68>\n"
]
}
],
Expand Down Expand Up @@ -1821,7 +1821,7 @@
"text": [
".................\n",
"----------------------------------------------------------------------\n",
"Ran 17 tests in 0.013s\n",
"Ran 17 tests in 0.014s\n",
"\n",
"OK\n",
".......\n",
Expand Down
6 changes: 5 additions & 1 deletion exercises/sorting/insertion_sort_solution.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

def insertion_sort(A):
""" Sorts in-place list A with insertion sort. """
""" Sorts in-place list A with insertion sort
Remember insertion sort has complexity O(n^2) where n is the
size of the list.
"""
#jupman-raise
for i in range(1, len(A)):
temp = A[i]
Expand Down
25 changes: 12 additions & 13 deletions exercises/sorting/selection_sort_solution.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
def swap(A, i, j):
""" MODIFIES the array A by swapping the elements at position i and j
"""
In the array A, swaps the elements at position i and j.
"""

#jupman-raise

temp = A[i]
A[i] = A[j]
A[j] = temp

#/jupman-raise

# if you are a Python ninja, you might prefer this very concise version:
# A[i], A[j] = A[j], A[i]

#/jupman-raise



def argmin(A, i):
"""
RETURN the *index* of the element in list A which is lesser or equal than all other elements in A
that start from index i included
""" RETURN the *index* of the element in list A which is lesser than or equal
to all other elements in A that start from index i included
- MUST execute in O(n) where n is the length of A
"""


#jupman-raise

Expand All @@ -37,12 +35,13 @@ def argmin(A, i):


def selection_sort(A):
"""
Sorts the list A in-place in O(n^2) time this ways:
1. Looks at minimal element in the array [i:n], and swaps it with first element.
""" Sorts the list A in-place in O(n^2) time this ways:
1. Looks at minimal element in the array [i:n],
and swaps it with first element.
2. Repeats step 1, but considering the subarray [i+1:n]
Remember selection sort has complexity O(n^2) where n is the size of the list.
Remember selection sort has complexity O(n^2) where n is the
size of the list.
"""
#jupman-raise

Expand Down
53 changes: 31 additions & 22 deletions exercises/sorting/sorting.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@
"outputs": [],
"source": [
"def swap(A, i, j):\n",
" \"\"\"\n",
" In the array A, swaps the elements at position i and j.\n",
" \"\"\" MODIFIES the array A by swapping the elements at position i and j\n",
" \"\"\"\n",
" raise Exception(\"TODO implement me!\")"
]
Expand Down Expand Up @@ -308,11 +307,10 @@
"outputs": [],
"source": [
"def argmin(A, i):\n",
" \"\"\"\n",
" Return the *index* of the element in list A which is lesser than or equal to all other elements in A \n",
" that start from index i included\n",
" \"\"\" RETURN the *index* of the element in list A which is lesser than or equal\n",
" to all other elements in A that start from index i included\n",
" \n",
" - MUST execute in O(n) where n is the length of A\n",
" - MUST execute in O(n) where n is the length of A\n",
" \"\"\"\n",
" raise Exception(\"TODO implement me!\")\n"
]
Expand Down Expand Up @@ -390,14 +388,15 @@
"source": [
"\n",
"def selection_sort(A):\n",
" \"\"\"\n",
" Sorts the list A in-place in O(n^2) time this way:\n",
" 0: sets i = 0\n",
" 1. Looks at minimal element in the array [i:n], and swaps it with first element.\n",
" \"\"\" Sorts the list A in-place in O(n^2) time this ways:\n",
" 1. Looks at minimal element in the array [i:n],\n",
" and swaps it with first element.\n",
" 2. Repeats step 1, but considering the subarray [i+1:n]\n",
" \n",
" Remember selection sort has complexity O(n^2) where n is the size of the list.\n",
" Remember selection sort has complexity O(n^2) where n is the \n",
" size of the list.\n",
" \"\"\"\n",
"\n",
" raise Exception(\"TODO implement me!\")\n"
]
},
Expand Down Expand Up @@ -452,7 +451,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Start editing the file `insertion_sort_exercise.py` and implement `insertion_sort` without looking at theory slides. From the animation, you can see these things are going on: \n",
"From the animation, you can see these things are going on: \n",
"\n",
"1. The red square selects one number starting from the leftomost (question: does it actually need to be the leftmost ? Can we save one iteration?). Let's say it starts at position `i`.\n",
"2. _While_ the number in the red square is lesser then the previous one, it is shifted back one position at a time\n",
Expand All @@ -463,8 +462,7 @@
"- how many cycles do we need ? One, Two, Three?\n",
"- Are they nested? \n",
"- Is there one cycle with a fixed number of iterations ? Is there one with an unknown number of iterations? \n",
"- What is the worst-case complexity of the algorithm?\n",
"\n"
"- What is the worst-case complexity of the algorithm?\n"
]
},
{
Expand Down Expand Up @@ -513,9 +511,20 @@
"A[j] = red\n",
"```\n",
"\n",
"Now try to think about the stop condition. "
"Start editing the file `insertion_sort_exercise.py` and implement `insertion_sort` without looking at theory slides. \n",
"\n",
"```python\n",
"def insertion_sort(A):\n",
" \"\"\" Sorts in-place list A with insertion sort \n",
" \"\"\"\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -621,7 +630,7 @@
{
"data": {
"text/plain": [
"<list_reverseiterator at 0x7f39b0ba43c8>"
"<list_reverseiterator at 0x7f1a749ce630>"
]
},
"execution_count": 10,
Expand Down Expand Up @@ -1753,32 +1762,32 @@
"text": [
"...............\n",
"----------------------------------------------------------------------\n",
"Ran 15 tests in 0.012s\n",
"Ran 15 tests in 0.010s\n",
"\n",
"OK\n",
"......\n",
"----------------------------------------------------------------------\n",
"Ran 6 tests in 0.005s\n",
"Ran 6 tests in 0.004s\n",
"\n",
"OK\n",
"..............\n",
"----------------------------------------------------------------------\n",
"Ran 14 tests in 0.010s\n",
"Ran 14 tests in 0.007s\n",
"\n",
"OK\n",
".................\n",
"----------------------------------------------------------------------\n",
"Ran 17 tests in 0.009s\n",
"Ran 17 tests in 0.010s\n",
"\n",
"OK\n",
".................\n",
"----------------------------------------------------------------------\n",
"Ran 17 tests in 0.012s\n",
"Ran 17 tests in 0.007s\n",
"\n",
"OK\n",
"...............................\n",
"----------------------------------------------------------------------\n",
"Ran 31 tests in 0.019s\n",
"Ran 31 tests in 0.015s\n",
"\n",
"OK\n"
]
Expand Down
11 changes: 8 additions & 3 deletions index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -541,9 +541,10 @@
"\n",
"## Labs timetable\n",
"\n",
"For the regular labs timetable please see Andrea Passerini's [course site](http://disi.unitn.it/~passerini/teaching/2019-2020/sci-pro/)\n",
"For the regular labs timetable please see:\n",
"\n",
"\n"
"* Part A: [Andrea Passerini's course site](http://disi.unitn.it/~passerini/teaching/2019-2020/sci-pro/)\n",
"* Part B: [Luca Bianco's course site](https://sciproalgo2019.readthedocs.io/en/latest/)\n"
]
},
{
Expand Down Expand Up @@ -602,7 +603,11 @@
"metadata": {},
"source": [
"### Schedule\n",
"See exams schedule on [Andrea Passerini's site](http://disi.unitn.it/~passerini/teaching/2019-2020/sci-pro/)"
"\n",
"See exams schedule on\n",
"\n",
"* Part A: [Andrea Passerini's course site](http://disi.unitn.it/~passerini/teaching/2019-2020/sci-pro/)\n",
"* Part B: [Luca Bianco's course site](https://sciproalgo2019.readthedocs.io/en/latest/)\n"
]
},
{
Expand Down
13 changes: 12 additions & 1 deletion slides.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,18 @@
"\n",
"Tuesday 19 November\n",
"\n",
"* [Sorting](exercises/sorting/sorting.ipynb) \n"
"* [Sorting 1](exercises/sorting/sorting.ipynb) (selection sort, insertion sort)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Lab B.4\n",
"\n",
"Thursday 21 November\n",
"\n",
"* [Sorting 2](exercises/sorting/sorting.ipynb) (merge sort, quicksort, SwapArray)"
]
},
{
Expand Down

0 comments on commit 9c9adad

Please sign in to comment.