fork download
  1. process.stdin.resume();
  2. process.stdin.setEncoding('utf8');
  3.  
  4. process.stdin.resume();
  5. process.stdin.setEncoding('utf8');
  6.  
  7. class Kamus {
  8. constructor() {
  9. this.data = new Map();
  10. }
  11.  
  12. tambah(kata, sinonim) {
  13. if (!this.data.has(kata)) {
  14. this.data.set(kata, []);
  15. }
  16. const daftar = this.data.get(kata);
  17. for (const s of sinonim) {
  18. if (!daftar.includes(s)) {
  19. daftar.push(s);
  20. }
  21. }
  22. }
  23.  
  24. ambilSinonim(kata) {
  25. const hasil = [];
  26.  
  27. for (const [kunci, daftar] of this.data) {
  28. if (kunci !== kata && daftar.includes(kata)) {
  29. hasil.push(kunci);
  30. }
  31. }
  32.  
  33. if (this.data.has(kata)) {
  34. for (const s of this.data.get(kata)) {
  35. if (!hasil.includes(s)) {
  36. hasil.push(s);
  37. }
  38. }
  39. }
  40.  
  41. return hasil.length === 0 ? null : hasil;
  42. }
  43. }
  44.  
  45. const kamus = new Kamus();
  46. kamus.tambah('big', ['large', 'great']);
  47. kamus.tambah('big', ['huge', 'fat']);
  48. kamus.tambah('huge', ['enormous', 'gigantic']);
  49.  
  50. console.log(kamus.ambilSinonim('big'));
  51. console.log(kamus.ambilSinonim('huge'));
  52. console.log(kamus.ambilSinonim('gigantic'));
  53. console.log(kamus.ambilSinonim('colossal'));
  54.  
Success #stdin #stdout 0.06s 45580KB
stdin
Standard input is empty
stdout
[ 'large', 'great', 'huge', 'fat' ]
[ 'big', 'enormous', 'gigantic' ]
[ 'huge' ]
null