Determine the output of the following code.
It counts as cheating if you run the code!
The computer first runs x = [2, 4, 8]. This tells the computer to take x and point it to the array, so we get the following picture:
Next, the computer runs y = x. It takes y and points it wherever x points.
Next, the computer runs x[0] = 1. It takes x[0] and points it to the number 1.
Finally, the computer runs print(y[0]). The computer steps to the y, and then along the 0 pointer, and prints out 1.
This result might have been surprising, since we changed x[0], and then y[0] also changed at the same time. But it's clear why this happened if you understand how pointers work in Python.
TreeNode.of
function can be used to create an entire binary tree using 1 line of code. The input is a BFS traversal of the tree including nulls, and the output is the root node of the tree.TreeNode.of([1, None, 2, None, 3])
ListNode.of
function lets you create an entire Linked List using 1 line of code. The input is an array, and the output is the head of the Linked List.ListNode.of([1, 2, 3])
ListNode.of([1, 2, 3], 1)
GraphNode.of
function lets you create an entire Graph using 1 line of code. The input is an Adjacency Matrix, and the output is the first node in the matrix.GraphNode.of([ ['A', 1, 2], ['B', 2], ['C', 0] ])
[['__init__', 15], ['add', 16], ['get']]
c = MyClass(15) c.add(16) c.get()