0% found this document useful (0 votes)
33 views9 pages

Fickry Bil Iman - Alterra Academy - Jawaban Tes Tulis

The document provides examples of functions to: 1. Write a Fibonacci series using iterative and recursive methods. 2. Check if a number is prime. 3. Check if a string is a palindrome using iterative and recursive methods. 4. Check if two strings are anagrams. 5. Traverse a binary search tree using pre-order traversal recursively. 6. Traverse a binary search tree using pre-order traversal iteratively.

Uploaded by

Andi Fitrianto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views9 pages

Fickry Bil Iman - Alterra Academy - Jawaban Tes Tulis

The document provides examples of functions to: 1. Write a Fibonacci series using iterative and recursive methods. 2. Check if a number is prime. 3. Check if a string is a palindrome using iterative and recursive methods. 4. Check if two strings are anagrams. 5. Traverse a binary search tree using pre-order traversal recursively. 6. Traverse a binary search tree using pre-order traversal iteratively.

Uploaded by

Andi Fitrianto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

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;

while (num >= 0) {


temp = start;
start += result;
result = temp;
num--;
}

return result;
}

2. Using recursive method

function fibonacci(num, memoization) {

memoization = memoization || {};

if (memoization[num]) {
return memoization[num];
}

if (num <= 1) {
return 1;
}

return memoization[num] = fibonacci(num - 1, memoization) + fibonacci(num -


2, memoization);
}


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;
}

let numberToLoop = Math.ceil(Math.sqrt(num));

for (let i = 2; i <= numberToLoop; i++) {


if (num % i === 0) {
return false;
}
}
return true;
}

3. Write a simple function to check if a given String is palindrome or not. A Palindrome is a


String which is

equal to the reverse of itself e.g. "Bob" is a palindrome because the reverse of "Bob" is
also "Bob".

1. Using non-recursive/ iterative method

function isPalindrome(word) {
let len = word.length;

if (len < 2) {
return true;
}

for (let i = 0; i < len / 2; i++) {


if (word.toLowerCase().charAt(i) !== word.toLowerCase().charAt(len - 1 -
i)) {
return false;
}
return true;
}
}

2. Using recursive method

function isPalindrome(word) {

if (word.toLowerCase()[0] === word.toLowerCase()[word.length - 1]) {


console.log(word.slice(1, -1));
return word.length <= 1 ? true : isPalindrome(word.slice(1, -1));
}
return false;
}

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.

function isAnagram(firstWord, secondWord) {


if (firstWord === secondWord) {
return true;
}

if(firstWord.length !== secondWord.length) {


return false;
}

let letter = {};

for (let i in firstWord) {


letter[firstWord[i].toLowerCase()] = (letter[firstWord[i]] || 0) + 1;
letter[secondWord[i].toLowerCase()] = (letter[secondWord[i]] || 0) - 1;

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;

const addSide = side => {


if (!current[side]) {
current[side] = newNode;
return this;
};
current = current[side];
};

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;

let traverse = node => {


if (node.left) traverse(node.left);
if (node.right) traverse(node.right);
visited.push(node.val);
};

traverse(current);
return visited;
};

inOrder() {
let visited = [];
let current = this.root;

let traverse = node => {


if (node.left) traverse(node.left);
visited.push(node.val);
if (node.right) traverse(node.right);
};

traverse(current);
return visited;
}

};

const tree = new BST();


tree.create(50);
tree.create(17);
tree.create(72);
tree.create(12);
tree.create(23);
tree.create(54);
tree.create(76);
tree.create(9);
tree.create(14);
tree.create(19);
tree.create(67);

// 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.

The number of individual shapes in the illustration is 3.

--- 0o0 ---


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):

self.value = value self.next = None

Susunlah algoritma untuk function-function berikut ini:

1. void addFirst(int value)


2. 2. int deleteLast()
3. 3. int removeAt(int index)

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;

if (index < 0 || index >= this.length) {


return undefined;
}

if (index <= this.length / 2) {


current = this.head;
for (let i = 0; i < index; i++) {
current = current.next;
}
} else {
current = this.tail;
for (let i = this.length; i > index; i--) {
current = current.prev;
}
}
return current
}

removeAt(index) {
if (index < 0 || index >= this.length) {
return null;
}

let removed = this.find(index);


removed.prev.next = removed.next;
removed.next.prev = removed.prev;

this.length--;
return removed;
}
};

const list = new linkedList();


list.addFirst(7);
list.addFirst(3);
list.addFirst(35);
list.addFirst(21);

list.deleteLast()
list.removeAt(2);

console.log(list);

Problem Solving Test


Sebuah ekspresi Matematika hanya bisa dikerjakan oleh komputer bilamana ekspresi
tersebut telah

disusun dalam bentuk prefix (atau postfix) . Contoh: Bentuk infix

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.

You might also like