Keyword Table

if else for while break
continue return repeat until function
is in or and not

Basic Syntax

  1. General syntax similar to Python
  2. No semicolons(;) and colon(:) in pseudo code
  3. For contents inside if/else, function, while, for, there should be a tab ahead of line.
  4. For loop syntax:
    (a) for x in y 
    (b) for i = 0 to 9
    (c) for i = 0 to 9, step 1
  5. While loop syntax, expr should be boolean or iterator-able variable
    while expr
  6. If else syntax:
    if expr1
    do something
    else if expr2
    do something
    else
    do something
  7. Define a function:
    function func-name(pram1, pram2,...)
    statement1
    statement2
    statement3

Random Factory

Random Factory is a separate package inside compiler which could generate instances of data structures randomly on the fly.
Initialization
ds = random_factory(structure, [lowRange, upRange])

For example: graph = random_factory(Graph)
Supported Data Structures
Graph, DiGraph, List, Stack, Queue, LinkedList, BinaryMinHeap, BinarySearchTree, AVLTree, RBTree, Matrix

Color Theme

Color theme is used to help highlight the elements changed.

Newly added element:

Indexed element:

Changed element:

Dict

Dictionaries are indexed by keys, which can be any immutable type; Strings and numbers can always be keys.

Initialization
d = {key1: val1, key2, val2}
d[key]
Return the item of d with key key
d[key] = value
Set d[key] to value
key in d
Return True if d has a key key, else False.
key not in d
Equivalent to not key in d.
.clear()
Remove all items from the dictionary
.get(key)
Return the value for key if key is in the dictionary
.items()
Return a copy of the dictionary's list of (key, value) pairs
.keys()
Return a copy of the dictionary's list of keys.
.pop(key)
If key is in the dictionary, remove it and return its value
.update([other])
Update the dictionary with the key/value pairs from other, overwriting existing keys.
.values()
Return a copy of the dictionary’s list of values.

List

A sequence of elements. Each element of a sequence is assigned a number - its position or index.
Initialization
arr = [val1, val2, val3 ...]
arr[i]
Return the element of index i
.append(x)
Add an item to the end of the list.
.insert(i,x)
Insert an item at a given position. The first argument is the index of the element before which to insert.
.remove(x)
Remove the first item from the list whose value is x.
.pop([i])
Remove the item at the given position in the list, and return it. 
If no index is specified, list.pop() removes and returns the last item in the list.
.swap(index1, index2)
Swap the element of index1 with element of index2.

Queue

A sequence of elements. First in first out.
Initialization
q = Queue()
len(q)
Return the length of the queue.
.enqueue(x)
Add an item to the end of the queue.
.dequeue()
Return the first item from the queue and return the value.

Stack

A sequence of elements. First in Last out.
Initialization
s = Stack()
.top
Return the top element of the Stack.
len(s)
Return the length of the stack.
.push(x)
Add an item to the end of the Stack.
.pop()
Return the last item from the stack and return the value.

LinkedList

Single linked list.
Initialization
list = LinkedList()
.length
Return length of the linked list.
.head
Return the top element of the linked list.
.search(val)
Return the first item from the list whose value is val.
.insert(x)
Add an item to the end of the linked list.
.delete(val)
Remove the first item from the list whose value is val.
.get(index)
Return the item from the list whose index is index.

Graph

Graph data structure consists of a finite set of nodes or vertices, together with a set of ordered pairs of these nodes called edges.
Initialization
graph = Graph(verticesList, edgesList, [weightsList])
weightsList is optional.

Example:
vertices = ('x', 'y', 'z')
edges = (('x', 'y'), ('x', 'z'))
weights = (1,2)
graph = Graph(vertices, edges, weights)
.vertex_count
Return the number of vertices.
.edge_count
Return the number of edges.
.V
Return the list of vertices in the graph, list is unchangeable.
.E
Return the list of edges in the graph, list is unchangeable.
.getVertex(Vertex-Name)
Return the vertex object in graph based on the Vertex-Name.
.getEdge(fromNode, toNode)
Return the edge between fromNode and toNode.
.getAdjEdges(node)
Return all the edges connecting to node.
.getWeight(edge)
Return the weight of the edge.
.setWeight(edge, weight)
Set the weight of edge to new weight.
.checkVex(node)
Return true if node is inside graph; else return false.
.checkEdge(edge)
Return true if edge is inside graph; else return false.
.addEdge(edge, weight)
Add new edge to the graph. Example: graph.addEdge(('x','y'), 9).
.delEdge(edge)
Remove the edge from graph.
.addVertex(node)
Add new node to the graph Example: graph.addVertex("A").
.delVertex(node)
Remove the node from graph.

Directed Graph

Similar to Graph, Directed Graph edges have a direction associated with them.
Initialization
graph = DiGraph(verticesList, edgesList, [weightsList])
weightsList is optional.

Example:
vertices = ('x', 'y', 'z')
edges = (('x', 'y'), ('x', 'z'))
weights = (1,2)
graph = DiGraph(vertices, edges, weights)
Directed Graph is inherited from Graph, please refer to Graph for function API.

DisjointSet

Disjoint set is a data structure that stores a set of elements which are divided into a number of disjoint subsets.
Initialization
 x = DisjointSet()
.add(item)
Create a new set with one element: item.
.find(item)
Return the root of the set which the item belongs to.
.union(item1, item2)
Union the two sets where the two items belong to.

Min Priority Queue

This is Minimal Priority Queue and element with smallest priority value is root node of the tree.
Initialization
pqueue = MinPriorityQueue()
.add_with_priority(obj, priority)
Add an element to the queue with an associated priority.
.decrease_priority(obj, priority)
Decrease the priority of the element.
.extract_min()
Remove the element from the queue that has the minimal priority, and return it.

Binary Min Heap

Structure similar to Binary Search Tree. But the root value is the smallest among all the elements and the parent node value should be smaller than child node's value.
Initialization
Initialize with a list of un-sorted elements or without any parameter.
heap = BinaryMinHeap([list of elements])
.top
Return the top element which has the minimal value.
.isEmpty
Return whether the heap is empty or not.
.pop()
Return and remove the element from the heap that has the minimal value.
.push(item)
Add a new item to the heap.
len(heap)
Return the number of elements in the heap.
iter(heap)
Return the iterator of the heap.

Binary Search Tree

Binary search tree is a node-based binary tree data structure that the value in any node is larger than all the values in its left sub-tree and smaller than all the values in its right sub-tree.
Initialization
tree = BinarySearchTree()
.add(value)
Add new item to the tree.
.find(value)
Find and return the node with value "value" in the tree.
.remove(value)
Remove the first occurrence of value in the tree. (From top to bottom)
.getTreeHeight()
Return the height of the tree.
.toList()
Return the list representation of the tree.(In-order)
.clear()
Clear the entire tree.

AVL Tree

AVL tree is a self balanced binary search tree and the heights of the two child sub trees di er by at most one.
Initialization
tree = AVLTree()
.add(value)
Add new item to the tree.
.find(value)
Find and return the node with value "value" in the tree.
.remove(value)
Remove the first occurrence of value in the tree. (From top to bottom)
.getTreeHeight()
Return the height of the tree.
.toList()
Return the list representation of the tree.(In-order)
.clear()
Clear the entire tree.

RB Tree

RB tree is a self balanced binary search tree.
Initialization
tree = RBTree()
.add(value)
Add new item to the tree.
.find(value)
Find and return the node with value "value" in the tree.
.remove(value)
Remove the first occurrence of value in the tree. (From top to bottom)
.getTreeHeight()
Return the height of the tree.
.toList()
Return the list representation of the tree.(In-order)
.clear()
Clear the entire tree.

Matrix

Matrix in mathematics and support all the mathematical operations in matrix.
Initialization
matrix = Matrix(row, col, [initVal])
.add(anothermatrix)
Add this matrix with another matrix.
.mul(anothermatrix)
Multiply this matrix with another matrix.
.pow(number)
Matrix power. Positive integer powers are supported.

String Matrix

This is specially designed for dynamic programming table, headers and arrows are supported in the table.
Initialization
matrix = StringMatrix(row, col, [initVal]) 
matrix = StringMatrix.FromList(list)
.setTopHeader(header)
Set the top header of the DP Table/String matrix. header could be string or array.
.setLeftHeader(header)
Set the left header of the DP Table/String matrix. header could be string or array.
.addEdge(row1, col1, row2, col2)
Add an arrow from (row1, col1) to (row2, col2).
.removeEdge(row1, col1, row2, col2)
Remove the arrow from (row1, col1) to (row2, col2).


Code Visualization