fork download
  1. import collections
  2.  
  3. class Kamus:
  4.  
  5. def __init__(self):
  6.  
  7. self.data = collections.defaultdict(list)
  8.  
  9. def tambah(self, kata, sinonim_list):
  10.  
  11. for s in sinonim_list:
  12. if s not in self.data[kata]:
  13. self.data[kata].append(s)
  14.  
  15.  
  16. if kata not in self.data[s]:
  17. self.data[s].append(kata)
  18.  
  19. def ambilSinonim(self, kata):
  20.  
  21. if kata in self.data:
  22. return self.data[kata]
  23. return None
  24.  
  25.  
  26. kamus = Kamus()
  27.  
  28. # Menambahkan kata dan sinonim
  29. kamus.tambah('big', ['large', 'great'])
  30. kamus.tambah('big', ['huge', 'fat'])
  31. kamus.tambah('huge', ['enormous', 'gigantic'])
  32.  
  33. # Pengujian 1: mengambil sinonim 'big'
  34. print(f"Sinonim untuk 'big': {kamus.ambilSinonim('big')}")
  35.  
  36. # Pengujian 2: mengambil sinonim 'huge'
  37. print(f"Sinonim untuk 'huge': {kamus.ambilSinonim('huge')}")
  38.  
  39. # Pengujian 3: mengambil sinonim 'gigantic'
  40. print(f"Sinonim untuk 'gigantic': {kamus.ambilSinonim('gigantic')}")
  41.  
  42. # Pengujian 4: mengambil sinonim 'colossal'
  43. print(f"Sinonim untuk 'colossal': {kamus.ambilSinonim('colossal')}")
Success #stdin #stdout 0.12s 14052KB
stdin
Standard input is empty
stdout
Sinonim untuk 'big': ['large', 'great', 'huge', 'fat']
Sinonim untuk 'huge': ['big', 'enormous', 'gigantic']
Sinonim untuk 'gigantic': ['huge']
Sinonim untuk 'colossal': None