Skip to content

Commit

Permalink
midterm b 2019-12-20 solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLeoni committed Dec 20, 2019
1 parent 4db5031 commit 8e139a3
Show file tree
Hide file tree
Showing 9 changed files with 1,838 additions and 52 deletions.
56 changes: 4 additions & 52 deletions exams/2019-11-07/exam-2019-11-07-solution.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"source": [
"# Midterm - Thu 07, Nov 2019 - solutions\n",
"\n",
"**Scientific Programming - Data Science @ University of Trento**\n"
"**Scientific Programming - Data Science @ University of Trento**\n",
"\n",
"## [Download exercises and solution](../../_static/datasciprolab-2019-11-07-exam.zip)"
]
},
{
Expand Down Expand Up @@ -339,57 +341,7 @@
"2 Giovedì 14 novembre 2019\n",
"3 Giovedì 21 novembre 2019\n",
"4 Giovedì 28 novembre 2019\n",
"5 Giovedì 5 dicembre 2019\n",
"6 Giovedì 12 dicembre 2019\n",
"7 Giovedì 19 dicembre 2019\n",
"8 Giovedì 26 dicembre 2019\n",
"9 mercoledì 13 novembre 2019\n",
"10 domenica 10 novembre 2019\n",
"11 Sabato 9 novembre 2019\n",
"12 Sabato 14 dicembre 2019\n",
"13 Sabato 23 novembre 2019\n",
"14 Sabato 21 dicembre 2019\n",
"15 giovedì 7 novembre 2019\n",
"16 sabato 9 novembre 2019\n",
"17 giovedì 14 novembre 2019\n",
"18 sabato 16 novembre 2019\n",
"19 giovedì 21 novembre 2019\n",
"20 sabato 23 novembre 2019\n",
"21 giovedì 28 novembre 2019\n",
"22 sabato 30 novembre 2019\n",
"23 giovedì 5 dicembre 2019\n",
"24 sabato 7 dicembre 2019\n",
"25 giovedì 12 dicembre 2019\n",
"26 sabato 14 dicembre 2019\n",
"27 giovedì 19 dicembre 2019\n",
"28 sabato 21 dicembre 2019\n",
"29 giovedì 26 dicembre 2019\n",
" ... \n",
"223 da giovedì 21 a domenica 24 novembre 2019\n",
"224 da giovedì 9 a domenica 12 gennaio 2020\n",
"225 da giovedì 12 a domenica 15 dicembre 2019\n",
"226 lunedì 9 dicembre 2019\n",
"227 martedì 26 novembre 2019\n",
"228 giovedì 14 novembre 2019\n",
"229 da giovedì 7 a domenica 10 novembre 2019\n",
"230 sabato 16 novembre 2019\n",
"231 venerdì 15 novembre 2019\n",
"232 venerdì 22 novembre 2019\n",
"233 da venerdì 18 ottobre a domenica 3 novembre 2019\n",
"234 domenica 3 novembre 2019\n",
"235 domenica 3 novembre 2019\n",
"236 da sabato 26 ottobre a sabato 9 novembre 2019\n",
"237 lunedì 11 novembre 2019\n",
"238 venerdì pomeriggio 15 e sabato mattina 16 nove...\n",
"239 sabato 4 e domenica 5 gennaio 2020\n",
"240 giovedì 2 Gennaio 2020\n",
"241 domenica 29 dicembre 2019\n",
"242 venerdì 15 novembre\n",
"243 sabato 30 novembre 2019\n",
"244 giovedì 7 novembre 2019\n",
"245 giovedì 7 novembre 2019\n",
"246 lunedì 4 novembre 2019\n",
"247 da sabato 2 a lunedì 4 novembre 2019\n",
"248 sabato 9 novembre 2019\n",
"249 da venerdì 8 a domenica 10 novembre 2019\n",
"250 giovedì 7 novembre 2019\n",
Expand Down Expand Up @@ -429,7 +381,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
Expand Down
139 changes: 139 additions & 0 deletions exams/2019-12-20/bin_tree_solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@

class BinaryTree:
""" A simple binary tree with left and right branches
"""

def __init__(self, data):
self._data = data
self._left = None
self._right = None

def data(self):
return self._data

def left(self):
return self._left

def right(self):
return self._right

def __str__(self):
""" Returns a pretty string of the tree """
def str_branches(node, branches):
""" Returns a string with the tree pretty printed.
branches: a list of characters representing the parent branches. Characters can be either ` ` or '│'
"""
strings = [str(node._data)]

i = 0
if node._left != None or node._right != None:
for current in [node._left, node._right]:
if i == 0:
joint = '├'
else:
joint = '└'

strings.append('\n')
for b in branches:
strings.append(b)
strings.append(joint)
if i == 0:
branches.append('│')
else:
branches.append(' ')

if current != None:
strings.append(str_branches(current, branches))
branches.pop()
i += 1
return "".join(strings)

return str_branches(self, [])


def insert_left(self, data):
""" Takes as input DATA (*NOT* a node !!) and MODIFIES current node this way:
- First creates a new BinaryTree (let's call it B) into which provided data is wrapped.
- Then:
- if there is no left node in self, new node B is attached to the left of self
- if there already is a left node L, it is substituted by new node B, and L becomes the
left node of B
"""

B = BinaryTree(data)
if self._left == None:
self._left = B
else:
B._left = self._left
self._left = B


def insert_right(self, data):
""" Takes as input DATA (*NOT* a node !!) and MODIFIES current node this way:
- First creates a new BinaryTree (let's call it B) into which provided data is wrapped.
- Then:
- if there is no right node in self, new node B is attached to the right of self
- if there already is a right node L, it is substituted by new node B, and L becomes the
right node of B
"""

B = BinaryTree(data)
if self._right == None:
self._right = B
else:
B._right = self._right
self._right = B


def sum_leaves_rec(self):
""" Supposing the tree holds integer numbers in all nodes,
RETURN the sum of ONLY the numbers in the leaves.
- a root with no children is considered a leaf
- implement it as a recursive Depth First Search (DFS) traversal
NOTE: with big trees a recursive solution would surely
exceed the call stack, but here we don't mind
"""

#jupman-raise

if self.left() == None and self.right() == None:
ret = self.data()
else:
ret = 0
if self.left() != None:
ret += self.left().sum_leaves_rec()
if self.right() != None:
ret += self.right().sum_leaves_rec()
return ret
#/jupman-raise


def leaves_stack(self):
""" RETURN a list holding the *data* of all the leaves of the tree,
in left to right order.
- a root with no children is considered a leaf
- DO *NOT* use recursion
- implement it with a while and a stack (as a python list)
"""
#jupman-raise
ret = []
stack = [self]
while len(stack) > 0:
node = stack.pop()
if node.left() == None and node.right() == None:
ret.append(node.data())
# first right then left so we don't need to reverse later
if node.right() != None:
stack.append( node.right() )
if node.left() != None:
stack.append( node.left() )

return ret
#/jupman-raise


0 comments on commit 8e139a3

Please sign in to comment.