fork download
  1. class Kamus:
  2. def __init__(self):
  3. self.data = {}
  4.  
  5. def tambah(self, kata, sinonim_list):
  6. if kata not in self.data:
  7. self.data[kata] = []
  8. for sinonim in sinonim_list:
  9. if sinonim not in self.data[kata]:
  10. self.data[kata].append(sinonim)
  11.  
  12. def ambilSinonim(self, kata):
  13. lists = []
  14. for kunci, sinonim_list in self.data.items():
  15. if kata in sinonim_list:
  16. lists.append(kunci)
  17. if kata in self.data:
  18. lists.extend(self.data[kata])
  19. if lists:
  20. return lists
  21. return None
  22.  
  23. # Pengujian contoh
  24. kamus = Kamus()
  25. kamus.tambah('big', ['large', 'great'])
  26. kamus.tambah('big', ['huge', 'fat'])
  27. kamus.tambah('huge', ['enormous', 'gigantic'])
  28.  
  29. print(kamus.ambilSinonim('big'))
  30. print(kamus.ambilSinonim('huge'))
  31. print(kamus.ambilSinonim('gigantic'))
  32. print(kamus.ambilSinonim('colossal'))
Success #stdin #stdout 0.13s 14140KB
stdin
Standard input is empty
stdout
['large', 'great', 'huge', 'fat']
['big', 'enormous', 'gigantic']
['huge']
None