Get access to all of DeriveIt's content with Premium.
Submits:
test
Test your code to get an output here!
Helper Functions
Use these functions to debug your code.
The 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])
When you print a TreeNode, it automatically gets printed as "TreeNode.of(...)"
The 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])
Optionally, you can add a loop using the second parameter. Here's an example that loops back to index 1:
ListNode.of([1, 2, 3], 1)
When you print a ListNode, it automatically gets printed as "ListNode.of(...)"
The 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.
Here's how you create the graph below. The function returns the node with value 'A'.
GraphNode.of([ ['A', 1, 2],
['B', 2],
['C', 0] ])
When you print a GraphNode, it automatically gets printed as "GraphNode.of(...)"
If the problem you're solving uses a Class, you can run your own custom test cases.
To do this, you can provide the functions that you want to call inside of an Array, along with their arguments.
Here's an example:
[['__init__', 15], ['add', 16], ['get']]
The above input will run this code:
c = MyClass(15)
c.add(16)
c.get()
The call to the __init__ function must come first and should not be repeated.