Create solution_01.py
This commit is contained in:
parent
48b194be16
commit
f6723def83
16
CH_04_design_patterns/exercise_01/solution_01.py
Normal file
16
CH_04_design_patterns/exercise_01/solution_01.py
Normal file
@ -0,0 +1,16 @@
|
||||
from collections.abc import Callable
|
||||
|
||||
class SortedDict:
|
||||
def __init__(self, _dict: dict, keyfunc: Callable) -> None:
|
||||
self.unsorted = _dict
|
||||
self.keyfunc = keyfunc
|
||||
|
||||
self._dict = self.sort()
|
||||
|
||||
def sort(self) -> dict:
|
||||
sorted_list = sorted(self.unsorted.items(), key=self.keyfunc)
|
||||
sorted_dict = {k:v for k, v in sorted_list}
|
||||
return sorted_dict
|
||||
|
||||
def __repr__(self):
|
||||
return repr(self._dict)
|
||||
Loading…
x
Reference in New Issue
Block a user