Libraries
Luminous has some built-in libraries and are very simple to use! Simply import the corresponding class and create an instance.
HashMap
HashMap(function)
takes in a user-defined hashing function and returns a new hashmap. Note that keys of the hashmap must be numbers or strings, but values can be any datatype.
import HashMap
function hashingFunction(val) {
return (val * 31) % 11;
}
map = HashMap(hashingFunction);
insert(key, value)
takes in a key and a value, and inserts the key-value pair in the hashmap. If key already exists in the hashmap, insert(key, value)
replaces the existing value with the new value.
map.insert(1, "one");
map.insert(1, "ONE");
find(key)
return the value corresponding to the key, or null
if key does not exist in the hashmap.
print(map.find(1));
print(map.find(2));
ONE
null
delete(key)
removes the corresponding key-value pair in the hashmap and returns the value. If key does not exist, delete(key)
does nothing and returns null
.
print(map.delete(1));
print(map.delete(1));
ONE
null
Math
Math()
returns a math utility instance.
import Math
math = Math();
abs(x)
returns the absolute value of x
.
print(math.abs(0));
print(math.abs(-100));
print(math.abs(50));
0
100
50
exp()
returns Euler's number.
print(math.exp());
2.71828
factorial()
returns the factorial of x
.
print(math.factorial(1));
print(math.factorial(5));
print(math.factorial(10));
1
120
3.6288e+06
isEven(x)
returns true
if x
is even, false
otherwise.
print(math.isEven(0));
print(math.isEven(11));
print(math.isEven(200));
true
false
true
isOdd(x)
returns true
if x
is odd, false
otherwise.
print(math.isOdd(0));
print(math.isOdd(11));
print(math.isOdd(200));
false
true
false
power(x, y)
returns x
to the power of y
.
print(math.power(2, 3));
print(math.power(1, 100));
print(math.power(-5, 2));
8
1
25
Priority Queue
PriorityQueue(function)
returns a new priority queue sorted by ascending values returned by function
.
import PriorityQueue
function simplePriority(x) {
return x;
}
priorityQueue = PriorityQueue(simplePriority);
size()
returns the size of the priority queue.
print(priorityQueue.size());
0
add(x)
adds x
to the priority queue.
priorityQueue.add(10);
priorityQueue.add(25);
priorityQueue.add(5);
print(priorityQueue.size());
3
min()
returns the item with the smallest priority in the priority queue.
print(priorityQueue.min());
5
deleteMin()
returns the item with the smallest priority in the priority queue and deletes it from the priority queue.
print(priorityQueue.deleteMin());
print(priorityQueue.deleteMin());
5
10
isEmpty()
returns true
if the priority queue is empty, false
otherwise.
print(priorityQueue.isEmpty());
print(priorityQueue.deleteMin());
print(priorityQueue.isEmpty());
false
25
true
Queue
Queue()
returns a new queue.
import Queue
queue = Queue();
push(val)
pushes val
to the back of the queue.
queue.push("one");
queue.push("two");
size()
returns the number of items in the queue.
print(queue.size());
2
front()
returns the first item of the queue.
print(queue.front());
one
pop()
removes and returns the first item of the queue.
print(queue.pop());
one
isEmpty()
returns true if the queue is empty and false otherwise.
print(queue.isEmpty());
queue.pop();
print(queue.isEmpty());
false
true
Random
Random(seed)
takes in a positive integer seed
and returns a new random number generator.
import Random
rand = Random(171);
generate()
returns a new random integer.
print(rand.generate());
1.87004e+09
generateDecimal()
returns a new random decimal number between 0 and 1.
print(rand.generateDecimal());
0.675575
Stack
Stack()
returns a new stack.
import Stack
stack = Stack();
push(val)
pushes the val
to the top of the stack.
stack.push("one");
stack.push("two");
size()
returns the number of items in the stack.
print(stack.size());
2
top()
returns the top item of the stack.
print(stack.top());
two
pop()
removes and returns the top item of the stack.
print(stack.pop());
two
isEmpty()
returns true if the stack is empty and false otherwise.
print(stack.isEmpty());
stack.pop();
print(stack.isEmpty());
false
true