fork download
  1. // Nama: Atyla Azfa Al Harits
  2. // NIM: 19230825
  3. // Universitas: Universitas Bina Sarana Informatika
  4. // Email: damsadam78@gmail.com
  5.  
  6.  
  7. <?php
  8.  
  9. class Kamus{
  10. private $penyimpanan = [];
  11.  
  12.  
  13. public function tambah(string $kata, array $sinonim): void {
  14. $oldSinonim = $this->penyimpanan[$kata] ?? [];
  15. $this->penyimpanan[$kata] = array_unique(array_merge($oldSinonim, $sinonim));
  16.  
  17. foreach($sinonim as $item){
  18. $getOldSinonimInStorage = $this->penyimpanan[$item] ?? [];
  19. $this->penyimpanan[$item] = array_unique(array_merge($getOldSinonimInStorage, [$kata]));
  20. }
  21.  
  22. }
  23.  
  24.  
  25. public function ambilSinonim(string $kata): ?array {
  26. return $this->penyimpanan[$kata] ?? null;
  27. }
  28. }
  29.  
  30.  
  31. $kamus = new Kamus();
  32.  
  33.  
  34. $kamus->tambah('big', ['large', 'great']);
  35. $kamus->tambah('big', ['huge', 'fat']);
  36. $kamus->tambah('huge', ['enormous', 'gigantic']);
  37.  
  38.  
  39. print_r($kamus->ambilSinonim('big'));
  40. print_r($kamus->ambilSinonim('huge'));
  41. print_r($kamus->ambilSinonim('gigantic'));
  42. var_dump($kamus->ambilSinonim('colossal'));
  43.  
Success #stdin #stdout 0.03s 26004KB
stdin
Standard input is empty
stdout
// Nama: Atyla Azfa Al Harits
// NIM: 19230825
// Universitas: Universitas Bina Sarana Informatika
// Email: damsadam78@gmail.com


Array
(
    [0] => large
    [1] => great
    [2] => huge
    [3] => fat
)
Array
(
    [0] => big
    [1] => enormous
    [2] => gigantic
)
Array
(
    [0] => huge
)
NULL