Skip to content

Instantly share code, notes, and snippets.

Assignment 2

interpreter.py

How does --trace work?

The structure resembles a tree where each node has properties: name, execution_time and children

Two global variables are important: current_frame and trace_root

Assignment 1

smart_house.py

How does SmartHouseManagement have access to all current instantiated objects?

  • Is not done via introspection (first approach)
    • We couldn’t use the if __name__ == "__main__" guard this way (objects instantiated inside of guard aren’t in globals)
  • We added a hook in make that adds new objects (just dicts) to a global instances list, SmartHouseManagement class uses this list to iterate over all objects

Assignment 3, Java

zvfs.java

Why read chunks of 8192 Bytes?

  • Buffers have to be a power of 2 because otherwise it’s possible to have a misalignment with filesystem blocks provided by operating system (requiring more blocks to be read for each operation, making IO slower)
  • 8192 is larger than 4096 which is the default on windows (⇒ performance gain)
  • 8192 (8 KiB) provides a good balance between performance and memory usage
struct node* BSTreeDelete(struct node* root, struct node* x) {
u = root; v = NULL;
while (u != x) {
v = u;
if (x->key < u->key) u = u->lft;
else u = u->rgt;
}
if (u->rgt == NULL) {
if (v == NULL) root = u->lft;
struct node* BSTTreeInsert(struct node* p, struct node* r) {
struct node* y = NULL;
struct node* x = r;
while (x != NULL) {
// y is one step behind y = x;
if (x -> key < p -> key) {
x = x -> rgt;
} else {
struct node* BSTreeSuccAbove(struct node* p, struct node* x) {
struct node* succ = NULL;
while (p != x) {
if (x->key < p->key) {
succ = p;
p = p->lft;
} else if (x->key > p->key) {
p = p->rgt;
}
}
BSTRecCount(struct node* p, int x) {
if (p ≠ NULL) {
if (x < p->val) {
return BSTRecCount(p->lft);
} else if (x > p-val) {
return BSTRecCount(p->rgt);
} else {
return 1 + BSTRecCount(p->lft) + BSTRecCount(p->rgt)
}
} else {
// Iterative
BSTreeSearch(p,v)
while p ≠ NIL AND p->key≠v do
if v<p->key then
p = p->lft
else
p = p->rgt;
return p;
// Recursive
infix(struct node* p = {
if (p ≠ NULL) {
if (needParanthesis(p, false)) {
printf("(");
infix(p->lft);
printf(")");
} else {
infix(p->lft);
}
}
bool needsParentheses(struct node* p, bool rgt) {
return (
// (a + b) / c
!rgt && precedence(p) < precedence(p->lft)) ||
// case where right operation has precedence
// for example: a + (b * c)
(rgt && precedence(p) < precedence(p->rgt)) ||