-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
35 lines (30 loc) · 1019 Bytes
/
util.py
File metadata and controls
35 lines (30 loc) · 1019 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class ListTable(list):
"""
Overridden list class which takes a 2-dimensional list of
the form [[1,2,3],[4,5,6]], and renders an HTML Table in
IPython Notebook.
"""
def _repr_html_(self):
html = ["<table>"]
for row in self:
html.append("<tr>")
for col in row:
html.append("<td>{0}</td>".format(col))
html.append("</tr>")
html.append("</table>")
return ''.join(html)
def print_knapsack_table(d, rows, m, v):
from IPython.core.display import display, HTML
n = len(d) - 1
M = len(d[0]) - 1
table = ListTable()
table.append(["", "", "", "0 <= j <= M kg"])
table.append(["i", "mass", "value", [x for x in range(M+1)]])
for i in range(rows+1):
if i == 0:
table.append([i, '-', '-', d[i]])
else:
table.append([i, str(m[i-1]) + "kg", "$" + str(v[i-1]), d[i]])
display(HTML("<h3>i=%s</h3>" % rows))
display(table)
display(HTML('<hr>'))