0% menganggap dokumen ini bermanfaat (0 suara)
4 tayangan21 halaman

Panduan Pemrograman Dasar dan Algoritma

Diunggah oleh

Hilmi Ahmad Fauzi
Hak Cipta
© All Rights Reserved
Kami menangani hak cipta konten dengan serius. Jika Anda merasa konten ini milik Anda, ajukan klaim di sini.
Format Tersedia
Unduh sebagai PDF, TXT atau baca online di Scribd
0% menganggap dokumen ini bermanfaat (0 suara)
4 tayangan21 halaman

Panduan Pemrograman Dasar dan Algoritma

Diunggah oleh

Hilmi Ahmad Fauzi
Hak Cipta
© All Rights Reserved
Kami menangani hak cipta konten dengan serius. Jika Anda merasa konten ini milik Anda, ajukan klaim di sini.
Format Tersedia
Unduh sebagai PDF, TXT atau baca online di Scribd

Basic Programming

● Important! Kemampuan Problem Solving sangatlah penting dalam pemrograman,


sebagai Software Engineer kamu akan dihadapkan permasalahan baru dan unik.
Menguasai Problem Solving tidak akan bisa kamu dapat secara instan, kamu harus sering
mengasah kemampuan tersebut dengan mengerjakan soal-soal pemrograman. Sebagai
pemula kerjakan soal-soal pemrograman di platform LeetCode dengan level Easy atau
Medium. Berusahalah konsisten menyelesaikan min. 2 dalam sehari.
● Penting namun tidak urgent, untuk meningkatkan kecepatan dalam coding, kamu sangat
disarankan untuk latihan Typing Fast ([Link]

PART 1 ─ Versioning

Buatlah github repository lalu masukkan Repository name dengan format


“ALTA-FE-BATCH1-Nama”, pilih Private sebagai repository visibility, lalu tekan tombol Create
repository.
- FE: Rubah sesuai dengan program yang di ambil.
- BATCH1: Rubah sesuai dengan batch yang kamu jalani.
- Nama: Rubah dengan nama panggilan
Setelah itu invite mentor yang bersangkutan kedalam repository menggunakan
username/email github mentor.

PART 2 ─ Intro to Algorithm

Problem 1 - Bilangan Keren


Suatu bilangan bulat positif disebut Bilangan Keren jika bilangan tersebut hanya habis dibagi
oleh bilangan 1, dirinya sendiri, dan maksimal dapat dibagi habis dengan 2 bilangan bulat positif
selain bilangan 1 dan bilangan dirinya sendiri. Buatlah flowchart untuk program Bilangan
Keren!

Gunakan web [Link] untuk menggambar flowchart!

Sample Test Cases


Input: 17
Output: YA

Input: 51
Output: YA
Input: 52
Output: TIDAK

Problem 2 - Konversi Nilai


Seorang Dosen sedang memeriksa ujian mahasiswa dan akan memberikan nilai pada
mahasiswa dengan A, B+, B, C, & D. dan kriteria penilaian dosen adalah sebagai berikut:

Nilai Number Nilai Huruf

80 sampai 100 A

65 sampai 79 B+

50 sampai 64 B

35 sampai 49 C

0 sampai 34 D

Buatlah flowchart dan tampilkan nilai mahasiswa saat dosen tersebut memasukkan nilai yang
dia inginkan. Jika nilai lebih dari 100 dan kurang dari 0 keluarkan kata “invalid”.

Problem 3 - Lampu dan Tombol!


Terdapat N tombol yang di nomori dari 1 hingga N dan sebuah lampu dalam keadaan mati.
Apabila tombol ke-i ditekan, keadaan lampu akan berubah apabila N habis dibagi oleh i (dari
mati menjadi menyala, atau sebaliknya). Apabila masing-masing tombol ditekan tepat sekali,
bagaimana keadaan lampu pada akhirnya?

Format Masukan
Sebuah baris berisi sebuah bilangan, yaitu N.
Sebuah baris berisi sebuah bilangan, yaitu N. Format Keluaran Sebuah baris berisi:
● "lampu mati", apabila keadaan akhir lampu adalah mati.
● "lampu menyala", apabila keadaan akhir lampu adalah menyala.

Buatlah flowchart untuk algoritma tersebut!

Sample Test Cases


Contoh Masukan 1
5
Contoh Keluaran 1
lampu mati
Contoh Masukan 2
4
Contoh Keluaran 2
lampu menyala

Penjelasan
Pada contoh pertama, tombol yang mempengaruhi keadaan lampu adalah tombol 1 dan tombol
5. Penekanan tombol 1 mengakibatkan lampu menjadi menyala, dan penekanan tombol 5
mengembalikannya ke keadaan mati.

Pada contoh kedua, tombol yang mempengaruhi keadaan lampu adalah tombol 1, tombol 2,
dan tombol 4. Penekanan tombol 1 mengakibatkan lampu menjadi menyala, penekanan tombol
2 mengembalikannya ke keadaan mati, dan penekanan tombol 4 membuat lampu kembali
menyala.

PART 3 ─ Variables and Data Types

Problem 1 - String Concatenation


Prompts input first name, last name and born year. Join the data and display a personalized
message to the user.

Enter your first name: John


Enter your last name: Doe
Enter your born year: 1998

Hello, John Doe! You are 25 years old.

Problem 2 - Menghitung Luas Permukaan Tabung


Luas permukaan tabung dapat dikatakan sebagai luas dari jumlah sisi yang dimiliki tabung.
Lp = 2 pi r^2 + 2 pi r T
= 2 pi r (r + T)

Pi = 22/7 atau 3.14


r = radius (jari jari lingkaran)
T = tinggi tabung
Buatlah sebuah program untuk menghitung luas permukaan tabung! Pada program ini kamu
akan diberikan sebuah inputan tinggi tabung (T) dan jari-jari tabung (r).

Sample Test Cases


Input: T = 20, r = 4
Output: 602.88

// input
let T = 20
let r = 4

// kode disini

Problem 3 - Temperature Converter


Write a JavaScript program that converts temperatures between Celsius, Fahrenheit, and
Kelvin. The program should prompt the user for an input temperature and the unit of that
temperature (Celsius, Fahrenheit, or Kelvin). Based on the input, convert the temperature to
the other two units and display the converted values.

Celsius to Fahrenheit: F = C * 9/5 + 32


Celsius to Kelvin: K = C + 273.15
Fahrenheit to Celsius: C = (F - 32) * 5/9
Fahrenheit to Kelvin: K = (F + 459.67) * 5/9
Kelvin to Celsius: C = K - 273.15
Kelvin to Fahrenheit: F = K * 9/5 - 459.67

Sample Test Cases


Input: Number = 25, Unit = Celsius
Output: 25 Celsius is equal to 77 Fahrenheit and 298.15 Kelvin

PART 4 - Function and Branching

Problem 1 - Exponentiation

Given two integers x and n, write a function to compute x^n.

Sample Test Cases


Input : x = 2, n = 3
Output : 8

Input : x = 7, n = 2
Output : 49

function exponentiation(x, n) {
// your code here
}

[Link](exponentiation(2, 3)) // 8
[Link](exponentiation(2, 12)) // 4096
[Link](exponentiation(7, 2)) // 49
[Link](exponentiation(9, 3)) // 729
[Link](exponentiation(22, 5)) // 5153632
[Link](exponentiation(1996, 0)) // 1
[Link](exponentiation(4213, -3)) // “wrong input”

Problem 2 - Palindrome
Kata palindrome adalah sebuah kata yang jika dibalik, tetap sama. Contoh, 'katak' dibalik
tetaplah 'katak'. Buatlah sebuah program untuk mendeteksi sebuah string merupakan
palindrom atau tidak!

Sample Test Cases


Input: katak
Output: true

Input: kupu-kupu
Output: false

function palindrome(word) {
// your code here
}

[Link](palindrome("civic")) // true
[Link](palindrome("katak")) // true
[Link](palindrome("kasur rusak")) // true
[Link](palindrome("kupu-kupu")) // false
[Link](palindrome("lion")) // false

Problem 3 - Mean dan Median

Diberikan sebuah program yang menerima sebuah input array angka. Program akan
menampilkan mean dan median dari array angka tersebut. Pada soal ini dapat dipastikan
bahwa input number sudah terurut.

Mean adalah angka rata-rata dari deret bilangan tersebut. Contoh, mean dari [1, 2, 3, 4] adalah
2.5. Kita perlu kemudian melakukan pembulatan angka dari hasil mean yang didapatkan.
Median dari deret yang berjumlah genap adalah rata-rata dari dua nilai tengah. Contoh median
dari [1, 2, 3, 4] adalah 2.5, hasil dari 2 + 3 dibagi dengan 2.

Sample Test Cases


Input: [1, 2, 3, 4]
Output: 2.5 2.5

function meanMedian(numbers) {
// your code here
}

[Link](meanMedian([1, 2, 3, 4])) // 2.5 2.5


[Link](meanMedian([1, 2, 3, 4, 5])) // 3 3
[Link](meanMedian([7, 8, 9, 13, 15])) // 10.4 9
[Link](meanMedian([10, 20, 30, 40, 50])) // 30 30
[Link](meanMedian([15, 20, 30, 60, 120])) // 49 30

PART 5 - Object and Array

Problem 1 - Compare String

Kamu diberikan dua string A dan B. Tentukan kesamaan substring diantara kedua string
tersebut.

Sample Test Case


Input: A = “AKA” B = “AKASHI”
Output: AKA

Input: A = “KANGAROO” B = “KANG”


Output: KANG

function compareString(a, b) {
// your code here
}

[Link](compareString("AKA", "AKASHI")) // AKA


[Link](compareString("KANGAROO", "KANG")) // KANG
[Link](compareString("KI", "KIJANG")) // KI
[Link](compareString("KUPU-KUPU", "KUPU")) // KUPU
[Link](compareString("ILALANG", "ILA")) // ILA

Problem 2 - Join Array Remove Duplicate


Buatlah sebuah program menggabungkan 2 array input, dalam problem ini jika terdapat nama
yang sama di dalam 2 array tersebut, maka kamu hanya boleh memasukan satu nilai saja.

Sample Test Cases


Input: ['apel', 'nanas', 'anggur'], ['apel', 'pisang']
Output: ['apel', 'nanas', 'anggur', 'pisang']

Input: ['apel', 'nanas'], ['anggur', 'apel']


Output: ['apel', 'nanas', 'anggur', 'apel']

function joinArrayRemoveDuplicate(arrayA, arrayB) {


// your code here
}
// Test cases
[Link](joinArrayRemoveDuplicate(["apel", "anggur"], ["lemon", "leci", "nanas"]))
// ["apel", "anggur", "lemon", "leci", "nanas"]

[Link](joinArrayRemoveDuplicate(["samsung", "apple"], ["apple", "sony",


"xiaomi"]))
// ["samsung", "apple", "sony", "xiaomi"]

[Link](joinArrayRemoveDuplicate(["football", "basketball"], ["basketball",


"football"]))
// [“football”, “basketball”]
Problem 3 - Remove Duplicates

Given an array of sorted numbers, remove all duplicates from it. You should not use any extra
space; after removing the duplicates in-place return the length of the subarray that has no
duplicate in it.

Sample Test Case


Input: [2, 3, 3, 3, 6, 9, 9]
Output: 4
Explanation: The first four elements after removing the duplicates will be [2, 3, 6, 9].

Sample Test Case


Input: [2, 2, 2, 11]
Output: 2
Explanation: The first two elements after removing the duplicates will be [2, 11].

function removeDuplicates(array) {
// your code here
}

[Link](removeDuplicates([2, 3, 3, 3, 6, 9, 9])) // 4
[Link](removeDuplicates([2, 3, 4, 5, 6, 9, 9])) // 6
[Link](removeDuplicates([2, 2, 2, 11])) // 2
[Link](removeDuplicates([1, 1, 2, 2, 3, 3, 4, 4])) // 4
[Link](removeDuplicates([1, 2, 3, 11, 11])) // 4

PART 6 - Loops and Recursive

Problem 1 - Exponentiation (Recursive)


Given two integers x and n, write a function to compute x^n with a recursion approach.

Sample Test Cases


Input : x = 2, n = 3
Output : 8

Input : x = 7, n = 2
Output : 49
function exponentiation(x, n) {
// your code here
}

[Link](exponentiation(2, 3)) // 8
[Link](exponentiation(2, 12)) // 4096
[Link](exponentiation(7, 2)) // 49
[Link](exponentiation(9, 3)) // 729
[Link](exponentiation(22, 5)) // 5153632
[Link](exponentiation(1996, 0)) // 1
[Link](exponentiation(4213, -3)) // “wrong input”

Problem 2 - Prima ke X

Dalam matematika, bilangan prima adalah bilangan asli yang lebih besar dari angka 1, yang
faktor pembaginya adalah 1 dan bilangan itu sendiri. Angka 2 dan 3 adalah bilangan prima.
Angka 4 bukan bilangan prima karena 4 bisa dibagi 2. Sepuluh deret bilangan prima yang
pertama adalah [2, 3, 5, 7, 11, 13, 17, 19, 23 dan 29]

Buatlah sebuah fungsi bernama primeX yang menampilkan bilangan prima sesuai dengan deret
urutannya.

Sample Test Cases


Input: 1
Output: 2

Input: 5
Output: 11

function primeX(number) {
// your code here
}

[Link](primeX(1)) // 2
[Link](primeX(5)) // 11
[Link](primeX(10)) // 29
[Link](primeX(15)) // 47
[Link](primeX(20)) // 71
Problem 3 - Prima Segi Empat
Buatlah segiempat berukuran high x wide yang berisikan bilangan prima setelah start, pada
bagian akhir jumlahkan seluruh bilangan prima tersebut.

function primaSegiEmpat(wide, high, start) {


// your code here
}

[Link](primaSegiEmpat(2, 3, 13))
/*
17 19
23 29
31 37
156
*/
[Link](primaSegiEmpat(5, 2, 1))
/*
2 3 5 7 11
13 17 19 23 29
129
*/

Problem 4 - Find the Largest Number in an Array (recursive)


Write a function that takes an array of numbers and returns the largest one using a recursive
approach.

Sample Test Cases


Input : array = [1, 2, 3, 4, 5]
Output : 5

Input : array = [10, 20, 15, 30, 40, 25]


Output : 40

function largestNumber(array) {
// your code here
}
[Link](largestNumber([5, 2, 67, 37, 85, 19, 10])) // 85
[Link](largestNumber([5, 10, 20, 3, 98, 95])) // 98
[Link](largestNumber([20, 22, 18, 25, 75, 62, 88])) // 88
[Link](largestNumber([6, 23, 9, 5])) // 23
[Link](largestNumber([70, 44, 28, 18, 55, 68, 11])) // 70

PART 7 - Sorting and Searching

Problem 1 - Maximum Buy Product


Kamu diminta untuk membeli sebuah barang, dan tantangan kali ini kamu harus bisa membeli
barang dengan jumlah maksimum dengan uang yang kamu miliki.

Program ini menerima money sebagai parameter pertama, dan yang kedua berupa harga list
produk yang bisa kamu beli. Kamu harus menampilkan nilai jumlah barang yang bisa dibeli.

Sample Test Cases


Input: money = 50000, productPrice = [25000, 25000, 10000, 14000]
Output: 3

Input: money = 30000, productPrice = [15000, 10000, 12000, 5000, 3000]


Output: 4

function maximumBuyProduct(money, productPrice) {


// your code here
}

[Link](maximumBuyProduct(50000, [25000, 25000, 10000, 14000])) // 3


[Link](maximumBuyProduct(30000, [15000, 10000, 12000, 5000, 3000])) // 4
[Link](maximumBuyProduct(10000, [2000, 3000, 1000, 2000, 10000])) // 4
[Link](maximumBuyProduct(4000, [7500, 3000, 2500, 2000])) // 1
[Link](maximumBuyProduct(0, [10000, 30000])) // 0

Problem 2 - Playing Domino


Yuk kita bermain domino, syarat dari permainan domino adalah kartu yang disarankan untuk
dikeluarkan adalah kartu yang salah satu angkanya sama dengan kartu yang ada di deck, dan
jumlah kartu tersebut merupakan jumlah terbesar. Jika tidak ada kartu yang memenuhi maka
kamu perlu "tutup kartu".
Buatlah program playingDomino yang menerima 2 parameter array.

● Parameter pertama merupakan kartu domino yang ada di tangan.


● Parameter kedua merupakan kartu yang sedang ada di deck.

Jika ada kartu yang disarankan maka output: [x,y], jika tidak ada kartu yang sesuai maka
keluarkan: [].

Sample Test Cases


Input : kartu = [[6, 5], [3, 4], [2, 1], [3, 3]], deck = [4, 3]
Output : [3, 4]

function playingDomino(cards, deck) {


// your code here
}

[Link](playingDomino([[6, 5], [3, 4], [2, 1], [3, 3]], [4, 3]))
// [3, 4]
[Link](playingDomino([[6, 5], [3, 3], [3, 4], [2, 1]], [3, 6]))
// [6 5]
[Link](playingDomino([[6, 6], [2, 4], [3, 6]], [5, 1]))
// []

Problem 3 - Most Appear Item


Buatlah sebuah program Most Appear Item yang dapat mengurutkan barang berdasarkan
jumlah kemunculannya. Jika ada barang yang duplicate kamu hanya perlu memunculkan sekali,
namun kamu perlu menampilkan total kemunculan barang tersebut.

Sample Test Cases


Input: ["js", "js", "golang", "ruby", "ruby", "js", "js"]
Output: { golang: 1, ruby: 2, js: 4 }

function mostAppearItem(items) {
// your code here
}
[Link](mostAppearItem(["js", "js", "golang", "ruby", "ruby", "js", "js"]))
// { golang: 1, ruby: 2, js: 4 }
[Link](mostAppearItem(["A", "B", "B", "C", "A", "A", "B", "A", "D", "D"]))
// { C: 1, D: 2, B: 3, A: 4 }
[Link](mostAppearItem(["football", "basketball", "tenis"]))
// { football: 1, basketball: 1, tenis: 1 }

PART 8 - Asynchronous

Problem 1 - Let’s have some snack

You want to get some snacks like boba and seblak, and in your pocket, you only have Rp.
20.000, there’s price list of snacks:

- Boba: Rp. 5.000, estimated time to get boba: 5 second


- Seblak: Rp. 8.000, estimated time to get seblak: 9 second

You asked to create programs to solve this problem using callback function

function jajanBoba(uang, callback) {


// your code here
}

function jajanSeblak(uang) {
// your code here
}

jajanBoba(20000, jajanSeblak)
jajanBoba(10000, jajanSeblak)

Expected output

kamu jajan boba dengan harga Rp. 5000


sisa uang kamu Rp. 15000

kamu jajan seblak dengan harga Rp. 8000


sisa uang kamu sebesar Rp. 7000

// if you doesn’t have enough money


Maaf uang kamu belum cukup untuk membeli <nama barang>
Sisa uang kamu sebesar <sisa uang>
Don’t forget to handle it if you don't have enough money to buy that snack and don’t forget to
use set timeout.

Problem 2 - Let’s buy some apparel


You want to buy some apparel in a mall, and every apparel has a different price and different
time to proceed the order. So you have to create a program that handles the process of buying
that apparel.

const clothes = {
item: "clothes",
price: 15000,
time: 3000,
};

const pants = {
item: "pants",
price: 25000,
time: 7000,
};

const hat = {
item: "hat",
price: 22000,
time: 2000,
};

const shoes = {
item: "shoes",
price: 46000,
time: 10000,
};

function buyApparel(money, objItem, callback) {


// your code here
}

Expected Output
saya membawa uang sebesar Rp. 150.000
saya ingin membeli baju
dengan harga Rp. 15.000
dan waktu yang dibutuhkan adalah 3 detik

saya membawa uang sebesar Rp. 135.000


saya ingin membeli celana
dengan harga Rp. 25.000
dan waktu yang dibutuhkan adalah 7 detik

saya membawa uang sebesar Rp. 110.000


saya ingin membeli topi
dengan harga Rp. 22.000
dan waktu yang dibutuhkan adalah 2 detik

saya membawa uang sebesar Rp. 88.000


saya ingin membeli sepatu
dengan harga Rp. 46.000
dan waktu yang dibutuhkan adalah 10 detik

sisa kembaliannya adalah Rp. 42.000

Problem 3 - Lottery
You’ll create a lottery program that receives number input from users and chances to win this
lottery is 1 : 1000 and users have to wait for this lottery for 10 seconds.
You have to create a random number from 1 - 1000, if the user number and random number
are equal, you have to send a congratulations message to that user who won this lottery, if it is
not equal, you have to send an apology message to the user that doesn’t win the lottery.
1 <= num <= 1000

Code
function lottery(num) {
// your code here
}

lottery(5)
.then((res) => [Link](res))
.catch((err) => [Link](err))
.finally(() => [Link]("undian lotre telah berakhir…"))
Expected Output

undian lotre dimulai...

sedang mengundi nomor anda…

// if user win this lottery


selamat anda mendapatkan hadiah utama berupa mobil

// if user lose this lottery


maaf anda kurang beruntung

undian lotre telah berakhir...

PART 9 - Brute Force and Greedy

Problem 1 - Simple Equations

We have three different integers, x, y and z, which satisfy the following three relations:

● x+y+z=A
● xyz = B
● x^2 + y^2 + z^2 = C

You are asked to write a program that solves for x, y and z for given values of A, B and C. (1 ≤ A,
B, C ≤ 10000).

Sample Test Cases


Input: 1 2 3
Output: No solution.

Input: 6 6 14
Output: 1 2 3

function simpleEquations(a, b, c) {
// your code here
}
simpleEquations(1, 2, 3) // no solution
simpleEquations(6, 6, 14) // 1 2 3

Problem 2 - Dragon of Loowater

There are n dragon heads and m knights (1 ≤ n, m ≤ 20000). Each dragon head has a diameter
and each knight has a height. A dragon head with diameter D can be chopped off by a knight
with height H if D ≤ H. A knight can only chop off one dragon head. Given a list of diameters of
the dragon heads and a list of heights of the knights, is it possible to chop off all the dragon
heads? If yes, what is the minimum total height of the knights used to chop off the dragons’
heads?

Sample Test Cases


Input
[5, 4], [7, 8, 4]
Output
11

Input
[5, 10], [5]
Output
‘knight fall’

Input
[7, 2], [4, 3, 1, 2]
Output
‘knight fall’

Input
[7, 2], [2, 1, 8, 5]
Output
10

function dragonOfLoowater(dragonHead, knightHeight) {


// your code here
}

dragonOfLoowater([5, 4], [7, 8, 4]) // 11


dragonOfLoowater([5, 10], [5]) // knight fall
dragonOfLoowater([7, 2], [4, 3, 1, 2]) // knight fall
dragonOfLoowater([7, 2], [2, 1, 8, 5]) // 10

Problem 3 - Pangram

A pangram is a sentence using every letter of the alphabet at least once. The best known
English pangram is:

The quick brown fox jumps over the lazy dog.

The alphabet used consists of ASCII letters a to z, inclusive, and is case insensitive. Input will
not contain non-ASCII symbols. Determine if a sentence is a pangram.

Example Test Case


Test Case 1
Input: The quick brown fox jumps over the lazy dog
Output: true

Test Case 2
Input: Public junk dwarves hug my beloved pillow
Output: false

Test Case 3
Input: Jim quickly realized that the beautiful gowns are expensive
Output: true

PART 10 - D&C and Dynamic Programming

Problem 1 - Fibonacci Number Top-down


Write a function to calculate the nth Fibonacci number.

Fibonacci numbers are a series of numbers in which each number is the sum of the two
preceding numbers. First few Fibonacci numbers are: 0, 1, 1, 2, 3, 5, 8, …
Sample Test Cases
Input: 5
Output: 5

function fiboTopDown(n) {
// your code here
}

[Link](fiboTopDown(0)) // 0
[Link](fiboTopDown(1)) // 1
[Link](fiboTopDown(2)) // 1
[Link](fiboTopDown(3)) // 2
[Link](fiboTopDown(5)) // 5
[Link](fiboTopDown(6)) // 8
[Link](fiboTopDown(7)) // 13
[Link](fiboTopDown(9)) // 34
[Link](fiboTopDown(10)) // 55

Problem 2 - Binary Search Algorithm


In a binary search we use the information that all the elements are sorted. Let’s try to solve the
task in which we ask for the position of a value x in a sorted array. Let’s see how the number of
candidates is reduced, for example for the value x = 31.

For every step of the algorithm we should remember the beginning and the end of the
remaining slice of the array (respectively, variables beg and end). The middle element of the
slice can easily be calculated as mid = [(beg+end)/2].

Input: [1, 1, 3, 5, 5, 6, 7], x = 3


Output: 2

Input: [12, 15, 15, 19, 24, 31, 53, 59, 60], x = 100
Output: -1
function binarySearch(array, x) {
// your code here
}

binarySearch([1, 1, 3, 5, 5, 6, 7], 3) // 2
binarySearch([1, 2, 3, 5, 6, 8, 10], 5) // 3
binarySearch([12, 15, 15, 19, 24, 31, 53, 59, 60], 53) // 6
binarySearch([12, 15, 15, 19, 24, 31, 53, 59, 60], 100) // -1

Problem 3 - Frog

There are N stones, numbered 1, 2, … , N. For each i (1 <= i <= N), the height of Stone i is hi.
There is a frog who is initially on Stone 1. He will repeat the following action some number of
times to reach stone N:
● If the frog is currently on Stone i, jump to Stone i + 1 or Stone i + 2. Here, a cost of
| hi - hj | is incurred, where j is stone to land on.
Find the minimum possible total cost incurred before the frog reaches Stone N.

Constrain:
● All values in input are integers.
● 2 <= N <= 10^5
● 1 <= hi <= 10^4

Input: [10, 30, 40, 20]


Output: 30
If follow the path 1 -> 2 -> 4, the total cost incurred would be | 10 - 30 | + | 30 - 20 | = 30.

Input: [30, 10, 60, 10, 60, 50]


Output: 40
If follow the path 1 -> 3 -> 5 -> 6, the total cost incurred would be | 30 - 60 | + | 60 - 60 | + | 60 -
50 | = 40
function frog(jumps) {
// your code here
}

[Link](frog([10, 30, 40, 20])) // 30


[Link](frog([30, 10, 60, 10, 60, 50])) // 40

Problem 4 - Roman Numerals


Tulis program Javascript untuk mengkonversi dari angka normal ke Angka Romawi!

Input: 6
Output: VI

Input: 9
Output: IX

Input: 23
Output: XXIII

Input: 2021
Output: MMXXI

Input: 1646
Output: MDCXLVI

function romanNumerals(value) {
// your code here
}

[Link](romanNumerals(6)) // VI
[Link](romanNumerals(9)) // IX
[Link](romanNumerals(23)) // XXIII
[Link](romanNumerals(2021)) // MMXXI
[Link](romanNumerals(1646)) // MDCXLVI

Anda mungkin juga menyukai