fork download
  1. <?php
  2. class Kamus {
  3. private $kamus = [];
  4. public function tambah($kata, $sinonim) {
  5. if (isset($this->kamus[$kata])) {
  6. $this->kamus[$kata] = array_unique(array_merge($this->kamus[$kata], $sinonim));
  7. } else {
  8. $this->kamus[$kata] = $sinonim;
  9. }
  10. foreach ($sinonim as $synonym) {
  11. if (isset($this->kamus[$synonym])) {
  12. $this->kamus[$synonym] = array_unique(array_merge($this->kamus[$synonym], [$kata]));
  13. } else {
  14. $this->kamus[$synonym] = [$kata];
  15. }
  16. }
  17. }
  18.  
  19. public function ambilSinonim($kata) {
  20. if (isset($this->kamus[$kata])) {
  21. return $this->kamus[$kata];
  22. } else {
  23. return null;
  24. }
  25. }
  26. }
  27.  
  28. $kamus = new Kamus();
  29. $kamus->tambah('big', ['large', 'great']);
  30. $kamus->tambah('big', ['huge', 'fat']);
  31. $kamus->tambah('huge', ['enormous', 'gigantic']);
  32.  
  33. print_r($kamus->ambilSinonim('big'));
  34.  
  35. print_r($kamus->ambilSinonim('huge'));
  36.  
  37. print_r($kamus->ambilSinonim('gigantic'));
  38.  
  39. print_r($kamus->ambilSinonim('colossal'));
  40. ?>
  41.  
Success #stdin #stdout 0.04s 25544KB
stdin
Standard input is empty
stdout
Array
(
    [0] => large
    [1] => great
    [2] => huge
    [3] => fat
)
Array
(
    [0] => big
    [1] => enormous
    [2] => gigantic
)
Array
(
    [0] => huge
)