Skip to content

Commit

Permalink
exam 2019-08-26, moved some old news
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLeoni committed Aug 27, 2019
1 parent 45cd752 commit 1893831
Show file tree
Hide file tree
Showing 13 changed files with 2,487 additions and 45 deletions.
2 changes: 1 addition & 1 deletion exams/2019-06-10/stack_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class WStackTest(unittest.TestCase):

def test_01_init(self):
s = WStack()
s = WStack()

def test_02_weight(self):
s = WStack()
Expand Down
24 changes: 12 additions & 12 deletions exams/2019-07-02/exam-2019-07-02-solution.ipynb

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions exams/2019-07-02/stacktris_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
class StacktrisTest(unittest.TestCase):

def test_init(self):
st = Stacktris()
self.assertEqual(st._stack, [])
st = Stacktris()
self.assertEqual(st._stack, [])

def test_str(self):
s = Stacktris()
Expand All @@ -28,15 +28,15 @@ class ShortenTest(unittest.TestCase):

def test_empty(self):
st = Stacktris()
st._shorten()
self.assertEqual(st._shorten(), [])
self.assertEqual(st._stack, [])

def test_010(self):
st = Stacktris()
st._stack = [
[0,1,0]
]
st._shorten()
self.assertEqual(st._shorten(), [])
self.assertEqual(st._stack, [
[0,1,0]
])
Expand All @@ -46,7 +46,7 @@ def test_111(self):
st._stack = [
[1,1,1]
]
st._shorten()
self.assertEqual(st._shorten(), [1,1,1])
self.assertEqual(st._stack, [
])

Expand All @@ -56,7 +56,7 @@ def test_212(self):
st._stack = [
[2,1,2]
]
st._shorten()
self.assertEqual(st._shorten(), [2,1,2])
self.assertEqual(st._stack, [
])

Expand All @@ -66,7 +66,7 @@ def test_010_221(self):
[0,1,0],
[2,2,1]
]
st._shorten()
self.assertEqual(st._shorten(), [2,2,1])
self.assertEqual(st._stack, [
[0,1,0]
])
Expand All @@ -77,7 +77,7 @@ def test_121_011(self):
[1,2,1],
[0,1,1],
]
st._shorten()
self.assertEqual(st._shorten(), [1,2,1])
self.assertEqual(st._stack, [
[0,1,1]
])
Expand All @@ -88,7 +88,7 @@ def test_121_221(self):
[1,2,1],
[2,2,1],
]
st._shorten()
self.assertEqual(st._shorten(), [2,2,1])
self.assertEqual(st._stack, [
[1,2,1]
])
Expand All @@ -100,7 +100,7 @@ def test_012_221_011(self):
[2,2,1],
[0,1,1],
]
st._shorten()
self.assertEqual(st._shorten(), [2,2,1])
self.assertEqual(st._stack, [
[0,1,2],
[0,1,1]
Expand Down
7 changes: 7 additions & 0 deletions exams/2019-08-26/B1_solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@



def inc(x):
#jupman-raise
return x + 1
#/jupman-raise
1 change: 1 addition & 0 deletions exams/2019-08-26/B1_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from B1_solution import *
163 changes: 163 additions & 0 deletions exams/2019-08-26/backpack_solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
DEBUG = True

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

class Backpack:

def __init__(self, max_weight):
""" Creates a Backpack with given max_weight.
- if max_weight is negative, raises ValueError
"""
if max_weight < 0:
raise ValueError("Expected a non-zero weight, got instead: %s " % max_weight)
self._elements = []
self._max_weight = max_weight
#jupman-raise
self._weight = 0
#/jupman-raise

def size(self):
""" RETURN the number of items in the backpack
- MUST run in O(1)
"""
#jupman-raise
return len(self._elements)
#/jupman-raise

def max_weight(self):
""" Return the maximum allowed weight
"""
return self._max_weight

def weight(self):
""" Return the backpack total current weight
************ MUST RUN IN O(1) ***************
"""
#jupman-raise
return self._weight
#/jupman-raise

def is_empty(self):
""" RETURN True if the backpack empty, False otherwise
- MUST run in O(1)
"""
#jupman-raise
return len(self._elements) == 0
#/jupman-raise

def __str__(self):
""" Return a string like
Backpack: weight=8 max_weight=10 elements=[('a',5), ('b',3)]
"""
#jupman-raise
return "Backpack: weight=%s max_weight=%s\n elements=%s" % (self._weight, self._max_weight,self._elements)
#/jupman-raise


def peek(self):
""" RETURN the top element in the stack (without removing it!)
- if stack is empty, raise IndexError
- Must run in O(1)
"""
#jupman-raise
if len(self._elements) == 0:
raise IndexError("Empty backpack !")

return self._elements[-1]
#/jupman-raise


def push(self, item, w):
""" Adds item of weight w on the top of the backpack.
- if w is negative, raises ValueError
- if w is heavier than topmost item, raises ValueError
- if max_weight is exceeded, raises ValueError
- MUST run in O(1)
"""
#jupman-raise
debug("Pushing (%s,%s)" % (item, w))
if w < 0:
raise ValueError("Expected a non-negative number, got instead %s" % w)
candidate_weight = self._weight + w
if not self.is_empty():
if w > self.peek()[1]:
raise ValueError("Pushing weight greater than top element ! %s > %s", (w, self.peek()[1]))
if candidate_weight > self._max_weight:
raise ValueError("Can't exceed max_weight ! (%s > %s)" % (candidate_weight, self._max_weight))
self._weight = candidate_weight
self._elements.append((item, w))
#/jupman-raise

def pop(self):
""" Removes the top element in the backpack and RETURN it
as a tuple (element_id, weight) like ('a', 3)
- if backpack is empty, raise IndexError
- MUST run in O(1)
"""
#jupman-raise
if len(self._elements) == 0:
raise IndexError("Empty stack !")
else:
debug("Popping %s " % str(self._elements[-1]))

el = self._elements.pop()
self._weight -= el[1]
return el
#/jupman-raise


# NOTE: this function is implemented *outside* the class !

def remove(backpack, el):
"""
Remove topmost occurrence of el found in the backpack,
and RETURN it (as a tuple name, weight)
- if el is not found, raises ValueError
- DO *NOT* ACCESS DIRECTLY FIELDS OF BACKPACK !!!
Instead, just call methods of the class!
- MUST perform in O(n), where n is the backpack size
- HINT: To remove el, you need to call Backpack.pop() until
the top element is what you are looking for. You need
to save somewhere the popped items except the one to
remove, and then push them back again.
"""
#jupman-raise
rem = []
ret = None
while not backpack.is_empty():
tup = backpack.peek()
if tup[0] == el:
ret = backpack.pop()
break
else:
rem.append(backpack.pop())

# restores backpack
while len(rem) != 0:
restored = rem.pop()
backpack.push(restored[0], restored[1])

if ret:
return ret
else:
raise ValueError("Couldn't find element %s" % el)

#/jupman-raise

0 comments on commit 1893831

Please sign in to comment.