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(', ', array_map(function($x) { return "'$x'"; }, $this->data[$kata])) . "]\n";
  17. return;
  18. }
  19.  
  20. foreach ($this->data as $key => $daftar) {
  21. if (in_array($kata, $daftar)) {
  22. if (isset($this->data[$kata])) {
  23. echo "[" . implode(', ', array_map(function($x) { return "'$x'"; }, $this->data[$kata])) . "]\n";
  24. } else {
  25. echo "['$key']\n";
  26. }
  27. return;
  28. }
  29. }
  30.  
  31. echo "null\n";
  32. }
  33. }
  34.  
  35. $kamus = new Kamus();
  36. $kamus->tambah('big', ['large', 'great']);
  37. $kamus->tambah('big', ['huge', 'fat']);
  38. $kamus->tambah('huge', ['enormous', 'gigantic']);
  39.  
  40. $kamus->ambilSinonim('big');
  41. $kamus->ambilSinonim('huge');
  42. $kamus->ambilSinonim('gigantic');
  43. $kamus->ambilSinonim('colossal');
Success #stdin #stdout 0.02s 26060KB
stdin
Standard input is empty
stdout
['large', 'great', 'huge', 'fat']
['enormous', 'gigantic']
['huge']
null