Skip to content

Commit

Permalink
merged exams into tutorials
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLeoni committed Nov 8, 2019
1 parent f0464ee commit b916a8d
Show file tree
Hide file tree
Showing 36 changed files with 7,391 additions and 28,282 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ private/*
/overlay/_static/*-exam.zip
/overlay/_static/project-template.zip

exercises/pandas/prepare/tles/


##### PYTHON
*.pyc
Expand Down
13 changes: 3 additions & 10 deletions exams/2018-11-16/exam-2018-11-16-solution.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Implementation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. union\n",
"### A1 union\n",
"\n",
"✪✪ When we talk about the _union_ of two graphs, we intend the graph having union of verteces of both graphs and having as edges the union of edges of both graphs. In this exercise, we have two graphs as list of lists with boolean edges. To simplify we suppose they have the same vertices but possibly different edges, and we want to calculate the union as a new graph.\n",
"\n",
Expand Down Expand Up @@ -530,7 +523,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. surjective\n",
"## A2 surjective\n",
"\n",
"✪✪ If we consider a graph as a nxn binary relation where the domain is the same as the codomain, such relation is called _surjective_ if every node is reached by _at least_ one edge. \n",
"\n",
Expand Down Expand Up @@ -726,7 +719,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. ediff\n",
"### A3 ediff\n",
"\n",
"✪✪✪ The _edge difference_ of two graphs `ediff(da,db)` is a graph with the edges of the first except the edges of the second. For simplicity, here we consider only graphs having the same verteces but possibly different edges. This time we will try operate on graphs represented as dictionaries of adjacency lists. "
]
Expand Down
14,086 changes: 93 additions & 13,993 deletions exams/2019-01-10/exam-2019-01-10.ipynb

Large diffs are not rendered by default.

38 changes: 22 additions & 16 deletions exams/2019-01-10/exits_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
from queue import Queue
from collections import deque

DEBUG = True
def debug(msg):
if DEBUG:
print("DEBUG: ", str(msg).replace('\n', '\n' + (' '*8)))


#PrettyPrint(indent=4)
pp = pprint.PrettyPrinter(indent=4).pprint
pformat = pprint.PrettyPrinter(indent=4).pformat
Expand Down Expand Up @@ -152,18 +158,18 @@ def bfs(self, source):

while len(Q)>0:
u = Q.popleft()
print("Removed from queue: %s" % u)
debug("Removed from queue: %s" % u)
# Visit node u
for v in self._edges[u]:
print(" Found neighbor: %s" % v)
debug(" Found neighbor: %s" % v)
# Visit edge (u,v)
if not visited[v]:
print(" not yet visited, enqueueing ..")
debug(" not yet visited, enqueueing ..")
visited[v] = True
Q.append(v)
else:
print(" already visited")
print(" Queue is: %s " % list(Q))
debug(" already visited")
debug(" Queue is: %s " % list(Q))

def dfs(self, source):
""" Example of a simple recursive depth first search on the graph,
Expand All @@ -182,21 +188,21 @@ def dfs(self, source):
for v in self.verteces():
visited[v] = False

print("Stack is: %s " % S)
debug("Stack is: %s " % S)
while not len(S) == 0:
u = S.pop()
print("popping from stack: %s" % u)
debug("popping from stack: %s" % u)
if not visited[u]:
print(" not yet visited")
debug(" not yet visited")
# visit node u (pre-order)
visited[u] = True
for v in self.adj(u):
print(" Scheduling for visit: %s" % v)
debug(" Scheduling for visit: %s" % v)
# visit edge (u,v)
S.append(v)
print("Stack is : %s " % S)
debug("Stack is : %s " % S)
else:
print(" already visited!")
debug(" already visited!")


def has_edge(self, source, target):
Expand Down Expand Up @@ -253,20 +259,20 @@ def cp(self, source):

while len(Q)>0:
u = Q.popleft()
print("Removed from queue: %s" % u)
debug("Removed from queue: %s" % u)
# Visit node u
for v in self._edges[u]:
print(" Found neighbor: %s" % v)
debug(" Found neighbor: %s" % v)
# Visit edge (u,v)
if not visited[v]:
print(" not yet visited, enqueueing ..")
debug(" not yet visited, enqueueing ..")
visited[v] = True
ret[v] = u
Q.append(v)

else:
print(" already visited")
print(" Queue is: %s " % list(Q))
debug(" already visited")
debug(" Queue is: %s " % list(Q))

return ret
#/jupman-raise
Expand Down
23 changes: 14 additions & 9 deletions exams/2019-01-10/tasks_solution.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@

DEBUG = True

def debug(msg):
if DEBUG:
print("DEBUG: ", str(msg).replace('\n', '\n' + (' '*8)))


class Stack:
""" A simple Stack supporting push, pop, and is_empty operations
"""

def __init__(self):
""" Creates a Stack.TODO
""" Creates a Stack.
"""
self._elements = []
Expand All @@ -28,8 +34,7 @@ def pop(self):
return self._elements.pop()

def push(self, item):
""" Inserts a task in the stack
TODO
""" Inserts a task in the stack
"""
self._elements.append(item)

Expand All @@ -50,16 +55,16 @@ def do(task, subtasks):
s = Stack()
ret = []
s.push(task)
print("Debug: %s" % s)
debug(s)
while not s.is_empty():
t1 = s.pop()
ret.append(t1)
print("Debug: Doing task %s, scheduling subtasks %s" % (t1, subtasks[t1]))
debug("Doing task %s, scheduling subtasks %s" % (t1, subtasks[t1]))
if len(subtasks[t1]) <= 0:
print(" Nothing else to do!")
debug(" Nothing else to do!")
for t2 in reversed(subtasks[t1]):
s.push(t2)
print(" %s" % s)
debug(" %s" % s)
return ret
#/jupman-raise

Expand All @@ -78,15 +83,15 @@ def do_level(task, subtasks):
s = Stack()
ret = []
s.push((task, 0))
print("Debug: \t\t\t\t\t\t%s" % s)
debug("\t\t\t\t\t\t%s" % s)
while not s.is_empty():
t1, level = s.pop()

ret.append((t1,level))
for t2 in reversed(subtasks[t1]):
s.push((t2, level + 1))

print("Debug: I'm doing \t%s%s\t\tlevel=%s %s" % (" " * level, t1, level, s))
debug("I'm doing \t%s%s\t\tlevel=%s %s" % (" " * level, t1, level, s))
return ret
#/jupman-raise

61 changes: 31 additions & 30 deletions exams/2019-01-10/tasks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,16 @@ def test_05_abcd(self):

def test_06_abcdfeg(self):

subtasks = {
'a':['b','g'],
'b':['c','d','e'],
'c':[],
'd':['f'],
'e':[],
'f':[],
'g':[]
}

self.assertEqual(do('a', subtasks), ['a', 'b', 'c', 'd', 'f', 'e', 'g'])
subtasks = subtasks = {
'a':['b','g'],
'b':['c','d','e'],
'c':['f'],
'd':['g'],
'e':[],
'f':[],
'g':[]
}
self.assertEqual(do('a', subtasks), ['a', 'b', 'c', 'f', 'd', 'g', 'e', 'g'])

class DoLevelTest(unittest.TestCase):

Expand Down Expand Up @@ -169,25 +168,7 @@ def test_05_abcd(self):
[('a', 0),('b',1),('c',2),('d',1)])



def test_06_abcdfeg(self):

subtasks = {
'a':['b','g'],
'b':['c','d','e'],
'c':[],
'd':['f'],
'e':[],
'f':[],
'g':[]
}

self.assertEqual(do_level('a', subtasks),
[('a', 0), ('b', 1), ('c', 2), ('d', 2), ('f', 3), ('e', 2), ('g', 1)])



def test_07_abcde(self):
def test_06_abcde(self):
""" Remember a level always depend on the *parent* task level !
level 0 1 2
Expand All @@ -207,5 +188,25 @@ def test_07_abcde(self):
self.assertEqual(do_level('a', subtasks),
[('a',0),('b',1),('d',2),('c',1), ('e',2)])



def test_07_abcdfeg(self):

subtasks = {
'a':['b','g'],
'b':['c','d','e'],
'c':['f'],
'd':['g'],
'e':[],
'f':[],
'g':[]
}

self.assertEqual(do_level('a', subtasks),
[('a', 0), ('b', 1), ('c', 2), ('f', 3), ('d', 2), ('g', 3), ('e', 2), ('g', 1)])






14 changes: 7 additions & 7 deletions exams/2019-01-23/exam-2019-01-23-solution.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### A.1 table_to_adj \n",
"## A.1 table_to_adj \n",
"\n",
"Suppose you have a table expressed as a list of lists with headers like this:\n"
]
Expand Down Expand Up @@ -373,7 +373,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### A.2 bus stops"
"## A.2 bus stops"
]
},
{
Expand Down Expand Up @@ -727,7 +727,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### B.1 Theory\n",
"## B.1 Theory\n",
"\n",
"Let `L` a list of size `n`, and `i` and `j` two indeces. Return the computational complexity of function `fun()` with respect to n.\n",
"\n",
Expand Down Expand Up @@ -762,7 +762,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### B.2 Linked List flatv\n",
"## B.2 Linked List flatv\n",
"\n",
"Suppose a `LinkedList` only contains integer numbers, say 3,8,8,7,5,8,6,3,9.\n",
"Implement method `flatv` which scans the list: when it finds the *first* occurence of a node which contains a number which is less then the previous one, and the less than successive one, it inserts after the current one another node with the same data as the current one, and exits.\n",
Expand All @@ -788,7 +788,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### B.3 Generic Tree rightmost\n",
"## B.3 Generic Tree rightmost\n",
"\n",
"\n",
"![](img/generic-tree-labeled.png)"
Expand Down Expand Up @@ -839,12 +839,12 @@
"text": [
"...........\n",
"----------------------------------------------------------------------\n",
"Ran 11 tests in 0.006s\n",
"Ran 11 tests in 0.008s\n",
"\n",
"OK\n",
"..........\n",
"----------------------------------------------------------------------\n",
"Ran 10 tests in 0.008s\n",
"Ran 10 tests in 0.006s\n",
"\n",
"OK\n"
]
Expand Down
171 changes: 86 additions & 85 deletions exams/2019-02-13/exam-2019-02-13-solution.ipynb

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions exams/2019-02-13/queue_solution.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

from collections import deque

DEBUG_ON = True
DEBUG = True

def debug(msg):
if DEBUG_ON:
print("DEBUG: %s" % msg)
if DEBUG:
print("DEBUG: %s" % str(msg))

class Company:

Expand Down

0 comments on commit b916a8d

Please sign in to comment.