fork download
  1. from typing import List, Set, Dict, Optional
  2.  
  3. class Kamus:
  4. def __init__(self):
  5. self._data: Dict[str, Set[str]] = {}
  6.  
  7. def tambah(self, kata: str, sinonim: List[str]) -> None:
  8. self._data.setdefault(kata, set()).update(sinonim)
  9. for s in sinonim:
  10. self._data.setdefault(s, set()).add(kata)
  11.  
  12. def ambilSinonim(self, kata: str) -> Optional[List[str]]:
  13. hasil_set = self._data.get(kata)
  14. if hasil_set is not None:
  15. return list(hasil_set)
  16. return None
  17.  
  18. def main_demonstrasi():
  19. # Inisialisasi dan penambahan data
  20. kamus = Kamus()
  21. kamus.tambah('big', ['large', 'great'])
  22. kamus.tambah('big', ['huge', 'fat'])
  23. kamus.tambah('huge', ['enormous', 'gigantic'])
  24.  
  25. # Menjalankan pengujian dan mencetak hasilnya saja
  26. hasil_big = kamus.ambilSinonim('big')
  27. print(sorted(hasil_big) if hasil_big else None)
  28.  
  29. hasil_huge = kamus.ambilSinonim('huge')
  30. print(sorted(hasil_huge) if hasil_huge else None)
  31.  
  32. hasil_gigantic = kamus.ambilSinonim('gigantic')
  33. print(hasil_gigantic)
  34.  
  35. hasil_colossal = kamus.ambilSinonim('colossal')
  36. print(hasil_colossal)
  37.  
  38. if __name__ == "__main__":
  39. main_demonstrasi()
Success #stdin #stdout 0.18s 16620KB
stdin
Standard input is empty
stdout
['fat', 'great', 'huge', 'large']
['big', 'enormous', 'gigantic']
['huge']
None