Skip to content

Commit 2113259

Browse files
committed
Merge branch 'release/v1.3.0'
2 parents 64a9e4e + 609900b commit 2113259

File tree

8 files changed

+267
-11
lines changed

8 files changed

+267
-11
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ Semua pembaruan pada `coding-interview` akan didokumentasikan pada dokumen ini.
44

55
## [Unreleased](https://github.com/ianriizky/coding-interview/compare/master...develop)
66

7+
## [v1.3.0 (2021-12-30)](https://github.com/ianriizky/coding-interview/compare/v1.2.0...v1.3.0)
8+
9+
### Baru
10+
- Studi kasus faktorial ([#1194c5c](https://github.com/ianriizky/coding-interview/commit/1194c5c041dd929af02aec2a0bd809229d893bc8)).
11+
12+
### Perubahan
13+
- Menambah keterangan UNLISTED di video youtube Programmer Zaman Now dataset ([#9dfd4d0](https://github.com/ianriizky/coding-interview/commit/9dfd4d03bb7b2f8540ac6931c38877ec8ab79bae)).
14+
- Perbarui readme di palindrome ([#9dfd4d0](https://github.com/ianriizky/coding-interview/commit/9dfd4d03bb7b2f8540ac6931c38877ec8ab79bae)).
15+
16+
717
## [v1.2.0 (2021-12-30)](https://github.com/ianriizky/coding-interview/compare/v1.1.4...v1.2.0)
818

919
### Baru

src/DataSet/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ Contoh *source code* dari studi kasus Dataset menggunakan bahasa pemrograman PHP
99

1010
<strong id="fn1">1</strong> [Pengertian Dataset dan Jenis-jenisnya, Kumparan](https://kumparan.com/kabar-harian/pengertian-dataset-dan-jenis-jenisnya-1wtM6xNlkpQ/2). [](#pg1)
1111
<br>
12-
<strong id="fn2">2</strong> [Programmer Zaman Now - Coding Interview Struktur Data Set](https://www.youtube.com/watch?v=OWm1vSHrC_A). [](#pg2)
12+
<strong id="fn2">2</strong> [Programmer Zaman Now - Coding Interview Struktur Data Set (UNLISTED)](https://www.youtube.com/watch?v=OWm1vSHrC_A). [](#pg2)
1313
<br>

src/Factorial/1.png

14.8 KB
Loading

src/Factorial/2.png

10.5 KB
Loading

src/Factorial/Factorial.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace Ianriizky\CodingInterview\Factorial;
4+
5+
class Factorial
6+
{
7+
/**
8+
* Temporary value for the recursive value.
9+
*
10+
* @var int
11+
*/
12+
protected int $result = 1;
13+
14+
/**
15+
* Create a new instance class.
16+
*
17+
* @param int $initialN
18+
* @return void
19+
*/
20+
public function __construct(protected int $initialN)
21+
{
22+
//
23+
}
24+
25+
/**
26+
* Return result of the factorial using loop way.
27+
*
28+
* @return int
29+
*/
30+
public function resultWithLoop(): int
31+
{
32+
if ($this->initialN <= 1) {
33+
return 1;
34+
}
35+
36+
$result = 1;
37+
38+
for ($n = $this->initialN; $n >= 1; $n--) {
39+
$result *= $n;
40+
}
41+
42+
return $result;
43+
}
44+
45+
/**
46+
* Return result of the factorial using recursive way.
47+
*
48+
* @return int
49+
*/
50+
public function resultWithRecursive(): int
51+
{
52+
if ($this->initialN <= 1) {
53+
return 1;
54+
}
55+
56+
$n = $this->initialN;
57+
58+
return $n * $this->resultWithRecursive($this->initialN = $n - 1);
59+
}
60+
61+
/**
62+
* Return result of the factorial using tail recursive way.
63+
*
64+
* @return int
65+
*/
66+
public function resultWithTailRecursive(): int
67+
{
68+
if ($this->initialN <= 0) {
69+
return $this->result;
70+
}
71+
72+
$this->result *= $this->initialN;
73+
$this->initialN--;
74+
75+
return $this->resultWithTailRecursive();
76+
}
77+
}

src/Factorial/README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Faktorial
2+
3+
Dalam matematika, Faktorial dari bilangan bulat positif dari n yang dilambangkan dengan n!, adalah produk dari semua bilangan bulat positif yang kurang dari atau sama dengan n:<sup id="pg1">[1](#fn1)</sup>.
4+
<br>
5+
![Rumus Faktorial](1.png)
6+
Sebagai contoh,
7+
<br>
8+
![Contoh Faktorial](2.png)
9+
<br>
10+
Nilai 0! adalah 1, menurut konvensi untuk produk kosong.
11+
12+
Pada studi kasus faktorial yang ada di *source code* [ini](Factorial.php), ada 3 metode yang bisa digunakan untuk menghitung nilai faktorial dari n yang diberikan. Metode tersebut di antaranya adalah:
13+
14+
## Metode *loop*<sup id="pg2">[2](#fn2)</sup>
15+
Di metode ini, kita menggunakan cara yang paling standar yaitu menggunakan perulangan. Contohnya dapat dilihat pada *source code* di bawah ini.
16+
```php
17+
if ($value <= 1) {
18+
return 1;
19+
}
20+
21+
$result = 1;
22+
23+
for ($n = $value; $n >= 1; $n--) {
24+
$result *= $n;
25+
}
26+
27+
return $result;
28+
```
29+
30+
## Metode *recursive*<sup id="pg2">[2](#fn2)</sup>
31+
Jika pada saat interview kita dilarang untuk menggunakan perulangan, kita bisa menggunakan cara rekursif. Rekursif adalah fungsi yang memanggil fungsi itu sendiri secara berulang. Contohnya dapat dilihat pada *source code* di bawah ini.
32+
```php
33+
$this->initialN = 5;
34+
35+
public function resultWithRecursive(): int
36+
{
37+
if ($this->initialN <= 1) {
38+
return 1;
39+
}
40+
41+
$n = $this->initialN;
42+
43+
return $n * $this->resultWithRecursive($this->initialN = $n - 1);
44+
}
45+
```
46+
47+
## Metode *tail recursive*<sup id="pg2">[2](#fn2)</sup>
48+
Jika kita perhatikan pada metode rekursif sebelumnya, kita bisa lihat bahwa fungsi tersebut harus menunggu hingga ke fungsi rekursif yang paling akhir di setiap ia memanggil dirinya sendiri.
49+
```php
50+
$this->initialN = 5;
51+
52+
$this->resultWithRecursive();
53+
// 5 * ($this->resultWithRecursive() = 4)
54+
// 5 * 4 * ($this->resultWithRecursive() = 3)
55+
// 5 * 4 * 3 * ($this->resultWithRecursive() = 2)
56+
// 5 * 4 * 3 * 2 * ($this->resultWithRecursive() = 1)
57+
// 5 * 4 * 3 * 2 * 1 * ($this->resultWithRecursive() = 0)
58+
```
59+
Salah satu masalah yang bisa muncul ketika kita menggunakan metode rekursif adalah terjadinya *memory leak*. Solusi yang bisa digunakan adalah menggunakan *tail recursive*. *Tail recursive* adalah rekursif yang seakan-akan tidak menunggu rekursif sebelumnya. Sehingga hasil eksekusinya akan menjadi seperti berikut ini.
60+
```php
61+
$this->initialN = 5;
62+
63+
$this->resultWithTailRecursive();
64+
// $this->resultWithTailRecursive() // $this->iniitalN = 5, $this->result = 1
65+
// $this->resultWithTailRecursive() // $this->iniitalN = 4, $this->result = 5
66+
// $this->resultWithTailRecursive() // $this->iniitalN = 3, $this->result = 20
67+
// $this->resultWithTailRecursive() // $this->iniitalN = 2, $this->result = 60
68+
// $this->resultWithTailRecursive() // $this->iniitalN = 1, $this->result = 120
69+
// $this->resultWithTailRecursive() // $this->iniitalN = 0, $this->result = 120
70+
```
71+
72+
Contoh dari metode *tail recursive* dapat dilihat pada *source code* di bawah ini.
73+
```php
74+
$this->initialN = 5;
75+
76+
public function resultWithTailRecursive(): int
77+
{
78+
if ($this->initialN <= 0) {
79+
return $this->result;
80+
}
81+
82+
$this->result *= $this->initialN;
83+
$this->initialN--;
84+
85+
return $this->resultWithTailRecursive();
86+
}
87+
```
88+
89+
---
90+
### Catatan Kaki:
91+
92+
<strong id="fn1">1</strong> [Faktorial, Wikipedia](https://id.wikipedia.org/wiki/Faktorial). [](#pg1)
93+
<br>
94+
<strong id="fn2">2</strong> [Programmer Zaman Now - Coding Interview Factorial (UNLISTED)](https://www.youtube.com/watch?v=tcPmCOAl-X4). [](#pg2)
95+
<br>

src/Palindrome/README.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,27 @@ Ada optimasi yang dilakukan pada *source code* di atas, di mana batas perulangan
5454
## Metode *recursive*<sup id="pg5">[5](#fn5)</sup>
5555
Metode ini prinsipnya hampir sama dengan metode *loop* yang telah dijelaskan sebelumnya. Perbedaan dari metode *recursive* ini hanya terletak pada dihilangkannya perulangan ``for()`` dan pengecekan batas perulangan diganti menjadi ``if ($index < floor(strlen($value) / 2))`` sehingga menghasilkan pemanggilan fungsi yang sama secara *recursive* sebagaimana dapat dilihat pada *source code* di bawah ini.
5656
```php
57-
$value = 'katak';
57+
$this->value = 'katak';
5858

59-
if ($index < floor(strlen($value) / 2)) {
60-
$lastCharacterIndex = strlen($value) - ($index + 1);
59+
public function checkUsingRecursive(int $index = 0): bool
60+
{
61+
$value = $this->value;
6162

62-
$firstCharacter = $value[$index];
63-
$lastCharacter = $value[$lastCharacterIndex];
63+
if ($index < floor(strlen($value) / 2)) {
64+
$lastCharacterIndex = strlen($value) - ($index + 1);
6465

65-
if ($firstCharacter !== $lastCharacter) {
66-
return false;
66+
$firstCharacter = $value[$index];
67+
$lastCharacter = $value[$lastCharacterIndex];
68+
69+
if ($firstCharacter !== $lastCharacter) {
70+
return false;
71+
}
72+
73+
return $this->checkUsingRecursive($index + 1);
6774
}
6875

69-
return $this->checkUsingRecursive($index + 1); // kunci recursive ada di sini
76+
return true;
7077
}
71-
72-
return true;
7378
```
7479

7580
---

tests/Unit/FactorialTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use Ianriizky\CodingInterview\Factorial\Factorial;
6+
use Tests\TestCase;
7+
8+
class FactorialTest extends TestCase
9+
{
10+
protected array $factorials = [
11+
0 => 1,
12+
1 => 1,
13+
2 => 2,
14+
3 => 6,
15+
4 => 24,
16+
5 => 120,
17+
6 => 720,
18+
7 => 5040,
19+
8 => 40320,
20+
9 => 362880,
21+
10 => 3628800,
22+
11 => 39916800,
23+
12 => 479001600,
24+
13 => 6227020800,
25+
14 => 87178291200,
26+
15 => 1307674368000,
27+
16 => 20922789888000,
28+
17 => 355687428096000,
29+
18 => 6402373705728000,
30+
19 => 121645100408832000,
31+
20 => 2432902008176640000,
32+
];
33+
34+
/**
35+
* Assert that factorial class with loop is run correctly.
36+
*
37+
* @return void
38+
*/
39+
public function test_factorial_with_loop()
40+
{
41+
foreach ($this->factorials as $n => $factorial) {
42+
$this->assertEquals($factorial, (new Factorial($n))->resultWithLoop());
43+
}
44+
}
45+
46+
/**
47+
* Assert that factorial class with recursive is run correctly.
48+
*
49+
* @return void
50+
*/
51+
public function test_factorial_with_recursive()
52+
{
53+
foreach ($this->factorials as $n => $factorial) {
54+
$this->assertEquals($factorial, (new Factorial($n))->resultWithRecursive());
55+
}
56+
}
57+
58+
/**
59+
* Assert that factorial class with tail recursive is run correctly.
60+
*
61+
* @return void
62+
*/
63+
public function test_factorial_with_tail_recursive()
64+
{
65+
foreach ($this->factorials as $n => $factorial) {
66+
$this->assertEquals($factorial, (new Factorial($n))->resultWithTailRecursive());
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)