fork download
  1. <?php
  2. class Kamus {
  3. private $kamus = array();
  4.  
  5. public function tambah(string $kata, array $sinonim) {
  6. // tidak mengembalikan hasil (void)
  7.  
  8. if(!in_array($kata, $kamus)) {
  9. $kamus[$kata] = array();
  10. }
  11.  
  12. $kamus[$kata] = array_merge($kamus[$kata], $sinonim);
  13. $kamus[$kata] = array_unique($kamus[$kata]);
  14.  
  15. foreach($sinonim as $other_kata) {
  16. if(!in_array($other_kata, $kamus)) {
  17. $kamus[$other_kata] = $kata;
  18. }
  19.  
  20. $kamus[$other_kata] = array_merge($kamus[$other_kata], $sinonim);
  21. $kamus[$other_kata] = array_unique($kamus[$other_kata]);
  22. }
  23.  
  24. return;
  25. }
  26.  
  27. public function ambilSinonim(string $kata) {
  28. // mengembalikan hasil array of strings
  29. return $kamus[$kata];
  30. }
  31. }
  32.  
  33. $kamus = new Kamus();
  34. $kamus->tambah('big', ['large', 'great']);
  35. $kamus->tambah('big', ['huge', 'fat']);
  36. $kamus->tambah('huge', ['enormous', 'gigantic']);
  37. // mengembalikan hasil ['large', 'great', 'huge', 'fat']
  38. $kamus->ambilSinonim('big');
  39. // Perhatikan baik-baik hasil pengujian di bawah ini
  40. // mengembalikan hasil ['big', 'enormous', 'gigantic']
  41. $kamus->ambilSinonim('huge');
  42. // mengembalikan hasil ['huge']
  43. $kamus->ambilSinonim('gigantic');
  44. // mengembalikan hasil null
  45. $kamus->ambilSinonim('colossal');
  46. ?>
Success #stdin #stdout #stderr 0.02s 26284KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
PHP Notice:  Undefined variable: kamus in /home/hPL174/prog.php on line 8
PHP Warning:  in_array() expects parameter 2 to be array, null given in /home/hPL174/prog.php on line 8
PHP Warning:  array_merge(): Expected parameter 1 to be an array, string given in /home/hPL174/prog.php on line 20
PHP Warning:  array_unique() expects parameter 1 to be array, null given in /home/hPL174/prog.php on line 21
PHP Warning:  array_merge(): Expected parameter 1 to be an array, string given in /home/hPL174/prog.php on line 20
PHP Warning:  array_unique() expects parameter 1 to be array, null given in /home/hPL174/prog.php on line 21
PHP Notice:  Undefined variable: kamus in /home/hPL174/prog.php on line 8
PHP Warning:  in_array() expects parameter 2 to be array, null given in /home/hPL174/prog.php on line 8
PHP Warning:  array_merge(): Expected parameter 1 to be an array, string given in /home/hPL174/prog.php on line 20
PHP Warning:  array_unique() expects parameter 1 to be array, null given in /home/hPL174/prog.php on line 21
PHP Warning:  array_merge(): Expected parameter 1 to be an array, string given in /home/hPL174/prog.php on line 20
PHP Warning:  array_unique() expects parameter 1 to be array, null given in /home/hPL174/prog.php on line 21
PHP Notice:  Undefined variable: kamus in /home/hPL174/prog.php on line 8
PHP Warning:  in_array() expects parameter 2 to be array, null given in /home/hPL174/prog.php on line 8
PHP Warning:  array_merge(): Expected parameter 1 to be an array, string given in /home/hPL174/prog.php on line 20
PHP Warning:  array_unique() expects parameter 1 to be array, null given in /home/hPL174/prog.php on line 21
PHP Warning:  array_merge(): Expected parameter 1 to be an array, string given in /home/hPL174/prog.php on line 20
PHP Warning:  array_unique() expects parameter 1 to be array, null given in /home/hPL174/prog.php on line 21
PHP Notice:  Undefined variable: kamus in /home/hPL174/prog.php on line 29
PHP Notice:  Undefined variable: kamus in /home/hPL174/prog.php on line 29
PHP Notice:  Undefined variable: kamus in /home/hPL174/prog.php on line 29
PHP Notice:  Undefined variable: kamus in /home/hPL174/prog.php on line 29