fork download
  1. class Kamus:
  2. def __init__(self):
  3. self.data = {}
  4.  
  5. def tambah(self, kata, sinonim):
  6. for s in sinonim:
  7. if s == kata:
  8. continue
  9. self._hubungkan(kata, s)
  10. self._hubungkan(s, kata)
  11.  
  12. def ambilSinonim(self, kata):
  13. if kata not in self.data:
  14. return None
  15. return list(self.data[kata])
  16.  
  17. def _hubungkan(self, a, b):
  18. if a not in self.data:
  19. self.data[a] = []
  20. if b not in self.data[a]:
  21. self.data[a].append(b)
  22.  
  23.  
  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')) # ['large', 'great', 'huge', 'fat']
  30. print(kamus.ambilSinonim('huge')) # ['big', 'enormous', 'gigantic']
  31. print(kamus.ambilSinonim('gigantic')) # ['huge']
  32. print(kamus.ambilSinonim('colossal')) # None
Success #stdin #stdout 0.01s 7252KB
stdin
Standard input is empty
stdout
['large', 'great', 'huge', 'fat']
['big', 'enormous', 'gigantic']
['huge']
None