fork download
  1. <?php
  2. class Kamus {
  3. private $kamus = [];
  4.  
  5. public function tambah(string $kata, array $sinonim) {
  6. // tidak mengembalikan hasil (void)
  7.  
  8. if(!isset($this->kamus[$kata])) {
  9. $this->kamus[$kata] = [];
  10. }
  11.  
  12. foreach($sinonim as $other_kata) {
  13. if(!in_array($other_kata, $this->kamus[$kata])) {
  14. $this->kamus[$kata] += $other_kata;
  15. }
  16.  
  17. if (!isset($this->kamus[$other_kata])) {
  18. $this->kamus[$other_kata] = [];
  19. }
  20.  
  21. if (!in_array($kata, $this->kamus[$other_kata])) {
  22. $this->kamus[$other_kata] += $kata;
  23. }
  24.  
  25. foreach($sinonim as $other_kata2) {
  26. if ($other_kata == $other_kata2) continue;
  27.  
  28. if (!in_array($other_kata2, $this->kamus[$other_kata])) {
  29. $this->kamus[$other_kata] += $other_kata2;
  30. }
  31. }
  32. }
  33.  
  34. return;
  35. }
  36.  
  37. public function ambilSinonim(string $kata) {
  38. // mengembalikan hasil array of strings
  39. return $this->kamus[$kata];
  40. }
  41. }
  42.  
  43. $kamus = new Kamus();
  44. $kamus->tambah('big', ['large', 'great']);
  45. $kamus->tambah('big', ['huge', 'fat']);
  46. $kamus->tambah('huge', ['enormous', 'gigantic']);
  47. // mengembalikan hasil ['large', 'great', 'huge', 'fat']
  48. $kamus->ambilSinonim('big');
  49. // Perhatikan baik-baik hasil pengujian di bawah ini
  50. // mengembalikan hasil ['big', 'enormous', 'gigantic']
  51. $kamus->ambilSinonim('huge');
  52. // mengembalikan hasil ['huge']
  53. $kamus->ambilSinonim('gigantic');
  54. // mengembalikan hasil null
  55. $kamus->ambilSinonim('colossal');
  56. ?>
Success #stdin #stdout #stderr 0.03s 26116KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
PHP Notice:  Undefined index: colossal in /home/WLWIqx/prog.php on line 29