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