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