Fickry Bil Iman - Alterra Academy - Jawaban Tes Tulis
Fickry Bil Iman - Alterra Academy - Jawaban Tes Tulis
Fickry Bil Iman - Alterra Academy - Jawaban Tes Tulis
Write a simple program which will print Fibonacci series e.g. 1 1 2 3 5 8 13 ... . up to a
given number
1. Using non-recursive/ iterative method
function fibonacci(num) {
let start = 1;
let result = 0;
let temp;
return result;
}
if (memoization[num]) {
return memoization[num];
}
if (num <= 1) {
return 1;
}
2. Write a simple function to check if a given number is prime or not. Remember, a prime
number is a
number which is not divisible by any other number e.g. 3, 5, 7, 11, 13, 17, etc.
function isPrime(num) {
if (num <= 1) {
return false;
}
if (num === 2) {
return true;
}
equal to the reverse of itself e.g. "Bob" is a palindrome because the reverse of "Bob" is
also "Bob".
function isPalindrome(word) {
let len = word.length;
if (len < 2) {
return true;
}
function isPalindrome(word) {
4. Write a function to check if two given String is Anagram of each other. Your function
should return true if the two Strings are Anagram, false otherwise. A string is said to be
an anagram if it contains the same characters and the same length but in different order
e.g. army and Mary are anagrams.
for(let j in letter) {
if (letter[j] !== 0) {
return false;
}
}
return true;
}
Tree
5. Write a pre-order traversal function (recursive) to traverse the tree Output = 9, 12, 14,
17, 19, 23, 50, 54, 67, 72, 66
6. Write a pre-order traversal function (iterative / non recursive) to traverse the tree
Output = 9, 12, 14, 17, 19, 23, 50, 54, 67, 72, 66
class Node {
constructor(val) {
this.val = val;
this.right = null;
this.left = null;
};
};
class BST {
constructor() {
this.root = null;
};
create(val) {
const newNode = new Node(val);
if (!this.root) {
this.root = newNode;
return this;
};
let current = this.root;
while(true) {
if (val === current.val) {
return this;
}
if (val < current.val) {
addSide('left');
} else {
addSide('right');
};
};
};
preOrder() {
let visited = [];
let current = this.root;
let traverse = node => {
visited.push(node.val);
if (node.left) traverse(node.left);
if (node.right) traverse(node.right);
};
traverse(current);
return visited;
};
postOrder() {
let visited = []
let current = this.root;
traverse(current);
return visited;
};
inOrder() {
let visited = [];
let current = this.root;
traverse(current);
return visited;
}
};
// console.log(tree);
// console.log(tree.root);
// console.log(tree.root.left);
// console.log(tree.root.right);
console.log(tree.preOrder()); // [ 50, 17, 12, 9, 14, 23, 19, 72, 54, 67, 76
]
console.log(tree.postOrder()); // [ 9, 14, 12, 19, 23, 17, 67, 54, 76, 72, 50
]
console.log(tree.inOrder()); // [ 9, 12, 14, 17, 19, 23, 50, 54, 67, 72, 76 ]
7. Given a series of nodes, that will form a shape. Nodes’ are given in clockwise order.
Every line formed between two nodes is either a horizontal or vertical line. Write an
algorithm to determine the number of individual shapes formed, if there is a set of
rectangles that intersect the bigger shape described before.
Problem Solving Test Diberikan struktur data berbentuk linked
list seperti berikut:
class LinkedList:
def __init__(self):
self.head = None
class Node:
def __init__(self, value):
class Node {
constructor(val) {
this.val = val;
this.next = null;
this.prev = null;
};
};
class linkedList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
};
addFirst(val) {
let newNode = new Node(val);
if (!this.head) {
this.head = newNode;
this.tail = this.head;
}
this.head.prev = newNode;
newNode.next = this.head;
this.head = newNode;
this.length++;
return this;
}
deleteLast() {
let removed = this.tail;
if (!this.tail) {
return undefined;
}
if (this.length === 1) {
this.head = null;
this.tail = null;
};
this.tail = removed.prev;
this.tail.next = null;
this.length--;
return removed;
}
find(index) {
let current;
removeAt(index) {
if (index < 0 || index >= this.length) {
return null;
}
this.length--;
return removed;
}
};
list.deleteLast()
list.removeAt(2);
console.log(list);
4 + 5 * 10 - 3
setelah diubah menjadi bentuk prefix akan menjadi seperti berikut ini:
+ 4 - * 5 10 3
Susunlah algoritma, yang dapat membantu komputer untuk menghitung ekspresi prefix di
atas.